diff --git a/src/scripts/import-open-api.ts b/src/scripts/import-open-api.ts index 8c509100..e4fa31bf 100644 --- a/src/scripts/import-open-api.ts +++ b/src/scripts/import-open-api.ts @@ -128,12 +128,14 @@ export const getObject = (item: SchemaObject): string => { } // Consolidation of item.properties & item.additionalProperties + const IdentifierRegexp = /^[a-zA-Z_\$][a-zA-Z0-9_\$]*$/ let output = "{"; if (item.properties) { output += Object.entries(item.properties) .map(([key, prop]: [string, ReferenceObject | SchemaObject]) => { const isRequired = (item.required || []).includes(key); - return `${key}${isRequired ? "" : "?"}: ${resolveValue(prop)}`; + const processedKey = (IdentifierRegexp.test(key) ? key : `"${key}"`) + return `${processedKey}${isRequired ? "" : "?"}: ${resolveValue(prop)}`; }) .join("; "); } diff --git a/src/scripts/tests/import-open-api.test.ts b/src/scripts/tests/import-open-api.test.ts index 3abb309d..87a7d334 100644 --- a/src/scripts/tests/import-open-api.test.ts +++ b/src/scripts/tests/import-open-api.test.ts @@ -505,6 +505,31 @@ describe("scripts/import-open-api", () => { ); }); + it("should give double quotes for special properties", () => { + const responses: ComponentsObject["responses"] = { + JobRun: { + description: "Job is starting", + content: { + "application/json": { + schema: { + type: "object", + properties: { + "execution-id": { + description: "ID of the job execution", + type: "string", + }, + }, + }, + }, + }, + }, + }; + + expect(generateResponsesDefinition(responses)).toContain( + `export interface JobRunResponse {"execution-id"?: string}`, + ); + }); + it("should declare a a type for composed object", () => { const responses: ComponentsObject["responses"] = { JobRun: {