Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

swagger @ApiModelProperty example value for List<String> property

I have one class in which there is one property which is List<String>

public class MyClass {     ....     @ApiModelProperty(position = 2)     private List<String> productIdentifiers;     .... } 

This code generates the example values as following:

{   "customerId": "1001",   "productIdentifiers": [     "string"   ],   "statuses": [     "NEW"   ] } 

The example values here shown are not valid. My expected example values should be like :

{   "customerId": "1001",   "productIdentifiers": [     "PRD1",     "PRD2",     "PRD3"   ],   "statuses": [     "NEW"   ] } 

I have tried passing example attribute as following but it is not generating proper value:

@ApiModelProperty(position = 2, example = "PRD1, PRD2, PRD3") // This generates -> "productIdentifiers": "PRD1, PRD2, PRD3" // Its not json array  @ApiModelProperty(position = 2, example = "[\"PRD1\", \"PRD2\", \"PRD3\"]") // This generates -> "productIdentifiers": "[\"PRD1\", \"PRD2\", \"PRD3\"]" // Its too not json array 

Is there any way I can generate proper example value for List property ?

Update :

I have tried the solutions suggested by @nullpointer and @Zeeshan Arif

@ApiModelProperty(position = 2, dataType="List", example = "PRD1, PRD2, PRD3") private List<String> productIdentifiers; //This generates -> `"productIdentifiers": "PRD1, PRD2, PRD3"` 

Update 2 :

Tried following approach which did not generate proper response

@ApiModelProperty(position = 2, dataType="java.util.List<String>", example = "PRD1, PRD2, PRD3") // This generates -> "productIdentifiers": "PRD1, PRD2, PRD3"   @ApiModelProperty(position = 2, dataType="String[]", example = "PRD1, PRD2, PRD3") // This generates -> "productIdentifiers": "PRD1, PRD2, PRD3" 

my maven dependency for swagger jar is :

<dependency>     <groupId>io.springfox</groupId>     <artifactId>springfox-swagger2</artifactId>     <version>2.5.0</version>     <exclusions>         <exclusion>             <artifactId>mapstruct</artifactId>             <groupId>org.mapstruct</groupId>         </exclusion>     </exclusions> </dependency> 

Update github ticket for this issue

like image 464
Anil Bharadia Avatar asked Dec 06 '16 06:12

Anil Bharadia


1 Answers

I managed to get this to work, generating a List of Strings.

Within the ApiModelProperty with springfox 2, write your example as follows:

example = "[\"AddLine1\",\"AddLine2\",\"AddLine3\",\"AddLine4\"]" 

Here is my example:

@ApiModelProperty(value = "Address", name = "addLines",      example = "[\"AddLine1\",\"AddLine2\",\"AddLine3\",\"AddLine4\"]") 

When I render the swagger page, I get the following output:

"addLines": [       "AddLine1",       "AddLine2",       "AddLine3",       "AddLine4"     ], 
like image 180
dane_griffiths Avatar answered Sep 19 '22 14:09

dane_griffiths