Is there any way to include classes that are not used in controller with Swagger in Spring?

1k views Asked by At

I'm using swagger-maven-plugin(kongchen) to generate static document and I would like to generate yaml like this:

swagger: "2.0"
info:
  version: "1.0.0"
  title: "Swagger example"
paths:
  /api/students:
    post:
      operationId: "addStudent"
      parameters:
      - in: "body"
        name: "body"
        required: false
        schema:
          $ref: "#/definitions/Student"
      responses:
        200:
          description: "successful operation"
          schema:
            type: "boolean"
definitions:
  Student:
    type: "object"
    properties:
      id:
        type: "integer"
        format: "int32"
        minimum: 1
        maximum: 20
      name:
        type: "string"
      surname:
        type: "string"

But i would also want plugin to include classes that are not defined in my controller.

my plugin setup:

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>com.github.kongchen</groupId>
                <artifactId>swagger-maven-plugin</artifactId>
                <version>3.1.8</version>
                <configuration>
                    <apiSources>
                        <apiSource>
                            <springmvc>true</springmvc>
                            <locations>
                                <location>
                                    mypackage
                                </location>
                            </locations>
                            <info>
                                <title>
                                    Swagger example
                                </title>
                                <version>
                                    1.0.0
                                </version>
                            </info>
                            <outputFormats>json,yaml</outputFormats>
                            <swaggerDirectory>generated</swaggerDirectory>
                            <swaggerApiReader>com.github.kongchen.swagger.docgen.reader.SpringMvcApiReader</swaggerApiReader>
                        </apiSource>
                    </apiSources>
                </configuration>
                <executions>
                    <execution>
                        <phase>compile</phase>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                </executions>
                <dependencies>
                    <!-- Adding dependency to swagger-hibernate-validations to enable the BeanValidator as a custom
                         model converter -->
                    <dependency>
                        <groupId>io.swagger</groupId>
                        <artifactId>swagger-hibernate-validations</artifactId>
                        <version>1.5.6</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>

Is there any way to achieve this goal using Swagger? The main goal of it is to have yaml that i can import to Apicurio and use in my applications.

Or maybe is there any way to generate yaml that includes all classes like this without using it in any controller?

 @ApiModel
    public class Student {
        @Min(1)
        @Max(20)
        @ApiModelProperty
        private int id;
        @ApiModelProperty
        private String name;}
2

There are 2 answers

1
Salil Kothadia On

Do you mean to include the models, request / response stubs? If so you can use Swagger annotations

Method Level:

@ApiOperation(value = "Get Details", response = ResponseClassStub, produces = MediaType.APPLICATION_JSON_VALUE)
0
James James On

It is possible with micronaut.openapi to include yml files. For example, if you have an openapi schema in openapi/mySchema.yml, you can import it to your schema by putting the following line in openapi.properties:

micronaut.openapi.additional.files=openapi

Unfortunately this approach doesn't work if you need to use the class. You can have a class defined separately but you need to keep it in sync with mySchema.yml manually.