From 477779cc55bbe66647c3d87ca85ede1ab01a561d Mon Sep 17 00:00:00 2001 From: Till Schweneker Date: Mon, 4 Dec 2023 16:15:18 +0100 Subject: [PATCH 1/3] fix: wrap properties beginning with a number in quotes --- lib/src/utils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/src/utils.ts b/lib/src/utils.ts index 7f626d9c..6d7986a5 100644 --- a/lib/src/utils.ts +++ b/lib/src/utils.ts @@ -15,7 +15,7 @@ export function normalizeString(text: string) { } export const wrapWithQuotesIfNeeded = (str: string) => { - if (/^\w+$/.test(str)) { + if (/^[a-zA-Z]\w*$/.test(str)) { return str; } From 0a1cad0450ed20125fe0ae28279f3bab0d796c36 Mon Sep 17 00:00:00 2001 From: Till Schweneker Date: Tue, 5 Dec 2023 11:13:14 +0100 Subject: [PATCH 2/3] test: add test to check external files for properties starting with a number --- lib/tests/ref-in-another-file.test.ts | 2 +- lib/tests/ref-in-another-file/single-schema.yaml | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/tests/ref-in-another-file.test.ts b/lib/tests/ref-in-another-file.test.ts index 74b2e026..c11bea03 100644 --- a/lib/tests/ref-in-another-file.test.ts +++ b/lib/tests/ref-in-another-file.test.ts @@ -19,7 +19,7 @@ test("ref-in-another-file", async () => { "parameters": [], "path": "/robots.txt", "requestFormat": "json", - "response": "z.object({ name: z.string(), completed: z.boolean() }).passthrough()", + "response": "z.object({ name: z.string(), completed: z.boolean(), "0_property_starting_with_number": z.number() }).passthrough()", }, ], "issues": { diff --git a/lib/tests/ref-in-another-file/single-schema.yaml b/lib/tests/ref-in-another-file/single-schema.yaml index 1112e105..09283a28 100644 --- a/lib/tests/ref-in-another-file/single-schema.yaml +++ b/lib/tests/ref-in-another-file/single-schema.yaml @@ -5,6 +5,9 @@ properties: type: string completed: type: boolean + 0_property_starting_with_number: + type: number required: - name - completed + - 0_property_starting_with_number From 80c0ca5b48c683b5da36ff2d9e3eeb6cb8e52daf Mon Sep 17 00:00:00 2001 From: Till Schweneker Date: Tue, 5 Dec 2023 11:22:08 +0100 Subject: [PATCH 3/3] chore: added changeset --- .changeset/warm-scissors-live.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/warm-scissors-live.md diff --git a/.changeset/warm-scissors-live.md b/.changeset/warm-scissors-live.md new file mode 100644 index 00000000..8df5cb1f --- /dev/null +++ b/.changeset/warm-scissors-live.md @@ -0,0 +1,5 @@ +--- +"openapi-zod-client": patch +--- + +When a property from an external json or yaml file starts with a number, e.g. 1st, instead of first, the generated Zod-Schema is corrupt. The change in the wrapWithQuotesIfNeeded method makes sure, that any property starting with a number is wrapped in quotes.