繁体   English   中英

Swagger 字典类型(Java 代码生成)使用 LinkedHashMap 而不是 HashMap?

[英]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 字典/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的类:

@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吗? (无需从代码生成中排除此类并手动修改它)。

我想在返回给客户时控制这本词典中条目的顺序。

我使用了一个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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM