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

Discriminator in OpenApi schema not recognized correctly #993

Closed
markosandalj opened this issue Aug 30, 2024 · 4 comments · Fixed by #1267
Closed

Discriminator in OpenApi schema not recognized correctly #993

markosandalj opened this issue Aug 30, 2024 · 4 comments · Fixed by #1267
Labels
bug 🔥 Something isn't working prioritized 🚚 This issue has been prioritized and will be worked on soon
Milestone

Comments

@markosandalj
Copy link

markosandalj commented Aug 30, 2024

Description

Given this OpenApi schema yaml

components:
  schemas:
    FrontofficePageBlock:
      oneOf:
      - $ref: '#/components/schemas/FrontofficePageBlockLinkWithIconBlockList'
      - $ref: '#/components/schemas/FrontofficePageBlockCourseListingBlock'
      discriminator:
        propertyName: type
        mapping:
          link_with_icon_list: '#/components/schemas/FrontofficePageBlockLinkWithIconBlockList'
          course_listing: '#/components/schemas/FrontofficePageBlockCourseListingBlock'
    FrontofficePageBlockCourseListingBlock:
      allOf:
      - $ref: '#/components/schemas/FrontofficePageBlockShared'
      - $ref: '#/components/schemas/CourseListingBlock'
    FrontofficePageBlockLinkWithIconBlockList:
      allOf:
      - $ref: '#/components/schemas/FrontofficePageBlockShared'
      - $ref: '#/components/schemas/LinkWithIconBlockList'
    FrontofficePageBlockShared:
      type: object
      properties:
        id:
          type: string
        type:
          $ref: '#/components/schemas/FrontofficePageBlockSharedTypeEnum'
      required:
      - id
      - type
    FrontofficePageBlockSharedTypeEnum:
      enum:
      - link_with_icon_list
      - course_listing
      type: string

I would expect the generated types to look like this

export type FrontofficePageBlock =
	| ({ type: 'course_listing' } & FrontofficePageBlockCourseListingBlock)
	| ({ type: 'link_with_icon_list' } & FrontofficePageBlockLinkWithIconBlockList)

but this is what we are getting

export type FrontofficePageBlock = FrontofficePageBlockLinkWithIconBlockList | FrontofficePageBlockCourseListingBlock ;

export type FrontofficePageBlockCourseListingBlock = FrontofficePageBlockShared & CourseListingBlock;
export type FrontofficePageBlockLinkWithIconBlockList = FrontofficePageBlockShared & LinkWithIconBlockList;

export type FrontofficePageBlockShared = {
    id: string;
    type: FrontofficePageBlockSharedTypeEnum;
};

export enum FrontofficePageBlockSharedTypeEnum {
    LINK_WITH_ICON_LIST = 'link_with_icon_list',
    COURSE_LISTING = 'course_listing',
}

At first it seems like types are correct but when trying to narrow the block type inside a switch statement by field "type" it is impossible since each block is not strictly connected to its "type" field.

An example:

switch (block.type) {
    case FrontofficePageBlockSharedTypeEnum.COURSE_LISTING:
        // typescript doesn't recognize this as FrontofficePageBlockCourseListingBlock but as FrontofficePageBlock
        ...
        break
    case FrontofficePageBlockSharedTypeEnum.LINK_WITH_ICON_LIST:
        // typescript doesn't recognize this as FrontofficePageBlockLinkWithIconBlockList but as FrontofficePageBlock
        ...
        break
}

It seems like the generator is ignoring the discriminator field from open api schema.

Reproducible example or configuration

https://stackblitz.com/edit/hey-api-client-fetch-example-emxls6?file=src%2FApp.tsx

OpenAPI specification (optional)

openapi: 3.0.3
info:
  title: Bug API
  version: 1.0.0
  description: API endpoints for bug
components:
  schemas:
    CourseListingBlock:
      type: object
      properties:
        foo:
          type: string
        id:
          type: string
      required:
      - id
      - foo
    LinkWithIconBlockList:
      type: object
      properties:      
        bar:
          type: string    
        id:
          type: string
      required:
      - id
      - bar
    FrontofficePageBlock:
      oneOf:
      - $ref: '#/components/schemas/FrontofficePageBlockLinkWithIconBlockList'
      - $ref: '#/components/schemas/FrontofficePageBlockCourseListingBlock'
      discriminator:
        propertyName: type
        mapping:
          link_with_icon_list: '#/components/schemas/FrontofficePageBlockLinkWithIconBlockList'
          course_listing: '#/components/schemas/FrontofficePageBlockCourseListingBlock'
    FrontofficePageBlockCourseListingBlock:
      allOf:
      - $ref: '#/components/schemas/FrontofficePageBlockShared'
      - $ref: '#/components/schemas/CourseListingBlock'
    FrontofficePageBlockLinkWithIconBlockList:
      allOf:
      - $ref: '#/components/schemas/FrontofficePageBlockShared'
      - $ref: '#/components/schemas/LinkWithIconBlockList'
    FrontofficePageBlockShared:
      type: object
      properties:
        id:
          type: string
        type:
          $ref: '#/components/schemas/FrontofficePageBlockSharedTypeEnum'
      required:
      - id
      - type
    FrontofficePageBlockSharedTypeEnum:
      enum:
      - link_with_icon_list
      - course_listing
      type: string

System information (optional)

No response

@markosandalj markosandalj added the bug 🔥 Something isn't working label Aug 30, 2024
Copy link

stackblitz bot commented Aug 30, 2024

@mrlubos
Copy link
Member

mrlubos commented Aug 30, 2024

@markosandalj I think that sounds right, discriminators aren't supported right now. Will need to add

@mrlubos mrlubos added the prioritized 🚚 This issue has been prioritized and will be worked on soon label Oct 20, 2024
@mrlubos mrlubos added this to the Parser milestone Oct 20, 2024
@mrlubos
Copy link
Member

mrlubos commented Oct 20, 2024

Added to the Parser release, thanks for the patience everyone!

@mrlubos
Copy link
Member

mrlubos commented Nov 10, 2024

This will be fixed in the next release of experimental parser 🎉 Let me know if you still run into cases where it's not handled properly!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🔥 Something isn't working prioritized 🚚 This issue has been prioritized and will be worked on soon
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants