Skip to content
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

[FEATURE] supporting discriminator - openAPI v3 #563

Open
fordavid opened this issue Mar 2, 2023 · 3 comments
Open

[FEATURE] supporting discriminator - openAPI v3 #563

fordavid opened this issue Mar 2, 2023 · 3 comments
Assignees
Labels
enhancement New feature or request

Comments

@fordavid
Copy link

fordavid commented Mar 2, 2023

Is your feature request related to a problem? Please describe.
The api config openAPI v3.0.2 json file I'm using contains discriminators. the code generation build process end up with error message and the generated files are missing the discriminators data.
sample json file that generate the problem attached.

(https://github.com/epam-cross-platform-lab/swagger-dart-code-generator/files/10871982/openapi3.txt)

Is it planned to be supported?

[SEVERE] swagger_dart_code_generator on swagger/.DS_Store (cached):

FileSystemException: Failed to decode data using encoding 'utf-8', path = 'swagger/.DS_Store'
[SEVERE] swagger_dart_code_generator on lib/swagger/.DS_Store (cached):

FileSystemException: Failed to decode data using encoding 'utf-8', path = 'lib/swagger/.DS_Store'
[SEVERE] Failed after 43ms
pub finished with exit code 1

Describe the solution you'd like
see the attached issue
Link to swagger previous request

Swagger specification link
Link to swagger/OpenApi documentation
[openapi3.txt]

@fordavid fordavid added the enhancement New feature or request label Mar 2, 2023
@fryette
Copy link
Contributor

fryette commented May 22, 2023

Discriminator section from Swagger documentation
https://swagger.io/docs/specification/data-models/inheritance-and-polymorphism/

@nythrox
Copy link

nythrox commented Jan 20, 2024

There are a few weird ways this could be typed.

Least frightening, oneOf could be typed with a generated class OneOf2<A,B> class OneOf3<A,B, C> class OneOf4<A,B, C,D> etc... and the user can final oneOf = result.body!; if (oneOf.a != null) // a is A.

If you want it to be the most strictly typed possible, every time we encounter a "anyOf": [{$ref: '#/components/schemas/Cat' }, {$ref: '#/components/schemas/Dog'}], we could generate the following classes:

sealed class CatOrDog {}
sealed class CatOrDog_Cat implements CatOrDog, Cat{}
sealed class CatOrDog_Dog implements CatOrDog, Dog  {}

switch (result.body!) { // AnyOfCatDog
   Cat(:name) => ...
   Dog(:age) => ...
}
// Works, checks for exhaustiveness!

We could also just return Object and let the user match on it to type-check.

@dJani97
Copy link

dJani97 commented Mar 21, 2024

This would be great - currently I have to use a different library called dart_mappable for manually modelling those classes that have a discriminator.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

5 participants