11package io .swagger .jaxrs ;
22
33import io .swagger .annotations .ApiImplicitParam ;
4+ import io .swagger .annotations .ApiParam ;
5+ import io .swagger .converter .ModelConverters ;
6+ import io .swagger .models .ArrayModel ;
7+ import io .swagger .models .Model ;
8+ import io .swagger .models .ModelImpl ;
9+ import io .swagger .models .RefModel ;
10+ import io .swagger .models .Swagger ;
11+ import io .swagger .models .parameters .AbstractSerializableParameter ;
12+ import io .swagger .models .parameters .BodyParameter ;
13+ import io .swagger .models .parameters .Parameter ;
14+ import io .swagger .models .properties .ArrayProperty ;
15+ import io .swagger .models .properties .Property ;
16+ import io .swagger .models .properties .PropertyBuilder ;
17+ import io .swagger .models .properties .RefProperty ;
418
519import java .lang .annotation .Annotation ;
620import java .lang .reflect .Method ;
923import java .util .List ;
1024import java .util .Map ;
1125
26+ import javax .ws .rs .DefaultValue ;
1227import javax .ws .rs .core .Context ;
1328
14- import io .swagger .models .ModelImpl ;
15- import io .swagger .models .Swagger ;
16- import io .swagger .models .parameters .AbstractSerializableParameter ;
17- import io .swagger .models .properties .PropertyBuilder ;
1829import org .apache .commons .lang3 .StringUtils ;
1930import org .slf4j .Logger ;
2031import org .slf4j .LoggerFactory ;
2334import com .fasterxml .jackson .databind .type .TypeFactory ;
2435import com .google .common .collect .HashBasedTable ;
2536import com .google .common .collect .Table ;
26- import io .swagger .annotations .ApiParam ;
27- import io .swagger .converter .ModelConverters ;
28- import io .swagger .models .ArrayModel ;
29- import io .swagger .models .Model ;
30- import io .swagger .models .RefModel ;
31- import io .swagger .models .parameters .BodyParameter ;
32- import io .swagger .models .parameters .Parameter ;
33- import io .swagger .models .properties .ArrayProperty ;
34- import io .swagger .models .properties .Property ;
35- import io .swagger .models .properties .RefProperty ;
3637
3738public class ParameterProcessor {
3839 static Logger LOGGER = LoggerFactory .getLogger (ParameterProcessor .class );
@@ -52,6 +53,7 @@ public static Parameter applyAnnotations(Swagger swagger, Parameter parameter, T
5253 return null ;
5354 }
5455 final ParamWrapper <?> param = helper .getApiParam ();
56+ final String defaultValue = helper .getDefaultValue ();
5557 final JavaType javaType = TypeFactory .defaultInstance ().constructType (type );
5658 if (parameter instanceof AbstractSerializableParameter ) {
5759 final AbstractSerializableParameter <?> p = (AbstractSerializableParameter <?>) parameter ;
@@ -80,7 +82,6 @@ public static Parameter applyAnnotations(Swagger swagger, Parameter parameter, T
8082 }
8183 }
8284
83- final String defaultValue = param .getDefaultValue ();
8485 if (p .getItems () != null || param .isAllowMultiple ()) {
8586 if (p .getItems () == null ) {
8687 // Convert to array
@@ -102,14 +103,14 @@ public static Parameter applyAnnotations(Swagger swagger, Parameter parameter, T
102103 }
103104
104105 final Map <PropertyBuilder .PropertyId , Object > args = new EnumMap <PropertyBuilder .PropertyId , Object >(PropertyBuilder .PropertyId .class );
105- if (! defaultValue . isEmpty ( )) {
106+ if (StringUtils . isNotEmpty ( defaultValue )) {
106107 args .put (PropertyBuilder .PropertyId .DEFAULT , defaultValue );
107108 }
108109 processAllowedValues (allowableValues , true , args );
109110 PropertyBuilder .merge (p .getItems (), args );
110111 p .collectionFormat ("csv" );
111112 } else {
112- if (! defaultValue . isEmpty ( )) {
113+ if (StringUtils . isNotEmpty ( defaultValue )) {
113114 p .setDefaultValue (defaultValue );
114115 }
115116 processAllowedValues (allowableValues , false , p );
@@ -147,6 +148,9 @@ public static Parameter applyAnnotations(Swagger swagger, Parameter parameter, T
147148 }
148149 else {
149150 LOGGER .debug ("found inner property " + innerProperty );
151+ if (StringUtils .isNotEmpty (defaultValue )) {
152+ innerProperty .setDefault (defaultValue );
153+ }
150154 bp .setSchema (new ArrayModel ().items (innerProperty ));
151155
152156 // creation of ref property doesn't add model to definitions - do it now instead
@@ -182,6 +186,9 @@ public static Parameter applyAnnotations(Swagger swagger, Parameter parameter, T
182186 model .setType (prop .getType ());
183187 model .setFormat (prop .getFormat ());
184188 model .setDescription (prop .getDescription ());
189+ if (StringUtils .isNotEmpty (defaultValue )) {
190+ model .setDefaultValue (defaultValue );
191+ }
185192 bp .setSchema (model );
186193 }
187194 }
@@ -209,21 +216,26 @@ private static class AnnotationsHelper {
209216 private static final ApiParam DEFAULT_API_PARAM = getDefaultApiParam (null );
210217 private boolean context ;
211218 private ParamWrapper apiParam = new ApiParamWrapper (DEFAULT_API_PARAM );
219+ private String defaultValue ;
212220
213221 /**
214222 * Constructs an instance.
215223 * @param annotations array or parameter annotations
216224 */
217225 public AnnotationsHelper (List <Annotation > annotations ) {
226+ String rsDefault = null ;
218227 for (Annotation item : annotations ) {
219228 if (item instanceof Context ) {
220229 context = true ;
221230 } else if (item instanceof ApiParam ) {
222231 apiParam = new ApiParamWrapper ((ApiParam ) item );
223232 } else if (item instanceof ApiImplicitParam ) {
224233 apiParam = new ApiImplicitParamWrapper ((ApiImplicitParam ) item );
234+ } else if (item instanceof DefaultValue ) {
235+ rsDefault = ((DefaultValue ) item ).value ();
225236 }
226237 }
238+ defaultValue = StringUtils .isNotEmpty (apiParam .getDefaultValue ()) ? apiParam .getDefaultValue () : rsDefault ;
227239 }
228240
229241 /**
@@ -243,6 +255,14 @@ public ParamWrapper getApiParam() {
243255 return apiParam ;
244256 }
245257
258+ /**
259+ * Returns default value from annotation.
260+ * @return default value from annotation
261+ */
262+ public String getDefaultValue () {
263+ return defaultValue ;
264+ }
265+
246266 /**
247267 * Returns a default @{@link ApiParam} annotation for parameters without it.
248268 * @param annotationHolder a placeholder for default @{@link ApiParam}
0 commit comments