How to generate OpenAPI 3.0 YAML file from existing Spring REST API?

19,289

I was missing some library for this for longer time. Finally, decided to implement my own generator https://github.com/jrcodeza/spring-openapi maybe you can check it out too. It's based on reflection and supports javax and spring annotations. It also generates inheritance model (with discriminators) based on Jackson annotations. Besides you can define your own interceptors if you want to alter generation process (e.g. when you have your own annotations and need to adjust generated sections of schema). You can use it in runtime mode or as a maven plugin. There is also OpenAPI3 to java client generator, which generates the model. Again it generates also Javax annotations and Jackson annotations for correct inheritance.

Share:
19,289
Dev Utkarsh
Author by

Dev Utkarsh

Music, Technology, and Travelling.

Updated on June 03, 2022

Comments

  • Dev Utkarsh
    Dev Utkarsh almost 2 years

    I have an existing Spring REST API for which I want to generate the OpenAPI 3.0 YAML file and not Swagger 2.0 JSON/YAML?

    Since as of now, SpringFox does not support YAML generation. It generates JSON with Swagger 2.0 (which follows OPEN API 3.0 spec).

    Also, there is https://github.com/openapi-tools/swagger-maven-plugin but it does not seem to support Spring Rest.

    I tried the Kongchen spring-maven-plugin which is able to generate the YAML file but with Swagger 2.0 definition and not OPEN API 3.0 like :

    swagger: "2.0"
    info:
      description: "Test rest project"
      version: "1.0"
      title: "Some desc"
      termsOfService: "http://swagger.io/terms/"
      contact:
        name: "Rest Support"
        url: "http://www.swagger.io/support"
        email: "[email protected]"
      license:
        name: "Apache 2.0"
        url: "http://www.apache.org/licenses/LICENSE-2.0.html"
    host: "example.com"
    basePath: "/api/"
    

    So my question is how can I generate the OPEN API YAML file like :

    openapi: 3.0.0
    info:
      description: Some desc
      version: "1.0"
      title: Test rest project
      termsOfService: http://swagger.io/terms/
      contact:
        name: Rest Support
        url: http://www.swagger.io/support
        email: [email protected]
      license:
        name: Apache 2.0
        url: http://www.apache.org/licenses/LICENSE-2.0.html
    

    I am currently using swagger-maven-plugin to generate YAML file with Swagger 2.0 definition and converting it to Open API 3.0 definition using swagger2openapi at https://mermade.org.uk/openapi-converter

    Question 1:
    Can spring-maven-plugin capture io.swagger.v3.oas.annotations to generate the YAML ?

    Question 2:
    What is the best way to generate the YAML with OPEN API definitions in a Spring MVC Project?

    Question 3:
    Can io.swagger.v3.oas be used with Spring projects or it is only for JAX-RS projects?