-
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve handling of oneOf #15
Comments
What should DefaultCodegen expose? Currently DefaultCodegen simply exposes a simple string Line 2165 in 23ad9f3
changing returnType would probably break a lot of templates, so maybe there is a way for a lang to opt-in to supporting `oneOf`` just some thoughts, no real preferences on this atm |
Well I think that in Java, an interface should be created, because you have no way to express a Type Union: return type is Yes we need to add this to the Codegen without breaking anything for existing templates. |
For Java, what would the interface look like for |
We have problem with names when schema are inlined (this should be addressed in #8). The interface can be empty in my opinion. The idea is that in Java code, you need to check with
In typescript you do not need About the discriminator ( |
ah, yes, an empty/marker interface + casting would work. The old-school way of doing tagged unions in langs that don't support them is to expose a struct with a discriminator/enum that identifies which field contains the data e.g.
which is more static in that it avoids casting, but has downside of having to use the discriminator to get the right data. But I'm not involved enough with Java to know what the best practices for Java are. The marker interface method may be a cleaner solution. Also worth noting other tools like c# autorest I don't think support this currently. |
I think we should published the necessary information in the Codegen layer, each template can implement in its own way (depending on the capabilities / language features) |
My company requires oneOf functionality. |
Is there a bugfix at the actual Version? I am using Version 3.3.2 and i have the same problem with oneOf. |
I think I will give the marker-interface approach a try in the Java-client generators: For those schemas: components:
schemas:
MainObj:
type: object
oneOf:
- $ref: '#/components/schemas/ObjA'
- $ref: '#/components/schemas/ObjB'
discriminator:
propertyName: realtype
mapping:
a-type: '#/components/schemas/ObjA'
b-type: '#/components/schemas/ObjB'
ObjA:
type: object
properties:
realtype:
type: string
message:
type: string
ObjB:
type: object
properties:
realtype:
type: string
description:
type: string
code:
type: integer
format: int32 I would generate:
|
@jmini sounds good to me 👍 |
Interesting case in oneOf.yaml: fruit:
title: fruit
type: object
properties:
color:
type: string
oneOf:
- $ref: '#/components/schemas/apple'
- $ref: '#/components/schemas/banana' It seems to be possible to add The interface pattern that I have described here, only works for Schema with only |
Do the OpenApi-Generator support OneOf / Any-Of combinations? |
What is the status of "oneOf" issues ? |
See also #2121 for Python Client support |
I did it for Spring and all JaxRS server generators in my fork. See mmalygin@3303da5 |
@mmalygin this relates/overlaps with #5381. Do you plan on creating a PR for this work? @bkabrda @jimschubert - Do you have a view of which implementation best aligns with the merged changes from #4785? I think it is imperative that we have alignment across all flavors of clients and server generators. |
I have the same problem when generating code from a spec containing oneOf, for QT5/C++. A file "OneOf..." header file is included, but not generated anywhere (at least with version 4.3.0). |
As @amitinfo2k. I also tried the go generator with the Video Analytics Serving OpenAPI It uses oneOf as well properties:
source:
discriminator:
propertyName: type
oneOf:
- $ref: '#/components/schemas/URISource'
- $ref: '#/components/schemas/DeviceSource'
type: object
destination:
discriminator:
propertyName: type
oneOf:
- $ref: '#/components/schemas/KafkaDestination'
- $ref: '#/components/schemas/MQTTDestination'
- $ref: '#/components/schemas/FileDestination'
type: object I used the docker generator for it: $ docker images | grep openapi-generator-cli
openapitools/openapi-generator-cli latest c03abe67cb2d 3 hours ago 135MB
$ docker run --rm -v ${PWD}:/local openapitools/openapi-generator-cli generate -i https://raw.githubusercontent.com/intel/video-analytics-serving/v0.3.0-alpha/vaserving/rest_api/video-analytics-serving.yaml -g go -o /local/out/go Then using it in a client throws errors: $ go run client.go
# openapi
../../go/src/openapi/model_pipeline_request.go:13:9: undefined: OneOfUriSourceDeviceSource
../../go/src/openapi/model_pipeline_request.go:14:14: undefined: OneOfKafkaDestinationMqttDestinationFileDestination The generated go code look like this: package openapi
// PipelineRequest struct for PipelineRequest
type PipelineRequest struct {
Source OneOfUriSourceDeviceSource `json:"source,omitempty"`
Destination OneOfKafkaDestinationMqttDestinationFileDestination `json:"destination,omitempty"`
// Client specified values. Returned with results.
Tags map[string]interface{} `json:"tags,omitempty"`
// Pipeline specific parameters.
Parameters map[string]interface{} `json:"parameters,omitempty"`
} Is there any fix I can test for this? |
Please try the latest |
Tried the docker run --rm -v ${PWD}:/local openapitools/openapi-generator-cli generate -i https://raw.githubusercontent.com/intel/video-analytics-serving/v0.3.0-alpha/vaserving/rest_api/video-analytics-serving.yaml -g go-experimental -o /local/out/go Still have problems with the test@ubuntu1804-2:~/Downloads/hello-go$ go run client.go
# _/home/test/Downloads/hello-go/openapi
openapi/model_pipeline_request.go:18:10: undefined: OneOfURISourceDeviceSource
openapi/model_pipeline_request.go:19:15: undefined: OneOfKafkaDestinationMQTTDestinationFileDestination
openapi/model_pipeline_request.go:44:39: undefined: OneOfURISourceDeviceSource
openapi/model_pipeline_request.go:46:11: undefined: OneOfURISourceDeviceSource
openapi/model_pipeline_request.go:54:43: undefined: OneOfURISourceDeviceSource
openapi/model_pipeline_request.go:71:39: undefined: OneOfURISourceDeviceSource
openapi/model_pipeline_request.go:76:44: undefined: OneOfKafkaDestinationMQTTDestinationFileDestination
openapi/model_pipeline_request.go:78:11: undefined: OneOfKafkaDestinationMQTTDestinationFileDestination
openapi/model_pipeline_request.go:86:48: undefined: OneOfKafkaDestinationMQTTDestinationFileDestination
openapi/model_pipeline_request.go:103:44: undefined: OneOfKafkaDestinationMQTTDestinationFileDestination
openapi/model_pipeline_request.go:78:11: too many errors The generated code is similar to the previous one: package openapi
import (
"encoding/json"
)
// PipelineRequest struct for PipelineRequest
type PipelineRequest struct {
Source *OneOfURISourceDeviceSource `json:"source,omitempty"`
Destination *OneOfKafkaDestinationMQTTDestinationFileDestination `json:"destination,omitempty"`
// Client specified values. Returned with results.
Tags *map[string]interface{} `json:"tags,omitempty"`
// Pipeline specific parameters.
Parameters *map[string]interface{} `json:"parameters,omitempty"`
} |
Is there not unit-test for For me the generated code does not even compile... My schema
The compile-error:
The generated model class
There is only I checked the behaviour also for |
@cljk Please try |
@wing328 OLD
NEW
Spoiler: the discriminator as enum does not work... |
Is there any way to make oneOf, anyOf, allOf etc. work in de ASP.NET Core server stub generator? We don't have full control of the OpenAPI document we have to auto-generate code for (with the only guarantee being that the document adheres to the 3.0 spec) Recently the document we have to adhere to started using oneOf and anyOf.
the attribute generated references a class named
The same happens for models like this:
Will generate the following uncompilable property:
Is there a way to "fix" this using the generator (since we can't change the OpenAPI doc) or would we have to change the generated code (example: by replacing these classes with generic .NET "object" references)? |
I have a similar use of the generator for netcore as your first example. The difference is that in the path response I have a $ref then the $ref has the oneOf in it. Something like this:
and the schema like this:
this creates a DTO named VPublic that has a combination of all fields across the subschemas. So I guess for you that won't be an answer since you can't manipulate the spec you use? I have no idea how to do this otherwise. For your second example I haven't tried anything like that. But I read that in the new spec Openapi 3.1 they introduced the polymorphism when defining types as an array... ["string","null"] or something like that. Of course that is the spec the tooling is not there yet :) |
Do not crash page if unknown enum found
For context here, oneOf can be combined with any of the other openapi keywords. |
With OAS3 it is possible to use oneOf.
https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.1.md#schema-object
See one example in composed-oneof.yaml from the test suite.
We should discuss how we want to handle this.
In my opinion (for java) the Schema containing only
oneOf
entries should be an interface, and all model classes corresponding to the schema mentioned in theoneOf
should implement this interface.The text was updated successfully, but these errors were encountered: