Swagger Codegen use existing class
Asked Answered
A

4

16

How can I get the swagger codegen to use an existing class instead of creating a new class? Is this possible? For instance I want to use org.springframework.data.domain.Page instead of swagger creating another page class.

Apostrophe answered 18/6, 2017 at 5:40 Comment(0)
S
27

You could use --import-mappings, as it is explained here:

Sometimes you don't want a model generated. In this case, you can simply specify an import mapping to tell the codegen what not to create. When doing this, every location that references a specific model will refer back to your classes.

You call this on swagger-codegen-cli generate, with the example you included it would be

--import-mappings Page=org.springframework.data.domain.Page

Although importMappings hasn't been included in the general configuration parameters here if you look at the code here you can see it's a List<String>. I haven't used it with the maven plugin but looking at the doc and the code I'm guessing this should work:

<plugin>
    <groupId>io.swagger</groupId>
    <artifactId>swagger-codegen-maven-plugin</artifactId>
    <version>2.2.2-SNAPSHOT</version>
    <executions>
        <execution>
            ...
            <configuration>
                ...
                <importMappings>
                   <importMapping>Page=org.springframework.data.domain.Page</importMapping>
                </importMappings>
            </configuration>
        </execution>
    </executions>
</plugin> 

But this was recently changed, so it might be different if you're using an older version of the plugin. Before that changed it seems to be like this:

<plugin>
    <groupId>io.swagger</groupId>
    <artifactId>swagger-codegen-maven-plugin</artifactId>
    <version>2.2.2-SNAPSHOT</version>
    <executions>
        <execution>
            ...
            <configuration>
                ...
                <configOptions>
                   <import-mappings>Page=org.springframework.data.domain.Page;Some=org.example.Some</import-mappings>
                </configOptions>
            </configuration>
        </execution>
    </executions>
</plugin>

According to the comment in that commit the old version should be supported too, but I haven't tried any of this so let me know if it works.

Stearin answered 18/6, 2017 at 20:20 Comment(7)
Ahh thank you. Do you know how I configure this with the swagger code-gen maven plugin?Apostrophe
I have updated how to use it based on the code, I haven't tried it, so it might not work. But at least if gives you an idea of where to look for it.Stearin
Thanks for that yeah I got to that stage in my Pom, I find that swagger is a bit dumb it just doesn't create a class for Page and it misses theimport org.springframework.data.domain.Page so I have to edit template files manually anyway. You also can't change the name of your class so I cant have a Page for resource A and resource B is the same swagger file. haha, man I am going to submit an issue on github haha.Apostrophe
In that case add a link to the issue in the question just in case someone is looking for thisStearin
How would you make reference of Page in your yml file?Chemotaxis
@Chemotaxis just as if it was a common component of type object named PageStearin
Thanks, I was hoping it worked with org.springframework.web.servlet.mvc.method.annotation.SseEmitter, but feels like it doesn't :-/Surrejoinder
A
20

None of the answers mentioned here talked about what to add to the swagger yaml file, in case someone is interested this is something that worked for me:

DisplayProperty:  
  type: object
  properties:
    name:
      type: string
    displayName:
      $ref: '#/components/schemas/Text'
    isRequired:
      type: boolean
      example: false
Text:
    type: object

and then have this in the pom

<importMappings>
<importMapping>Text=com.--.--.--.--.Text</importMapping>

Artois answered 16/11, 2018 at 19:38 Comment(1)
Seems like you missed the following yaml declarations before 'Text' in your example : components: schemas:Eldreda
P
8

It's not always possible to use --import-mappings if you have long list of mappings. (At least in case of Windows, which has size-limit for string in command prompt.) That is why better way do it: use mapping with swagger configuration file. (And this option is not fully documented.)

Like that:

java -jar swagger-codegen-cli-2.3.1.jar generate -i myspec.yaml -l java -c myconfig.json

myconfig.json:

{
  "hideGenerationTimestamp": true,
  "dateLibrary": "java8",
  "useRuntimeException": true,
  "modelPackage": "org.my.package.model",
  "apiPackage": "org.my.package.api",
  "importMappings": {
    "Page": "org.springframework.data.domain.Page",
    "MySuperType": "org.my.SuperType"
  }
}
Poniard answered 22/5, 2018 at 7:0 Comment(0)
B
1

Let me add that for the java generator you also have to specify an additional property ignoreImportMappings=false. For some reason, the default is to ignore the import mappings with that generator. See this issue report.

Here is the configuration which works for me with the swagger-codegen-maven-plugin:

<plugin>
  <groupId>io.swagger.codegen.v3</groupId>
  <artifactId>swagger-codegen-maven-plugin</artifactId>
  <version>3.0.45</version>
  <executions>
    <execution>
      ...
      <configuration>
        <language>java</language>
        <additionalProperties>
          <additionalProperty>ignoreImportMappings=false</additionalProperty>
        </additionalProperties>
        <importMappings>
          <importMapping>MyClass=com.blah.blah.MyClass</importMapping>
        </importMappings>
        ...

Refer to it in the yaml file as usual:

$ref: "#/components/schemas/MyClass"
Birth answered 24/6, 2023 at 17:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.