openapi-ts icon indicating copy to clipboard operation
openapi-ts copied to clipboard

JSON Schema with if/then keywords is generated into unknown type

Open giovanism opened this issue 3 months ago • 2 comments

Description

My use case is to use allOf in combination with if and then keyword. I'm expecting to have the sub schema defined under then merged but they are generated as unknown instead.

Reproducible example or configuration

https://github.com/giovanism/issue-demo-hey-api-openapi-ts-if-then

OpenAPI specification (optional)

No response

System information (optional)

No response

giovanism avatar Oct 15 '25 07:10 giovanism

@giovanism What's the expected output in your example?

mrlubos avatar Oct 15 '25 14:10 mrlubos

if, then should create a union type based on the conditions with the conditional attribute(or schema) as a required property.

This is also the preferred way to define a discriminator in 3.1.x+, rather than using the discriminator keyword.

type CreateUserRequest =
  | {
      accountType: 'premium';
      billingAddress: Address;
      creditLimit: number & { min: 1000; max: 10000 };
      // Other optional properties
    }
  | {
      accountType: 'enterprise';
      companyInfo: CompanyInfo;
      billingAddress: Address;
      creditLimit: number & { min: 10000 };
      // Other optional properties
    }
  | {
      accountType: 'basic';
      preferences?: UserPreferences;
      // Other optional properties
    };

jeremyfiel avatar Oct 28 '25 01:10 jeremyfiel