Skip to content

Commit

Permalink
fix: add double quotes for special properties
Browse files Browse the repository at this point in the history
  • Loading branch information
aisensiy authored and fabien0102 committed Dec 11, 2019
1 parent 9b75814 commit 23f0e7f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/scripts/import-open-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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("; ");
}
Expand Down
25 changes: 25 additions & 0 deletions src/scripts/tests/import-open-api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down

0 comments on commit 23f0e7f

Please sign in to comment.