简体   繁体   中英

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

I'm using openapi 3.0.2, and the codegen plugin:

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

I'm using the swagger dictionary/hashmap type described here:

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

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

When I generate Java code for this, it is modelled as a class which extends 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>  {
...
}

Is there any way to instruct swagger to use a LinkedHashMap instead of a HashMap ? (without having to exclude this class from codegen and manually modify it).

I would like to control the ordering of the entries in this dictionary when returning it to clients.

I used a maven plugin to modify the generated file:

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

You can use instantiationTypes option:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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