-
-
Notifications
You must be signed in to change notification settings - Fork 121
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
Weird types for schemas with discriminator #1561
Comments
@zumm is this with the experimental parser? https://heyapi.dev/openapi-ts/configuration#parser |
Yes, here is my config: import { defineConfig } from '@hey-api/openapi-ts'
export default defineConfig({
experimentalParser: true,
client: '@hey-api/client-fetch',
input: 'openapi.yaml',
output: 'api',
plugins: [{
name: '@hey-api/transformers',
dates: true,
}, {
name: '@hey-api/sdk',
transformer: true,
}, {
name: '@tanstack/vue-query',
// @ts-expect-error undocumented feature
output: 'vue-query',
}],
}) |
Thanks! I'll have a look |
Confirming that there is an issue with experimental parser. Here is my With classic parser, the generated code is: export type Principal = PrincipalParent | PrincipalStudent;
export type PrincipalParent = {
type: 'PARENT';
value: ParentPrincipal;
};
export type PrincipalStudent = {
type: 'STUDENT';
value: StudentPrincipal;
}; With experimental parser, the generated code is: export type Principal =
| ({
type?: 'PrincipalParent';
} & PrincipalParent)
| ({
type?: 'PrincipalStudent';
} & PrincipalStudent);
export type PrincipalParent = {
type: 'PARENT';
value: ParentPrincipal;
};
export type PrincipalStudent = {
type: 'STUDENT';
value: StudentPrincipal;
}; I did not have time to check the parser code, but it seems that there is some eager logic to encode the discriminator. We may not need explicit discriminators as TS is typed structurally. PS: There are some issues regarding the error types which are all typed as |
@vst If you can share the relevant parts of your spec, that would be appreciated! |
@mrlubos Sure. Sorry, it took a while to extract the relevant part. Let me know if you need more. openapi: 3.0.0
info:
title: API Documentation
version: 0.0.21
components:
schemas:
EmailAddress:
type: string
ParentId:
description: |-
AuthUser Identifier.
Type representing Universally Unique Identifiers (UUID) as specified in RFC 4122.
type: string
ParentPrincipal:
properties:
email:
$ref: '#/components/schemas/EmailAddress'
emailVerifiedAt:
description: |-
Parent email verified at.
UTCTime
nullable: true
type: string
id:
$ref: '#/components/schemas/ParentId'
username:
description: Parent username.
type: string
required:
- id
- email
- username
- emailVerifiedAt
type: object
Principal:
discriminator:
mapping:
PARENT: PrincipalParent
STUDENT: PrincipalStudent
propertyName: type
oneOf:
- $ref: '#/components/schemas/PrincipalParent'
- $ref: '#/components/schemas/PrincipalStudent'
PrincipalParent:
properties:
type:
enum:
- PARENT
type: string
value:
$ref: '#/components/schemas/ParentPrincipal'
required:
- value
- type
type: object
PrincipalStudent:
properties:
type:
enum:
- STUDENT
type: string
value:
$ref: '#/components/schemas/StudentPrincipal'
required:
- value
- type
type: object
StudentId:
description: |-
AuthUser Identifier.
Type representing Universally Unique Identifiers (UUID) as specified in RFC 4122.
type: string
StudentPrincipal:
properties:
email:
$ref: '#/components/schemas/EmailAddress'
emailVerifiedAt:
description: |-
Student email verified at.
UTCTime
nullable: true
type: string
id:
$ref: '#/components/schemas/StudentId'
username:
description: Student username.
type: string
required:
- id
- email
- username
- emailVerifiedAt
type: object
paths:
/me:
get:
description: This endpoint returns principal information.
responses:
'200':
content:
application/json;charset=utf-8:
schema:
$ref: '#/components/schemas/Principal'
description: ''
'401':
description: Authentication failed
'403':
description: Authorization failed
summary: Principal Endpoint |
Description
For scheme described below openapi-ts generates this code:
Shouldn't it be just:
Without third union member and with required
type
?Also this scheme causes warning:
As i understand transformers doesn't work with unions. Could we get discriminated unions supported?
Reproducible example or configuration
No response
OpenAPI specification (optional)
System information (optional)
No response
The text was updated successfully, but these errors were encountered: