如何使用 OpenAPI 代码生成器为具有附加属性的对象生成模型 class

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

我在我的 OpenAPI 定义中使用 additionalPropertes 来引用地图对象。我的OAS部分如下:

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

当使用 openapi-generator 的 maven codegen 插件生成代码时,版本 - 4.3.1 具有以下配置,没有生成模型 class 型号 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>

我们如何为 OAS 中具有 additionalProperties 的对象生成模型 classes?

将配置 generateAliasAsModel 设置为 true 将为 OAS 中定义的地图生成模型 类。正确配置如下:

            <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>