How to set @ApiModelProperty dataType to String for Swagger documentation

I use Spring MVC (via Spring Boot) and integrated the Swagger API documentation using the swagger-spring -mvc library.

I have a class that looks something like this:

@ApiModel public class CartItem { ... private Money listPrice; // joda money class @JsonSerialize(using = ToStringSerializer.class) @ApiModelProperty(required = true, dataType = "java.lang.String") public Money getListPrice() { return listPrice; } ... } 

Since I use ToStringSerializer for this field, it returns listPrice.toString in JSON, in other words:

 { "listPrice": "USD 10.50" } 

However, the swagger documentation does not honor dataType = "java.lang.String". It displays the response model as:

 "CartItem": { "description": "", "id": "CartItem", "properties": { "listPrice": { "required": false, "type": "Money" } } } 

I tried putting the @ApiModelProperty annotation in the field as well as the method, and in both cases the required field is respected, but the dataType field dataType ignored. I also tried using "String", "string" and "java.lang.String" for dataType, but none of them worked.

Am I missing something, or is it just a bug in the swagger-spring-mvc library?

+6
source share
1 answer

It turns out that dataType completely ignored in the current version of the Swagger Spring MVC library. I found a short discussion here:

https://github.com/springfox/springfox/issues/602

It looks like it may be included in version 2 after it comes out.

EDIT: although version 2 says it supports dataType, it doesn't seem to be working at this time. The best approach for my needs is to adjust the documentation parameters using direct model substitution as follows:

 @Bean public Docket swaggerSpringMvcPlugin() { return new Docket(DocumentationType.SWAGGER_2) .directModelSubstitute(Money.class, String.class); } 
+4
source

Source: https://habr.com/ru/post/984460/


All Articles