Search code examples
swagger-codegenopenapi-generator

OpenApi Generator reference an external POJO in YAML file specification


I'm using OpenApi v3.3.4 (formerly called Swagger CodeGen) maven plugin to generate my rest controller via api.yml files where I describe all the operations I want to expose.

In my use case, I want to expose a method POST: handleNotification(@RequestBody SignatureNotification notification) which its request body's type is generated via another maven-plugin in /targer folder.

Actually I'm defining SignatureNotification in Componentspart of my .yml file:

...
requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/SignatureNotification'
...

Which gets generated by OpenApi plugin and then I map it to SignatureNotification that already exists and has the same attributes.

I'm not very satisfied with this solution so I want to know if there is a way to tell OpenApi Generator to use an external object as a reference?


Solution

  • If I understand your needs correctly, you just want to tell generator not to generate again your existing class. If above is correct, then you can configure plugin importMappings like this:

    <plugin>
      <groupId>org.openapitools</groupId>
      <artifactId>openapi-generator-maven-plugin</artifactId>
      <version>${openapi-generator-maven-plugin.version}</version>
      <configuration>
          ... excluded for simplicity
          <importMappings>
              <importMapping>SignatureNotification=path.to.your.SignatureNotification</importMapping>        
          </importMappings>
      </configuration>
    </plugin>
    

    With this config, openapi generator won't generate class from SignatureNotification definition, instead it will use existing one.