-
-
Notifications
You must be signed in to change notification settings - Fork 7k
[BUG][typescript] oneOf
+ allOf
breaks serialization
#19868
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
Comments
This issue occurs because the schema described doesn't include a discriminator. Without a discriminator, it's challenging to find out which object the API returns. discriminator:
propertyName: type
mapping:
apple: '#/components/schemas/FruitOneOf'
banana: '#/components/schemas/FruitOneOf1' Example mapping with Apple and Banana: discriminator:
propertyName: type
mapping:
apple: '#/components/schemas/Apple'
banana: '#/components/schemas/Banana' if {"type":"Apple","variety":"good apples"} Swagger file: discriminator:
propertyName: type |
Example of full Swagger fileopenapi: 3.0.1
info:
title: fruity
version: 0.0.1
paths:
/:
get:
responses:
"200":
description: get a fruit
content:
application/json:
schema:
$ref: "#/components/schemas/Fruit"
components:
schemas:
Apple:
title: Apple
type: object
properties:
variety:
type: string
description: The type of apple
required:
- variety
Banana:
title: Banana
type: object
properties:
ripeness:
type: number
description: How ripe the banana is from 0 to 1
Fruit:
title: Fruit
oneOf:
- type: object
allOf:
- type: object
properties:
type:
type: string
enum:
- apple
- $ref: '#/components/schemas/Apple'
required:
- type
- type: object
allOf:
- type: object
properties:
type:
type: string
enum:
- banana
- $ref: '#/components/schemas/Banana'
required:
- type
discriminator:
propertyName: type
mapping:
apple: '#/components/schemas/FruitOneOf'
banana: '#/components/schemas/FruitOneOf1'
|
Yeah, I know that's a workaround, but unfortunately I am dealing with a swagger file in the wild that doesn't specify one. I could patch it but I'd rather not maintain a patch against a complicated and changing 3rd party API if I don't have to. Additionally the OpenAPI v3 spec explicitly allows for discriminator-less schemas (note the use of MAY vs MUST):
As seen here, if the discriminator is provided, it can (and probably should) be used to shortcut validation. But if it is missing, like in the example shown above, it should still run validation against the provided options. |
If the discriminator is not provided, the generator should create a "Franken-class" that combines the properties of each constituent part. |
From what I've read in the spec, I think the way to be fully spec-compliant if there is no discriminator is to list all the child types on the parent and whether it's an Basically do the following pseudo-code:
Generating a franken-class is a good-enough workaround for my particular use-case though, so I would be happy enough to settle for that in the meantime. |
@joscha |
I'd probably do the following:
|
Bug Report Checklist
Description
When using an API that uses a
oneOf
union containing severalallOf
elements, trying to send/receive data will fail with the errortypeMap[type].getAttributeTypeMap is not a function
. The underlying reason is that nodiscriminator
value ormapping
is generated for this value.Prior to #19494 this worked fine, but as of 7.9.0 trying to run this schema will crash.
openapi-generator version
7.9.0, this is a regression.
OpenAPI declaration file content or url
Generation Details
Generated with v7.9.0.
Steps to reproduce
Generate the above yaml using the
typescript
generator, then try to use the API. It will fail withtypeMap[type].getAttributeTypeMap is not a function
Related issues/PRs
Not as far as I could tell.
Suggest a fix
I don't have an exact fix in mind since I don't fully understand how this mapping logic is supposed to work.
The text was updated successfully, but these errors were encountered: