Skip to content

Commit fce449c

Browse files
committed
fix: discriminator and mapping with invalid discriminator property name (#551)
1 parent d00e9c8 commit fce449c

File tree

16 files changed

+78
-35
lines changed

16 files changed

+78
-35
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
feat: `const` keyword OpenAPI 3.0 draft
44
fix: problem with using `anyOf`
5-
feat: `--extract-responses` (nodejs: `extractResponses`) option to extract all schemas from `/components/responses`
5+
feat: `--extract-responses` (nodejs: `extractResponses`) option to extract all schemas from `/components/responses`
6+
fix: discriminator and mapping with invalid discriminator property name (#551)
67

78
## 13.0.0
89

src/schema-parser/base-schema-parsers/discriminator.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ class DiscriminatorSchemaParser extends MonoSchemaParser {
9090
content: this.config.Ts.IntersectionType([
9191
this.config.Ts.ObjectWrapper(
9292
this.config.Ts.TypeField({
93-
key: discriminator.propertyName,
93+
key: this.config.Ts.StringValue(discriminator.propertyName),
9494
value: 'Key',
9595
}),
9696
),

tests/generated/v3.0/additional-properties2.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* ---------------------------------------------------------------
1010
*/
1111

12-
export type Primitive = string | number | boolean | null | (string & number & boolean & null);
12+
export type Primitive = string | number | boolean | null;
1313

1414
export type PrimitiveMap = Record<string, Primitive>;
1515

tests/generated/v3.0/anyof-example.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ export class Api<SecurityDataType extends unknown> extends HttpClient<SecurityDa
241241
* @name PetsPartialUpdate
242242
* @request PATCH:/pets
243243
*/
244-
petsPartialUpdate: (data: PetByAge | PetByType | (PetByAge & PetByType), params: RequestParams = {}) =>
244+
petsPartialUpdate: (data: PetByAge | PetByType, params: RequestParams = {}) =>
245245
this.request<void, any>({
246246
path: `/pets`,
247247
method: "PATCH",

tests/generated/v3.0/full-swagger-scheme.ts

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3021,7 +3021,7 @@ export interface Installation {
30213021
* @example "https://api.github.com/installations/1/access_tokens"
30223022
*/
30233023
access_tokens_url: string;
3024-
account: SimpleUser | Enterprise | (SimpleUser & Enterprise) | null;
3024+
account: SimpleUser | Enterprise | null;
30253025
/** @example 1 */
30263026
app_id: number;
30273027
/** @example "github-actions" */
@@ -17831,13 +17831,6 @@ export class Api<SecurityDataType extends unknown> extends HttpClient<SecurityDa
1783117831
status?: "queued" | "in_progress";
1783217832
[key: string]: any;
1783317833
}
17834-
| ({
17835-
status?: "completed";
17836-
[key: string]: any;
17837-
} & {
17838-
status?: "queued" | "in_progress";
17839-
[key: string]: any;
17840-
})
1784117834
) & {
1784217835
/**
1784317836
* Displays a button on GitHub that can be clicked to alert your app to do additional tasks. For example, a code linting app can display a button that automatically fixes detected errors. The button created in this object is displayed after the check run completes. When a user clicks the button, GitHub sends the [`check_run.requested_action` webhook](https://docs.github.com/webhooks/event-payloads/#check_run) to your app. Each action includes a `label`, `identifier` and `description`. A maximum of three actions are accepted. See the [`actions` object](https://docs.github.com/rest/reference/checks#actions-object) description. To learn more about check runs and requested actions, see "[Check runs and requested actions](https://docs.github.com/rest/reference/checks#check-runs-and-requested-actions)." To learn more about check runs and requested actions, see "[Check runs and requested actions](https://docs.github.com/rest/reference/checks#check-runs-and-requested-actions)."
@@ -17979,13 +17972,6 @@ export class Api<SecurityDataType extends unknown> extends HttpClient<SecurityDa
1797917972
status?: "queued" | "in_progress";
1798017973
[key: string]: any;
1798117974
}
17982-
| ({
17983-
status?: "completed";
17984-
[key: string]: any;
17985-
} & {
17986-
status?: "queued" | "in_progress";
17987-
[key: string]: any;
17988-
})
1798917975
) & {
1799017976
/**
1799117977
* Possible further actions the integrator can perform, which a user may trigger. Each action includes a `label`, `identifier` and `description`. A maximum of three actions are accepted. See the [`actions` object](https://docs.github.com/rest/reference/checks#actions-object) description. To learn more about check runs and requested actions, see "[Check runs and requested actions](https://docs.github.com/rest/reference/checks#check-runs-and-requested-actions)."
@@ -22161,17 +22147,7 @@ export class Api<SecurityDataType extends unknown> extends HttpClient<SecurityDa
2216122147
branch: string;
2216222148
/** The repository directory that includes the source files for the Pages site. Allowed paths are `/` or `/docs`. */
2216322149
path: "/" | "/docs";
22164-
}
22165-
| (
22166-
| "gh-pages"
22167-
| "master"
22168-
| ("master /docs" & {
22169-
/** The repository branch used to publish your site's source files. */
22170-
branch: string;
22171-
/** The repository directory that includes the source files for the Pages site. Allowed paths are `/` or `/docs`. */
22172-
path: "/" | "/docs";
22173-
})
22174-
);
22150+
};
2217522151
},
2217622152
params: RequestParams = {},
2217722153
) =>

tests/generated/v3.0/personal-api-example.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ export type TestAllOfDc2 = FooBarBaz & {
186186
prop?: string;
187187
};
188188

189-
export type TestAnyOfDc = (FooBarBaz | FooBar | (FooBarBaz & FooBar)) & {
189+
export type TestAnyOfDc = (FooBarBaz | FooBar) & {
190190
prop?: string;
191191
};
192192

tests/spec/discriminator/expected.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,12 @@ export type LizardWithEnum = BasePetWithEnum & {
107107
lovesRocks?: boolean;
108108
};
109109

110+
export type InvalidDiscriminatorPropertyName = BaseInvalidDiscriminatorPropertyName &
111+
(
112+
| BaseInvalidDiscriminatorPropertyNameTypeMapping<"num", number>
113+
| BaseInvalidDiscriminatorPropertyNameTypeMapping<"str", string>
114+
);
115+
110116
interface BaseBlockDtoWithEnum {
111117
title: string;
112118
type: BlockDTOEnum;
@@ -139,3 +145,9 @@ interface BasePetWithEnum {
139145
type BasePetWithEnumPetTypeMapping<Key, Type> = {
140146
pet_type: Key;
141147
} & Type;
148+
149+
type BaseInvalidDiscriminatorPropertyName = object;
150+
151+
type BaseInvalidDiscriminatorPropertyNameTypeMapping<Key, Type> = {
152+
"@type": Key;
153+
} & Type;

tests/spec/discriminator/schema.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,20 @@
363363
}
364364
}
365365
]
366+
},
367+
"InvalidDiscriminatorPropertyName": {
368+
"type": "object",
369+
"discriminator": {
370+
"propertyName": "@type",
371+
"mapping": {
372+
"num": {
373+
"type": "number"
374+
},
375+
"str": {
376+
"type": "string"
377+
}
378+
}
379+
}
366380
}
367381
}
368382
}

