11package springfox .documentation .builders ;
22
33import org .springframework .lang .NonNull ;
4+ import org .springframework .lang .Nullable ;
5+ import springfox .documentation .annotations .Incubating ;
46import springfox .documentation .schema .CollectionSpecification ;
57import springfox .documentation .schema .CompoundModelSpecification ;
68import springfox .documentation .schema .MapSpecification ;
1517import java .util .function .Consumer ;
1618import java .util .stream .Stream ;
1719
20+ import static springfox .documentation .builders .BuilderDefaults .*;
1821import static springfox .documentation .builders .NoopValidator .*;
1922
2023public class ModelSpecificationBuilder {
@@ -27,11 +30,21 @@ public class ModelSpecificationBuilder {
2730 private CompoundModelSpecificationBuilder compoundModelBuilder ;
2831 private final Validator <ModelSpecificationBuilder > validator = this ::validateSpecification ;
2932
30- public ModelSpecificationBuilder name (String name ) {
31- this .name = name ;
33+ /**
34+ * Updates the name of the model
35+ * @param name - Name of the model
36+ * @return this
37+ */
38+ public ModelSpecificationBuilder name (@ NonNull String name ) {
39+ this .name = defaultIfAbsent (name , this .name );
3240 return this ;
3341 }
3442
43+ /**
44+ * Provides a consumer to build facets
45+ * @param facets - consumer that facilitates building a facet
46+ * @return this
47+ */
3548 public ModelSpecificationBuilder facets (@ NonNull Consumer <ModelFacetsBuilder > facets ) {
3649 if (facetsBuilder == null ) {
3750 facetsBuilder = new ModelFacetsBuilder ();
@@ -40,6 +53,11 @@ public ModelSpecificationBuilder facets(@NonNull Consumer<ModelFacetsBuilder> fa
4053 return this ;
4154 }
4255
56+ /**
57+ * Updates the scalar type
58+ * @param type - scalar type
59+ * @return this
60+ */
4361 public ModelSpecificationBuilder scalarModel (ScalarType type ) {
4462 if (type != null ) {
4563 this .scalar = new ScalarModelSpecification (type );
@@ -54,37 +72,121 @@ private CompoundModelSpecificationBuilder compoundModelBuilder() {
5472 return compoundModelBuilder ;
5573 }
5674
75+ /**
76+ * Provides a consumer to help building a compound model
77+ * @param compound - consumer that facilitates building a compound model
78+ * @return this
79+ */
5780 public ModelSpecificationBuilder compoundModel (@ NonNull Consumer <CompoundModelSpecificationBuilder > compound ) {
5881 compound .accept (compoundModelBuilder ());
5982 return this ;
6083 }
6184
85+ /**
86+ * Provides a consumer to help build a compound model if one already exists. If the model is a different type,
87+ * then any operations done with the help of the consumer will be a no-op
88+ * @param compound consumer that facilitates building a compound model
89+ * @return this
90+ */
91+ public ModelSpecificationBuilder compoundModelIfExists (
92+ @ NonNull Consumer <CompoundModelSpecificationBuilder > compound ) {
93+ if (compoundModelBuilder != null ) {
94+ compound .accept (compoundModelBuilder );
95+ } else {
96+ CompoundModelSpecificationBuilder throwAwayBuilder = new CompoundModelSpecificationBuilder ();
97+ compound .accept (throwAwayBuilder );
98+ }
99+ return this ;
100+ }
101+
102+ /**
103+ * Provides a consumer to build the collection model
104+ * @param consumer consumer that facilitates building a collection
105+ * @return this
106+ */
62107 public ModelSpecificationBuilder collectionModel (@ NonNull Consumer <CollectionSpecificationBuilder > consumer ) {
63108 consumer .accept (collectionBuilder ());
64109 return this ;
65110 }
66111
112+ /**
113+ * Conditionally provides a consumer to build the colleciton model. If the model is a different type,
114+ * then any operations done with the help of the consumer will be a no-op
115+ * @param consumer consumer that facilitates building a collection
116+ * @return this
117+ */
118+ public ModelSpecificationBuilder collectionModelIfExists (
119+ @ NonNull Consumer <CollectionSpecificationBuilder > consumer ) {
120+ if (collection != null ) {
121+ consumer .accept (collection );
122+ } else {
123+ CollectionSpecificationBuilder throwAwayBuilder = new CollectionSpecificationBuilder ();
124+ consumer .accept (throwAwayBuilder );
125+ }
126+ return this ;
127+ }
128+
67129 private CollectionSpecificationBuilder collectionBuilder () {
68130 if (collection == null ) {
69131 collection = new CollectionSpecificationBuilder ();
70132 }
71133 return collection ;
72134 }
73135
136+ /**
137+ * Provides a consumer to build the map model.
138+ * @param consumer consumer that facilitates building a map
139+ * @return this
140+ */
74141 public ModelSpecificationBuilder mapModel (@ NonNull Consumer <MapSpecificationBuilder > consumer ) {
75142 consumer .accept (mapBuilder ());
76143 return this ;
77144 }
78145
146+ /**
147+ * Conditionally provides a consumer to build the map model.
148+ * @param consumer consumer that facilitates building a map
149+ * @return this
150+ */
151+ public ModelSpecificationBuilder mapModelIfExists (@ NonNull Consumer <MapSpecificationBuilder > consumer ) {
152+ if (map != null ) {
153+ consumer .accept (map );
154+ } else {
155+ MapSpecificationBuilder throwAwayBuilder = new MapSpecificationBuilder ();
156+ consumer .accept (throwAwayBuilder );
157+ }
158+ return this ;
159+ }
160+
79161 private MapSpecificationBuilder mapBuilder () {
80162 if (map == null ) {
81163 map = new MapSpecificationBuilder ();
82164 }
83165 return map ;
84166 }
85167
86- public ModelSpecificationBuilder referenceModel (Consumer <ReferenceModelSpecificationBuilder > modelKey ) {
87- modelKey .accept (referenceModelBuilder ());
168+ /**
169+ * Provides a consumer to build the reference model.
170+ * @param consumer consumer that facilitates building a reference model
171+ * @return this
172+ */
173+ public ModelSpecificationBuilder referenceModel (Consumer <ReferenceModelSpecificationBuilder > consumer ) {
174+ consumer .accept (referenceModelBuilder ());
175+ return this ;
176+ }
177+
178+ /**
179+ * Provides a consumer to build the reference model.
180+ * @param consumer consumer that facilitates building a reference model
181+ * @return this
182+ */
183+ public ModelSpecificationBuilder referenceModelIfExists (Consumer <ReferenceModelSpecificationBuilder > consumer ) {
184+ if (referenceModel != null ) {
185+ consumer .accept (referenceModel );
186+ } else {
187+ ReferenceModelSpecificationBuilder throwAwayBuilder = new ReferenceModelSpecificationBuilder ();
188+ consumer .accept (throwAwayBuilder );
189+ }
88190 return this ;
89191 }
90192
@@ -112,7 +214,12 @@ public ModelSpecification build() {
112214 referenceModel != null ? referenceModel .build () : null );
113215 }
114216
115- public ModelSpecificationBuilder copyOf (ModelSpecification other ) {
217+ /**
218+ * Copies from an other model
219+ * @param other the other model
220+ * @return this
221+ */
222+ public ModelSpecificationBuilder copyOf (@ Nullable ModelSpecification other ) {
116223 if (other != null ) {
117224 ScalarType scalar = other .getScalar ()
118225 .map (ScalarModelSpecification ::getType )
@@ -180,6 +287,12 @@ private Object safeReferenceBuild() {
180287 return referenceModel != null ? referenceModel .build () : null ;
181288 }
182289
290+ /**
291+ * This is an experimental API. May be removed/modified
292+ * @param scalar - scalar to replace the models with
293+ * @return this
294+ */
295+ @ Incubating ("3.0.0" )
183296 public ModelSpecificationBuilder maybeConvertToScalar (ScalarType scalar ) {
184297 scalarModel (scalar );
185298 if (compoundModelBuilder != null ) {
0 commit comments