swagger-api / swagger-api/swagger-codegen
[JAVA] swagger-codegen-maven-plugin : wrong generation with discriminator
Nobody has claimed this yet.
- Dominant language
- Mustache
- Stars
- 17.8k
- Forks
- 6k
- PR merge metrics
- No merged PRs in 30d
Description
Description
The code generation seems to be incorrect
I have 2 problems with swagger-codegen-maven-plugin on java and the library resttemplate.
I use the sample describe here https://swagger.io/docs/specification/data-models/oneof-anyof-allof-not/ with Pet, Dog inherited from Pet, Cat inherited from Pet.
use case 1
I created a schema for Person which have a pet (a Dog or a Cat).
I specified a discriminator.propertyName : pet_type, but on the generated interface, i have :
@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.PROPERTY,
property = "type")
@JsonSubTypes({
@JsonSubTypes.Type(value = Cat.class, name = "Cat"),
@JsonSubTypes.Type(value = Dog.class, name = "Dog")
})
public interface OneOfPersonPet {
}
The property should be pet_type (not type).
Each time I try to call my endPoint, the objectMapper failed because of the required property type
use case 2
The other problem is that I have an endPoint /pets with take a Cat (Pet) or a Dog (Pet) into the body.
After generation, the DefaultApi contains the method :
public void petsPatch(PetsBody body) throws RestClientException {
petsPatchWithHttpInfo(body);
}
where PetsBody is a class whish is not related to a Dog, Cat or Pet. Just the same initerface OneOfpetsBody.
@javax.annotation.Generated(value = "io.swagger.codegen.v3.generators.java.JavaClientCodegen", date = "2022-02-24T22:04:37.021078+01:00[Europe/Paris]")@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "petType", visible = true )
@JsonSubTypes({
})
public class PetsBody implements OneOfpetsBody {
@Override
public boolean equals(java.lang.Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
return true;
}
@Override
public int hashCode() {
return Objects.hash();
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("class PetsBody {\n");
sb.append("}");
return sb.toString();
}
/**
* Convert the given object to string with each line indented by 4 spaces
* (except the first line).
*/
private String toIndentedString(java.lang.Object o) {
if (o == null) {
return "null";
}
return o.toString().replace("\n", "\n ");
}
}
How can I create a cat and use the method petsPatch(PetsBody body) ? I miss something ?
Swagger-codegen version
swagger-codegen-maven-plugin version 3.0.33
Swagger declaration file content or url
openapi: "3.0.3"
info:
title: "pet"
version: "1.0"
tags:
- name: pet
servers:
- url: /rest/v1
paths:
/pets:
patch:
requestBody:
content:
application/json:
schema:
oneOf:
- $ref: '#/components/schemas/Cat'
- $ref: '#/components/schemas/Dog'
discriminator:
propertyName: pet_type
responses:
'200':
description: Updated
components:
schemas:
Pet:
type: object
required:
- pet_type
properties:
pet_type:
type: string
discriminator:
propertyName: pet_type
Dog:
allOf:
- $ref: '#/components/schemas/Pet'
- type: object
properties:
bark:
type: boolean
breed:
type: string
enum: [Dingo, Husky, Retriever, Shepherd]
Cat:
allOf:
- $ref: '#/components/schemas/Pet'
- type: object
properties:
hunts:
type: boolean
age:
type: integer
Person:
properties:
name:
type : string
pet:
oneOf:
- $ref: '#/components/schemas/Cat'
- $ref: '#/components/schemas/Dog'
discriminator:
propertyName: pet_type
Command line used for generation
java 11
mvn clean install
pom.xml
<plugin>
<groupId>io.swagger.codegen.v3</groupId>
<artifactId>swagger-codegen-maven-plugin</artifactId>
<version>3.0.33</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>${project.basedir}/src/main/resources/folder/file-v1.yaml</inputSpec>
<language>java</language>
<library>resttemplate</library>
<apiPackage>blabla.api</apiPackage>
<modelPackage>blabla.model</modelPackage>
<invokerPackage>blabla.handler</invokerPackage>
<generateApiTests>false</generateApiTests>
<generateApiDocumentation>false</generateApiDocumentation>
<generateModelTests>false</generateModelTests>
<generateModelDocumentation>false</generateModelDocumentation>
<generateSupportingFiles>true</generateSupportingFiles>
<configOptions>
<interfaceOnly>true</interfaceOnly>
<dateLibrary>java8</dateLibrary>
</configOptions>
</configuration>
</execution>
</executions>
</plugin>
Regards
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the supplied OpenAPI declaration and Maven configuration, then inspect the Java generator output for OneOfPersonPet, PetsBody, and DefaultApi. Compare the generated discriminator property and oneOf request types with the schema; done means pet_type is preserved and the /pets request can represent Cat or Dog correctly.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, openapi
- Domain
- api, backend-api-design
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100