tests/spec/discriminator/schema.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,12 @@ export type LizardWithEnum = BasePetWithEnum & {
107107
lovesRocks?: boolean;
108108
};
109109

110+
export type InvalidDiscriminatorPropertyName = BaseInvalidDiscriminatorPropertyName &
111+
(
112+
| BaseInvalidDiscriminatorPropertyNameTypeMapping<"num", number>
113+
| BaseInvalidDiscriminatorPropertyNameTypeMapping<"str", string>
114+
);
115+
110116
interface BaseBlockDtoWithEnum {
111117
title: string;
112118
type: BlockDTOEnum;
@@ -139,3 +145,9 @@ interface BasePetWithEnum {
139145
type BasePetWithEnumPetTypeMapping<Key, Type> = {
140146
pet_type: Key;
141147
} & Type;
148+
149+
type BaseInvalidDiscriminatorPropertyName = object;
150+
151+
type BaseInvalidDiscriminatorPropertyNameTypeMapping<Key, Type> = {
152+
"@type": Key;
153+
} & Type;

tests/spec/enumNamesAsValues/expected.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export type TestAllOfDc2 = FooBarBaz & {
1717
prop?: string;
1818
};
1919

20-
export type TestAnyOfDc = (FooBarBaz | FooBar | (FooBarBaz & FooBar)) & {
20+
export type TestAnyOfDc = (FooBarBaz | FooBar) & {
2121
prop?: string;
2222
};
2323

tests/spec/enumNamesAsValues/schema.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export type TestAllOfDc2 = FooBarBaz & {
1717
prop?: string;
1818
};
1919

20-
export type TestAnyOfDc = (FooBarBaz | FooBar | (FooBarBaz & FooBar)) & {
20+
export type TestAnyOfDc = (FooBarBaz | FooBar) & {
2121
prop?: string;
2222
};
2323

tests/spec/nullable-3.0/expected.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export interface TestObject {
2323
otherObjectMaybeNullA: OtherObject | null;
2424
otherObjectMaybeNullB: OtherObject | null;
2525
otherObjectMaybeNullC: OtherObject | null;
26-
otherObjectMaybeNullD: OtherObject | null | (OtherObject & null);
26+
otherObjectMaybeNullD: OtherObject | null;
2727
}
2828

2929
export type OtherObject = object;

tests/spec/nullable-3.0/schema.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export interface TestObject {
2323
otherObjectMaybeNullA: OtherObject | null;
2424
otherObjectMaybeNullB: OtherObject | null;
2525
otherObjectMaybeNullC: OtherObject | null;
26-
otherObjectMaybeNullD: OtherObject | null | (OtherObject & null);
26+
otherObjectMaybeNullD: OtherObject | null;
2727
}
2828

2929
export type OtherObject = object;

tests/spec/object-types/expected.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@
1111

1212
type UtilRequiredKeys<T, K extends keyof T> = Omit<T, K> & Required<Pick<T, K>>;
1313

14+
/**
15+
* title
16+
* description
17+
* @example "https://ya.ru/a.png"
18+
*/
19+
export type AnyOfWithNullable = string | null;
20+
1421
/** https://github.com/acacode/swagger-typescript-api/issues/445 */
1522
export interface SpecificEnum1 {
1623
myEnum?: ["foo", "bar", "baz"];

tests/spec/object-types/schema.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,20 @@
1919
"produces": ["application/json"],
2020
"paths": {},
2121
"definitions": {
22+
"AnyOfWithNullable": {
23+
"anyOf": [
24+
{
25+
"type": "string"
26+
},
27+
{
28+
"type": "null"
29+
}
30+
],
31+
"description": "description",
32+
"example": "https://ya.ru/a.png",
33+
"nullable": true,
34+
"title": "title"
35+
},
2236
"SpecificEnum1": {
2337
"type": "object",
2438
"description": "https://github.com/acacode/swagger-typescript-api/issues/445",

tests/spec/object-types/schema.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@
1111

1212
type UtilRequiredKeys<T, K extends keyof T> = Omit<T, K> & Required<Pick<T, K>>;
1313

14+
/**
15+
* title
16+
* description
17+
* @example "https://ya.ru/a.png"
18+
*/
19+
export type AnyOfWithNullable = string | null;
20+
1421
/** https://github.com/acacode/swagger-typescript-api/issues/445 */
1522
export interface SpecificEnum1 {
1623
myEnum?: ["foo", "bar", "baz"];

0 commit comments

Comments
 (0)