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.
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.
import 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 Page
in your yml file? –
Chemotaxis Page
–
Stearin org.springframework.web.servlet.mvc.method.annotation.SseEmitter
, but feels like it doesn't :-/ –
Surrejoinder 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>
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"
}
}
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"
© 2022 - 2024 — McMap. All rights reserved.