Bug Report Checklist
Description
When generating a model using scala-sttp, oneOf in the OpenAPI generates case classes that can never match any actual valid json.
I also tried some of the other scala generators and they seem to have the same problem.
openapi-generator version
Latest master
OpenAPI declaration file content or url
Link to OpenAPI yaml gist
Generation Details
java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate -i /tmp/test/example.yaml -g scala-sttp -o /tmp/test/out
Steps to reproduce
If you look in the model package you will see that the specific animal types (Dog, Cat) are all perfectly fine.
However, the top-level Example case class has a field of type Animal:
case class Example(
requestId: Option[String] = None,
animal: Option[Animal] = None
)
And here is the generated Animal case class:
case class Animal(
dogName: String,
foodType: Option[CapybaraFoodType] = None,
catId: Int,
capyBaraId: Option[Int] = None
)
There are two problems with this:
dogName and catId are only required in Dog and Cat respectively. This means that it is impossible to deserialize an Example containing a Dog unless you provide a catId for the Dog.
- the
foodType is set to CapybaraFoodType even though Dog and Cat each have their own FoodType schema. An Example containing a Cat or Dog with a CatFoodType or DogFoodType cannot be modelled at all, even though it's valid according to the spec.
Suggest a fix
The most straightforward fix would be to make Animal an empty trait, and have Cat, Dog and Capybara be subtypes of this trait. The Example then contains the Animal. Then, any (de)serializer could use the discriminator in the yaml to make an instance of the correct subtype.
Note that a trait is better than an abstract class in this case, because the OpenAPI spec allows an object to be reused among multiple oneOf statements for different objects, and a case class can extend multiple traits but only one abstract class.
I realize there might be more work to get the compatible serializer libs to work correctly with this new change, but since this case currently isn't supported at all. I would be very happy with just being able to generate a valid model as a first step.
Bug Report Checklist
Description
When generating a model using
scala-sttp,oneOfin the OpenAPI generates case classes that can never match any actual valid json.I also tried some of the other scala generators and they seem to have the same problem.
openapi-generator version
Latest master
OpenAPI declaration file content or url
Link to OpenAPI yaml gist
Generation Details
Steps to reproduce
If you look in the model package you will see that the specific animal types (
Dog,Cat) are all perfectly fine.However, the top-level Example case class has a field of type
Animal:And here is the generated Animal case class:
There are two problems with this:
dogNameandcatIdare only required inDogandCatrespectively. This means that it is impossible to deserialize anExamplecontaining aDogunless you provide acatIdfor theDog.foodTypeis set toCapybaraFoodTypeeven thoughDogandCateach have their own FoodType schema. AnExamplecontaining aCatorDogwith aCatFoodTypeorDogFoodTypecannot be modelled at all, even though it's valid according to the spec.Suggest a fix
The most straightforward fix would be to make
Animalan empty trait, and haveCat,DogandCapybarabe subtypes of this trait. TheExamplethen contains theAnimal. Then, any (de)serializer could use thediscriminatorin the yaml to make an instance of the correct subtype.Note that a trait is better than an abstract class in this case, because the OpenAPI spec allows an object to be reused among multiple oneOf statements for different objects, and a case class can extend multiple traits but only one abstract class.
I realize there might be more work to get the compatible serializer libs to work correctly with this new change, but since this case currently isn't supported at all. I would be very happy with just being able to generate a valid model as a first step.