Skip to content
This repository has been archived by the owner on Nov 11, 2023. It is now read-only.

Commit

Permalink
support required at root level
Browse files Browse the repository at this point in the history
  • Loading branch information
abonifacio authored and fabien0102 committed Jul 20, 2021
1 parent e18fa08 commit e179727
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
20 changes: 18 additions & 2 deletions src/scripts/import-open-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ export const getArray = (item: SchemaObject): string => {
return `${item_type}[]`;
};

const requireProperties = (type: string, toRequire: string[]) => {
return `Require<${type}, ${toRequire.map(property => `"${property}"`).join(" | ")}>`;
};

/**
* Return the output type from an object
*
Expand All @@ -127,11 +131,19 @@ export const getObject = (item: SchemaObject): string => {
}

if (item.allOf) {
return item.allOf.map(resolveValue).join(" & ");
const composedType = item.allOf.map(resolveValue).join(" & ");
if (item.required) {
return requireProperties(composedType, item.required);
}
return composedType;
}

if (item.oneOf) {
return item.oneOf.map(resolveValue).join(" | ");
const unionType = item.oneOf.map(resolveValue).join(" | ");
if (item.required) {
return requireProperties(unionType, item.required);
}
return unionType;
}

if (!item.type && !item.properties && !item.additionalProperties) {
Expand Down Expand Up @@ -882,6 +894,10 @@ import { ${imports.join(", ")} } from "restful-react";
outputHeaders += `\n${customImport}\n`;
}

if (output.match(/Require</)) {
outputHeaders += "\ntype Require<T,R extends keyof T> = T & Required<Pick<T, R>>;\n";
}

if (pathParametersEncodingMode) {
outputHeaders += `${getEncodingFunction(pathParametersEncodingMode)}
Expand Down
19 changes: 19 additions & 0 deletions src/scripts/tests/import-open-api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,24 @@ describe("scripts/import-open-api", () => {
`);
});

it("should deal with allOf and required at root level", () => {
const item = {
type: "object",
allOf: [{ $ref: "#/components/schemas/foo" }, { $ref: "#/components/schemas/bar" }],
required: ["a_bar_property", "a_foo_property"],
};
expect(getObject(item)).toEqual(`Require<Foo & Bar, "a_bar_property" | "a_foo_property">`);
});

it("should deal with oneOf and required at root level", () => {
const item = {
type: "object",
oneOf: [{ $ref: "#/components/schemas/foo" }, { $ref: "#/components/schemas/bar" }],
required: ["a_common_property"],
};
expect(getObject(item)).toEqual(`Require<Foo | Bar, "a_common_property">`);
});

it("should deal with oneOf with null", () => {
const item = {
type: "object",
Expand All @@ -364,6 +382,7 @@ describe("scripts/import-open-api", () => {
};
expect(getObject(item)).toMatchInlineSnapshot(`"Foo | null"`);
});

it("should handle empty properties (1)", () => {
const item = {
properties: {},
Expand Down

0 comments on commit e179727

Please sign in to comment.