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

Commit

Permalink
Merge pull request #374 from contiamo/all-of-one-of-root-with-empty-r…
Browse files Browse the repository at this point in the history
…equired

Fix: empty required should not generate invalid types.
  • Loading branch information
micha-f authored Sep 23, 2021
2 parents 15f04ca + adc09c4 commit a86ef29
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/scripts/import-open-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ export const getObject = (item: SchemaObject): string => {

if (item.allOf) {
const composedType = item.allOf.map(resolveValue).join(" & ");
if (item.required) {
if (item.required && item.required.length) {
return requireProperties(composedType, item.required);
}
return composedType;
Expand All @@ -144,7 +144,7 @@ export const getObject = (item: SchemaObject): string => {

if (item.oneOf) {
const unionType = item.oneOf.map(resolveValue).join(" | ");
if (item.required) {
if (item.required && item.required.length) {
return requireProperties(unionType, item.required);
}
return unionType;
Expand Down
18 changes: 18 additions & 0 deletions src/scripts/tests/import-open-api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,15 @@ describe("scripts/import-open-api", () => {
expect(getObject(item)).toEqual(`Require<Foo & Bar, "a_bar_property" | "a_foo_property">`);
});

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

it("should deal with oneOf and required at root level", () => {
const item = {
type: "object",
Expand All @@ -410,6 +419,15 @@ describe("scripts/import-open-api", () => {
expect(getObject(item)).toEqual(`Require<Foo | Bar, "a_common_property">`);
});

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

it("should deal with oneOf with null", () => {
const item = {
type: "object",
Expand Down

0 comments on commit a86ef29

Please sign in to comment.