使用 LinkedHashMap 而不是 HashMap 作为 Swagger 字典类型 (Java codegen)?

Use LinkedHashMap instead of HashMap for Swagger dictionary type (Java codegen)?

我正在使用 openapi 3.0.2 和 codegen 插件:

<groupId>io.swagger.codegen.v3</groupId>
<artifactId>swagger-codegen-maven-plugin</artifactId>
<version>3.0.14</version>

我正在使用此处描述的 swagger dictionary/hashmap 类型:

https://swagger.io/docs/specification/data-models/dictionaries/

openapi: "3.0.2"
...
Labels:
    type: object
    additionalProperties:
        type: string
        minLength: 1
    description:  A description.

当我为此生成 Java 代码时,它被建模为扩展 HashMap:

的 class
@ApiModel(description = "A description.")
@Validated
@javax.annotation.Generated(value = "io.swagger.codegen.v3.generators.java.SpringCodegen", date = "2020-02-26T12:17:36.248Z[Europe/London]")
public class Labels extends HashMap<String, String>  {
...
}

有什么方法可以指示 swagger 使用 LinkedHashMap 而不是 HashMap? (无需从 codegen 中排除此 class 并手动修改它)。

我想在将字典返回给客户时控制字典中条目的顺序。

我使用了maven插件修改生成的文件:

<plugin>
        <groupId>org.codehaus.gmavenplus</groupId>
        <artifactId>gmavenplus-plugin</artifactId>
        <executions>
                <execution>
                        <id>modify-swagger</id>
                        <phase>generate-sources</phase>
                        <goals>
                                <goal>execute</goal>
                        </goals>
                        <configuration>
                                <properties>
                                        <property>
                                                <name>targetDir</name>
                                                <value>${project.build.directory}</value>
                                        </property>
                                </properties>
                                <scripts>
                                        <script><![CDATA[
                                                def labelsFile = new File(targetDir
                                                                + "/swagger-codegen/src/main/java/com/acme/models/Labels.java")
                                                def labelsFileContents = labelsFile.text
                                                if (labelsFileContents == null) {
                                                        throw Exception();
                                                }

                                                def newLabelsFileContents = labelsFileContents.replaceAll('HashMap', 'LinkedHashMap')
                                                labelsFile.write(newLabelsFileContents)
                                        ]]></script>
                                </scripts>
                        </configuration>
                </execution>
        </executions>
</plugin>

您可以使用 instantiationTypes 选项:

        <plugin>
            <groupId>io.swagger</groupId>
            <artifactId>swagger-codegen-maven-plugin</artifactId>
            <version>2.4.9</version>
            <configuration>
                <language>java</language>
            </configuration>
            <executions>
                <execution>
                    <id>generate-java</id>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                    <configuration>
                        <language>spring</language>
                        <instantiationTypes>map=java.util.LinkedHashMap</instantiationTypes>
                    </configuration>
                </execution>
            </executions>
        </plugin>