java - 使用 LinkedHashMap 而不是 HashMap 用于 Swagger 字典类型(Java codegen)?

标签 java swagger openapi swagger-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? (无需从 codegen 中排除此类并手动修改它)。

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

最佳答案

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

关于java - 使用 LinkedHashMap 而不是 HashMap 用于 Swagger 字典类型(Java codegen)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60414026/

相关文章:

python - 如何在名称中带有连字符的 FastApi-SwaggerUI 中键入变量?

Java Spring Rest 和 Swagger

java - 如何在不创建对象的情况下将java类转换为json格式结构

java - OpenApi -API,使用 X-WWW-FORM-URLEncoded 作为 requestBody

enums - openApi 模式中不区分大小写的字符串参数

java - 让 BiConsumer 接受对象

java - 使用 Java 应用程序向我的手机发送消息

java - 为什么我的代码在java中输出package.class@letternumbers?

java - Spring Boot WebMvcTest 对于 GET 方法返回 302 而不是 401

swagger-ui - 通过 Swagger UI 查看时如何将 FileUpload 响应正文视为上传按钮