Search code examples
openapiopenapi-generatorcode-generationswagger-3.0

How to generate model class for object with additionalProperties using OpenAPI code generator


I am using additionalPropertes in my OpenAPI definition to refer a map object. The part of my OAS is given below:

Configuration:
  title: Configuration Info
  type: object
  additionalProperties:
    type: string
  description: The config parameters.
  example:
    configName: header
    configValue: Context
    configId: "12"

When generating code using maven codegen plugin of openapi-generator, version - 4.3.1 with the following configurations, no model class is generated for the model Configuration.

           <plugin>
                <groupId>org.openapitools</groupId>
                <artifactId>openapi-generator-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                            <inputSpec>api.yaml</inputSpec>
                            <language>jaxrs-cxf-cdi</language>
                            <configOptions>
                                <apiPackage>${api-package}</apiPackage>
                                <modelPackage>${model-package}</modelPackage>
                                <sourceFolder>src/gen/java</sourceFolder>
                            </configOptions>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

How can we generate model classes for objects with additionalProperties in OAS?


Solution

  • Setting the configuration generateAliasAsModel as true will generate model classes for Maps defined in OAS. The correct configuration is given below:

                <plugin>
                    <groupId>org.openapitools</groupId>
                    <artifactId>openapi-generator-maven-plugin</artifactId>
                    <executions>
                        <execution>
                            <goals>
                                <goal>generate</goal>
                            </goals>
                            <configuration>
                                <inputSpec>api.yaml</inputSpec>
                                <language>jaxrs-cxf-cdi</language>
                                <configOptions>
                                    <apiPackage>${api-package}</apiPackage>
                                    <modelPackage>${model-package}</modelPackage>
                                    <sourceFolder>src/gen/java</sourceFolder>
                                </configOptions>
                                <generateAliasAsModel>true</generateAliasAsModel>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>