Forum Discussion

igiguere's avatar
2 months ago

Avoid extra line breaks in yaml generated from annotated code

When starting from the OpenApi document (json or yaml), it is possible to define minimum and maximum string length: https://swagger.io/docs/specification/v3_0/data-models/data-types/

Is there an equivalent setting that can be used when generating the OpenApi document from code annotations ? Maybe properties to be set in a Spring Boot application.properties, perhaps ?

See also on GitHub : https://github.com/swagger-api/swagger-core/issues/4846

1 Reply

  • Hi igiguere 

    In OpenAPI documents generated from annotated code in Spring Boot, defining minimum and maximum string length is possible, but the behavior depends on the OpenAPI library used.

    • When using SpringDoc OpenAPI, the @Size(min, max) annotation is usually recognized and automatically adds minLength and maxLength to the OpenAPI specification.
    • For instance, annotating a username field with @Size(min = 5, max = 15) should result in the generated OpenAPI YAML or JSON including minLength: 5 and maxLength: 15.
    • Though, if @Size is not reflected in the OpenAPI output, manual enforcement is possible using a SpringDoc OpenAPI customizer.
    • This involves implementing a customizer to explicitly set minLength and maxLength for the desired schema.

    Especially, this can be achieved through annotations or customization, eliminating the need for Spring Boot application.properties settings.

    Hope this helps - Happy to help further!!
    Thank you very much and have a great one!
    Warm regards