springdoc / springdoc-openapi

Library for OpenAPI 3 with spring-boot
https://springdoc.org
Apache License 2.0
3.29k stars 499 forks source link

Allows to customize serialization #953

Closed EstebanDugueperoux2 closed 3 years ago

EstebanDugueperoux2 commented 3 years ago

In org.springdoc.webmvc.api.OpenApiResource we can see 2 operations exposing OpenAPI in different serialization, JSON and YAML. Would it be possible to have a single operation using HTTP content negotiation? I ask this because we use Zalando problem details [1] to follow rfc7807 [2] and in our resources we provide @Example through Example.value:Object. In this Example.value depending of examples, we provide a instance of org.zalando.problem.DefaultProblem or org.zalando.problem.violations.ConstraintViolationProblem which contains technical fields that I don't want to be serialized.

springdoc-openapi being Spring Boot aware, I could use @JsonComponent but here it doesn't works because serialization is done statically "Json.mapper().writeValueAsString(openAPI)" and not using Spring @Bean.

Are you ok to do the little refactoring?

[1] https://github.com/zalando/problem [2] https://tools.ietf.org/html/rfc7807

Best Regards.

bnasslahsen commented 3 years ago

Hi @EstebanDugueperoux2,

You can customize serialization, by just registering your modules.

    Json.mapper().registerModule(new ProblemModule())
    .registerModule(new ConstraintViolationProblemModule());

Not that the Json/YAML endpoints are different. You are asking for a breaking change, that may cause huge impacts.

If you want change the implementation of OpenApiResource, you can simply extend org.springdoc.webmvc.api.OpenApiResource, and create your own bean. This will help you get much finer control.

EstebanDugueperoux2 commented 3 years ago

Ok, and how customize YAML serialization?

bnasslahsen commented 3 years ago

Same principles:

Yaml.mapper().registerModule(new ProblemModule())
    .registerModule(new ConstraintViolationProblemModule());