Skip to content

Commit 30d0d88

Browse files
authored
Add test for #3369 (#5154)
1 parent cdff16f commit 30d0d88

File tree

3 files changed

+203
-0
lines changed

3 files changed

+203
-0
lines changed

packages/rtk-query-codegen-openapi/test/__snapshots__/generateEndpoints.test.ts.snap

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3205,6 +3205,57 @@ export const { useGetApiV1AnimalsQuery } = injectedRtkApi;
32053205
"
32063206
`;
32073207

3208+
exports[`tests from issues > issue #3369: discriminated unions should use enum values, not schema names 1`] = `
3209+
"import { api } from "./tmp/emptyApi";
3210+
const injectedRtkApi = api.injectEndpoints({
3211+
endpoints: (build) => ({
3212+
getAllowances: build.query<GetAllowancesApiResponse, GetAllowancesApiArg>({
3213+
query: () => ({ url: \`/api/allowances\` }),
3214+
}),
3215+
createAllowance: build.mutation<
3216+
CreateAllowanceApiResponse,
3217+
CreateAllowanceApiArg
3218+
>({
3219+
query: (queryArg) => ({
3220+
url: \`/api/allowances/\${queryArg.id}\`,
3221+
method: "POST",
3222+
body: queryArg.allowance,
3223+
}),
3224+
}),
3225+
}),
3226+
overrideExisting: false,
3227+
});
3228+
export { injectedRtkApi as enhancedApi };
3229+
export type GetAllowancesApiResponse = /** status 200 Success */ Allowance[];
3230+
export type GetAllowancesApiArg = void;
3231+
export type CreateAllowanceApiResponse = /** status 201 Created */ Allowance;
3232+
export type CreateAllowanceApiArg = {
3233+
id: string;
3234+
allowance: Allowance;
3235+
};
3236+
export type EngineeringAllowance = {
3237+
allowance_type?: "engineering";
3238+
distribution?: "MARECO" | "LINEAR";
3239+
capacity_speed_limit?: number;
3240+
};
3241+
export type StandardAllowance = {
3242+
allowance_type?: "standard";
3243+
default_value?: number;
3244+
ranges?: {
3245+
min?: number;
3246+
max?: number;
3247+
}[];
3248+
};
3249+
export type Allowance =
3250+
| ({
3251+
allowance_type: "engineering";
3252+
} & EngineeringAllowance)
3253+
| ({
3254+
allowance_type: "standard";
3255+
} & StandardAllowance);
3256+
"
3257+
`;
3258+
32083259
exports[`yaml parsing > should be able to use read a yaml file 1`] = `
32093260
"import { api } from "./tmp/emptyApi";
32103261
export const addTagTypes = ["pet", "store", "user"] as const;
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
{
2+
"openapi": "3.0.1",
3+
"info": {
4+
"title": "Discriminator Enum Test - Issue 3369",
5+
"description": "Test that discriminated unions use enum values (e.g., 'standard') instead of schema names (e.g., 'StandardAllowance')",
6+
"version": "1.0.0"
7+
},
8+
"servers": [
9+
{
10+
"url": "https://localhost:8000"
11+
}
12+
],
13+
"paths": {
14+
"/api/allowances": {
15+
"get": {
16+
"operationId": "getAllowances",
17+
"summary": "Get all allowances",
18+
"responses": {
19+
"200": {
20+
"description": "Success",
21+
"content": {
22+
"application/json": {
23+
"schema": {
24+
"type": "array",
25+
"items": {
26+
"$ref": "#/components/schemas/Allowance"
27+
}
28+
}
29+
}
30+
}
31+
}
32+
}
33+
}
34+
},
35+
"/api/allowances/{id}": {
36+
"post": {
37+
"operationId": "createAllowance",
38+
"summary": "Create an allowance",
39+
"parameters": [
40+
{
41+
"name": "id",
42+
"in": "path",
43+
"required": true,
44+
"schema": {
45+
"type": "string"
46+
}
47+
}
48+
],
49+
"requestBody": {
50+
"required": true,
51+
"content": {
52+
"application/json": {
53+
"schema": {
54+
"$ref": "#/components/schemas/Allowance"
55+
}
56+
}
57+
}
58+
},
59+
"responses": {
60+
"201": {
61+
"description": "Created",
62+
"content": {
63+
"application/json": {
64+
"schema": {
65+
"$ref": "#/components/schemas/Allowance"
66+
}
67+
}
68+
}
69+
}
70+
}
71+
}
72+
}
73+
},
74+
"components": {
75+
"schemas": {
76+
"Allowance": {
77+
"oneOf": [
78+
{
79+
"$ref": "#/components/schemas/EngineeringAllowance"
80+
},
81+
{
82+
"$ref": "#/components/schemas/StandardAllowance"
83+
}
84+
],
85+
"discriminator": {
86+
"propertyName": "allowance_type"
87+
}
88+
},
89+
"EngineeringAllowance": {
90+
"type": "object",
91+
"properties": {
92+
"allowance_type": {
93+
"type": "string",
94+
"enum": ["engineering"]
95+
},
96+
"distribution": {
97+
"type": "string",
98+
"enum": ["MARECO", "LINEAR"]
99+
},
100+
"capacity_speed_limit": {
101+
"type": "number",
102+
"format": "double"
103+
}
104+
}
105+
},
106+
"StandardAllowance": {
107+
"type": "object",
108+
"properties": {
109+
"allowance_type": {
110+
"type": "string",
111+
"enum": ["standard"]
112+
},
113+
"default_value": {
114+
"type": "number"
115+
},
116+
"ranges": {
117+
"type": "array",
118+
"items": {
119+
"type": "object",
120+
"properties": {
121+
"min": {
122+
"type": "number"
123+
},
124+
"max": {
125+
"type": "number"
126+
}
127+
}
128+
}
129+
}
130+
}
131+
}
132+
}
133+
}
134+
}

packages/rtk-query-codegen-openapi/test/generateEndpoints.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -725,6 +725,24 @@ describe('tests from issues', () => {
725725
});
726726
expect(result).toMatchSnapshot();
727727
});
728+
729+
it('issue #3369: discriminated unions should use enum values, not schema names', async () => {
730+
const result = await generateEndpoints({
731+
apiFile: './tmp/emptyApi.ts',
732+
schemaFile: './test/fixtures/issue-3369-discriminator-enum.json',
733+
});
734+
735+
// The discriminator value should be 'engineering' and 'standard' (from enum)
736+
// NOT 'EngineeringAllowance' and 'StandardAllowance' (schema names)
737+
expect(result).toContain('allowance_type: "engineering"');
738+
expect(result).toContain('allowance_type: "standard"');
739+
740+
// Should NOT use schema names as discriminator values
741+
expect(result).not.toContain('allowance_type: "EngineeringAllowance"');
742+
expect(result).not.toContain('allowance_type: "StandardAllowance"');
743+
744+
expect(result).toMatchSnapshot();
745+
});
728746
});
729747

730748
describe('openapi spec', () => {

0 commit comments

Comments
 (0)