Search code examples
javaspringgradleopenapi-generator

Is it possible to set specific package name for generated openApi spec file


In our project (java/spring/gradle stack), we are using openApi specs. We have a couple of services and specs for them. Also, we have common modules that duplicate in each spec. We moved those common modules to a separate spec file and include them in others. For instance:

specific_project1:
   openapi:
      spec.yaml
      common_modules.yaml
   build.gradle

The spec.yaml has the next code:

openapi: 3.0.0
info:
  version: 0.0.1
paths:
  /specific/post:
    post:
      requestBody:
        context:
          application/json:
             schema:
               $ref: "common_modules.yaml#/components/schemas/TestModule"

and the common_modules.yaml the next:

openapi: 3.0.0
info:
  version: 0.0.1
components:
  schemas:
    TestModule:
      type: object
      properties:
        value:
          type: string

As the result, we need that spec.yaml to be generated with package name com.specific.project1, common_modules.yaml is generated with package name com.common.modules and the generated java class that is generated from spec.yaml has an import for TestModule with package com.common.modules in imports.


Solution

  • We found the solution on how to refer to the same object. We published TestModule as a separate project with its own package and in other projects, we used the importMappings configuration to define the proper package name for the object.

    importMappings = [
       TestModule: "com.common.modules.TestModule"
    ]