17

I am using Spring MVC (via Spring Boot) and have integrated 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'm using the ToStringSerializer for this field, it's returning listPrice.toString in the JSON, in other words:

{
    "listPrice": "USD 10.50"
}

However, the swagger documentation is not honoring the dataType = "java.lang.String". It shows the response model as:

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

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

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

nerdherd
  • 2,508
  • 2
  • 24
  • 40

3 Answers3

17

Turns out that dataType is completely ignored in the current version of the Swagger Spring MVC library. I found a short discussion on it here:

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

Looks like it could be included in version 2 once that is out.

EDIT: Although version 2 says it supports dataType, it doesn't appear to be working at this time. A better approach for my needs is to configure the documentation settings with a direct model substitution like this:

@Bean
public Docket swaggerSpringMvcPlugin() {
    return new Docket(DocumentationType.SWAGGER_2)
            .directModelSubstitute(Money.class, String.class);
}
nerdherd
  • 2,508
  • 2
  • 24
  • 40
  • 4
    You are correct that it's now supported in 2.0.x (available in 2.0.0-SNAPSHOT), with some caveats. The data type needs to be a valid package qualified class. – Dilip Krishnan Mar 31 '15 at 23:54
  • Thanks @DilipKrishnan. I see the documentation on github has been updated to reflect the change as well. – nerdherd Apr 01 '15 at 00:37
  • 1
    @DilipKrishnan Please put your comment as an answer or incorporate it into nerdherd's answer - it's a very important piece of information – Adam Michalik Feb 27 '20 at 15:56
3

For OpenApi (Swagger 3.0) and SpringDoc the following global configuration could be used.

static {
     SpringDocUtils.getConfig().replaceWithSchema(Money.class, new StringSchema());
}
vzhemevko
  • 815
  • 8
  • 25
-5
@ApiModel
@JsonInclude(JsonInclude.Include.NON_EMPTY)
public class Model {
    @JsonDeserialize(using = LocalDateTimeDeserializer.class)
    @JsonSerialize(using = LocalDateTimeSerializer.class)
    @JsonProperty("myDate")
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss")
    private final LocalDateTime myDateTime;

}
Yolo
  • 3
  • 2