Skip to content

Commit e27aa46

Browse files
author
Piet van Agtmaal
committed
Fix property escaping for discriminators
1 parent 65ff4b1 commit e27aa46

File tree

3 files changed

+57
-2
lines changed

3 files changed

+57
-2
lines changed

packages/openapi-typescript/src/transform/schema-object.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ export function defaultSchemaObjectTransform(schemaObject: SchemaObject | Refere
184184
const matchedValue = Object.entries(discriminator.mapping).find(([_, v]) => (!v.startsWith("#") && v === value) || (v.startsWith("#") && parseRef(v).path.pop() === value));
185185
if (matchedValue) value = matchedValue[0]; // why was this designed backwards!?
186186
}
187-
coreType.unshift(indent(`${discriminator.propertyName}: ${escStr(value)};`, indentLv + 1));
187+
coreType.unshift(indent(`${escObjKey(discriminator.propertyName)}: ${escStr(value)};`, indentLv + 1));
188188
break;
189189
}
190190
}

packages/openapi-typescript/test/schema-object.test.ts

+29
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,35 @@ describe("Schema Object", () => {
349349
operation: "test";
350350
} & Omit<components["schemas"]["parent"], "operation"> & {
351351
string?: string;
352+
}`);
353+
});
354+
355+
test("discriminator escape", () => {
356+
const schema: SchemaObject = {
357+
type: "object",
358+
allOf: [
359+
{ $ref: 'components["schemas"]["parent"]' },
360+
{ type: "object", properties: { string: { type: "string" } } },
361+
],
362+
};
363+
const generated = transformSchemaObject(schema, {
364+
path: options.path,
365+
ctx: {
366+
...options.ctx,
367+
discriminators: {
368+
'components["schemas"]["parent"]': {
369+
propertyName: "@type",
370+
mapping: {
371+
test: options.path,
372+
},
373+
},
374+
},
375+
},
376+
});
377+
expect(generated).toBe(`{
378+
"@type": "test";
379+
} & Omit<components["schemas"]["parent"], "@type"> & {
380+
string?: string;
352381
}`);
353382
});
354383
});

packages/openapi-typescript/test/utils.test.ts

+27-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { bench } from "vitest";
2-
import { parseRef, tsIntersectionOf, tsUnionOf } from "../src/utils.js";
2+
import { escObjKey, parseRef, tsIntersectionOf, tsUnionOf } from "../src/utils.js";
33

44
describe("utils", () => {
55
describe("tsUnionOf", () => {
@@ -80,4 +80,30 @@ describe("utils", () => {
8080
expect(parseRef('components["schemas"]["SchemaObject"]')).toStrictEqual({ filename: ".", path: ["components", "schemas", "SchemaObject"] });
8181
});
8282
});
83+
84+
describe("escObjKey", () => {
85+
it("basic", () => {
86+
expect(escObjKey("some-prop")).toStrictEqual("\"some-prop\"");
87+
});
88+
89+
it("@ escapes", () => {
90+
expect(escObjKey("@type")).toStrictEqual("\"@type\"");
91+
});
92+
93+
it("number escapes", () => {
94+
expect(escObjKey("123var")).toStrictEqual("\"123var\"");
95+
});
96+
97+
it("only number no escapes", () => {
98+
expect(escObjKey("123")).toStrictEqual("123");
99+
});
100+
101+
it("$ no escapes", () => {
102+
expect(escObjKey("$ref")).toStrictEqual("$ref");
103+
});
104+
105+
it("_ no escapes", () => {
106+
expect(escObjKey("_ref_")).toStrictEqual("_ref_");
107+
})
108+
})
83109
});

0 commit comments

Comments
 (0)