springdoc / springdoc-openapi-demos

Demo for OpenAPI 3 with spring-boot
http://springdoc.org
Apache License 2.0
493 stars 267 forks source link

How to use serializers? #31

Closed bvn13 closed 3 years ago

bvn13 commented 3 years ago

Hi!

Is there any way to make this library to apply a serialization for any field of DTO?

Consider I have a DTO:

@lombok.Data
class MoneyDto {

   BigDecimal amount;
   Currency currency;

}

How to make this library to serialize currency as String? I can set up an ObjectMapper to do it. But it will not be reflected in generated Swagger Docs.

bvn13 commented 3 years ago

Looks like I havefound a solution.

@Schema(description = "Money")
@lombok.Data
public class MoneyDto {

    @Schema(description = "Amount of money")
    @NonNull
    BigDecimal amount;

    @Schema(description = "Currency of money", type = "string")
    @JsonSerialize(using = JsonCurrencySerializer.class)
    @NonNull
    Currency currency;

}
bvn13 commented 3 years ago

This issue is solved.