gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 17:30:16 28 4
gpt4 key购买 nike

我正在使用 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com