Search code examples
swaggeropenapiswagger-codegenopenapi-generatoropenapi-generator-maven-plugin

How to generate model classes only from Open Api in SpringBoot?


For example this is my open api

openapi: "3.0.0"
paths:
  /pets:
    get:
      summary: List all pets
      operationId: listPets
      tags:
        - pets
      parameters:
        - name: limit
          in: query
          ...
      responses:
        ...
    post:
      summary: Create a pet
      operationId: createPets
      ...
  /pets/{petId}:
    get:
      summary: Info for a specific pet
      operationId: showPetById
      ...
components:
  schemas:
    Pet:
      type: object
      required:
        - id
        - name
      properties:
        id:
          type: integer
          format: int64
        name:
          type: string
        tag:
          type: string
    Error:
      type: object
      required:
        - code
        - message
      properties:
        code:
          type: integer
          format: int32
        message:
          type: string

and I want to generate only model classes not apis and other things in the com.service.model package of src/main/java instead of target folder in springboot. What is the configuration needed in the pom.xml file?


Solution

  • I think that the generated sources should always be created in the target folder and not in your source code folder. Besides that, all the code (generated or your own) will eventually be on the same classpath.

    But if you insist of changing the default location (target/generated-sources) you can add something like this to your swagger-codegen configuration in your pom.xml: <output>src/main/java</output>

    To generate only the Model code you can add these tags to your codegen configuration:

    <plugin>
      <artifactId>swagger-codegen-maven-plugin</artifactId>
      <executions>
        <execution>
          <configuration>                            
            <generateApis>false</generateApis>
            <generateModelDocumentation>false</generateModelDocumentation>
            <generateSupportingFiles>false</generateSupportingFiles>
          </configuration>
        </execution>
      </executions>
    </plugin>