Open MGabr opened 1 month ago
Add a api-docs.yaml spec with a polymorphic schema :
api-docs.yaml
openapi: 3.0.1 info: title: OpenAPI definition version: v0 servers: - url: http://localhost:8080 description: Generated server url paths: /pet: get: tags: - pet-controller operationId: getPet responses: "200": description: OK content: '*/*': schema: $ref: "#/components/schemas/Pet" components: schemas: Cat: required: - type type: object allOf: - $ref: "#/components/schemas/Pet" - type: object properties: lives: type: integer format: int32 Dog: required: - type type: object allOf: - $ref: "#/components/schemas/Pet" - type: object properties: age: type: integer format: int32 Pet: required: - type type: object properties: type: type: string enum: - DOG - CAT discriminator: propertyName: type mapping: DOG: "#/components/schemas/Dog" CAT: "#/components/schemas/Cat"
Add a config orval.config.js with useNativeEnums: true
orval.config.js
useNativeEnums: true
module.exports = { 'api-docs-file': { input: './api-docs.yaml', output: { target: './api-orval.ts', override: { useNativeEnums: true, } } }, };
Generate types with orval --config orval.config.js.
orval --config orval.config.js
Add a TypeScript file api-orval-types-test.ts that uses the types:
api-orval-types-test.ts
import {Dog, DogType} from './api-orval'; const dog: Dog = { type: DogType.DOG, age: 7, };
Run tsc api-orval.ts api-orval-types-test.ts
tsc api-orval.ts api-orval-types-test.ts
The following model gets generated:
export enum PetType { DOG= 'DOG', CAT= 'CAT', } export interface Pet { type: PetType; } export enum DogType { DOG= 'DOG', } export type DogAllOf = { age?: number; }; export type Dog = Pet & DogAllOf & { type: DogType; };
So there are two different types for the type field of Dog. PetType for Pet and DogType for Dog.
type
Dog
PetType
Pet
DogType
This leads to the following TypeScript errors:
Type 'import("api-orval").DogType' is not assignable to type 'never'. Type 'number' is not assignable to type 'never'.
No TypeScript errors.
Types like the following being generated:
export enum PetType { DOG= 'DOG', CAT= 'CAT', } export interface Pet { type: PetType; } export type DogAllOf = { age?: number; }; export type Dog = Pet & DogAllOf & { type: PetType.DOG; };
System: OS: Windows 11 10.0.26100 CPU: (12) x64 AMD Ryzen 5 3600X 6-Core Processor Memory: 17.26 GB / 31.95 GB npmPackages: axios: ^1.7.7 => 1.7.7 orval: ^7.1.1 => 7.1.1 typescript: ^5.6.2 => 5.6.2
What are the steps to reproduce this issue?
Add a
api-docs.yaml
spec with a polymorphic schema :Add a config
orval.config.js
withuseNativeEnums: true
Generate types with
orval --config orval.config.js
.Add a TypeScript file
api-orval-types-test.ts
that uses the types:Run
tsc api-orval.ts api-orval-types-test.ts
What happens?
The following model gets generated:
So there are two different types for the
type
field ofDog
.PetType
forPet
andDogType
forDog
.This leads to the following TypeScript errors:
What were you expecting to happen?
No TypeScript errors.
Types like the following being generated:
What versions are you using?