Skip to content

Commit

Permalink
fix: Properly escape property keys (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
eliassjogreen authored Jul 24, 2024
1 parent 7487b43 commit 5779444
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
2 changes: 1 addition & 1 deletion deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@denosaurs/typefetch",
"version": "0.0.8",
"version": "0.0.9",
"exports": {
".": "./main.ts"
},
Expand Down
9 changes: 8 additions & 1 deletion mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ export interface Options {
includeServerUrls?: boolean;
}

export function escapeObjectKey(key: string): string {
if (/^[a-zA-Z_$][0-9a-zA-Z_$]*$/.test(key)) {
return key;
}
return `"${key}"`;
}

export function toSchemaType(
document: OpenAPI.Document,
schema?:
Expand Down Expand Up @@ -125,7 +132,7 @@ export function toSchemaType(
return `{${
Object.entries(schema.properties)
.map(([property, type]) =>
`${property}:${toSchemaType(document, type)}`
`${escapeObjectKey(property)}:${toSchemaType(document, type)}`
)
.join(";")
}}`;
Expand Down

0 comments on commit 5779444

Please sign in to comment.