Skip to content

Commit db37f3c

Browse files
pvanagtmaalPiet van Agtmaal
and
Piet van Agtmaal
authored
Fix property escaping for discriminators (#1166)
* Fix property escaping for discriminators * Add changeset --------- Co-authored-by: Piet van Agtmaal <[email protected]>
1 parent 74bfc0d commit db37f3c

File tree

4 files changed

+62
-2
lines changed

4 files changed

+62
-2
lines changed

.changeset/five-eels-call.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"openapi-typescript": patch
3+
---
4+
5+
Fix property escaping for discriminators

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

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

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

+29
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,35 @@ describe("Schema Object", () => {
364364
operation: "test";
365365
} & Omit<components["schemas"]["parent"], "operation"> & {
366366
string?: string;
367+
}`);
368+
});
369+
370+
test("discriminator escape", () => {
371+
const schema: SchemaObject = {
372+
type: "object",
373+
allOf: [
374+
{ $ref: 'components["schemas"]["parent"]' },
375+
{ type: "object", properties: { string: { type: "string" } } },
376+
],
377+
};
378+
const generated = transformSchemaObject(schema, {
379+
path: options.path,
380+
ctx: {
381+
...options.ctx,
382+
discriminators: {
383+
'components["schemas"]["parent"]': {
384+
propertyName: "@type",
385+
mapping: {
386+
test: options.path,
387+
},
388+
},
389+
},
390+
},
391+
});
392+
expect(generated).toBe(`{
393+
"@type": "test";
394+
} & Omit<components["schemas"]["parent"], "@type"> & {
395+
string?: string;
367396
}`);
368397
});
369398
});

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)