Skip to content

Commit

Permalink
cli: Fixed generation for schema with nullable and allOf
Browse files Browse the repository at this point in the history
  • Loading branch information
Fredx87 committed Feb 3, 2022
1 parent fe95f90 commit 39494f5
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/smooth-guests-repair.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@openapi-io-ts/cli": patch
---

Fixed generation for schema with nullable and allOf
20 changes: 20 additions & 0 deletions packages/cli/src/parser/__tests__/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,4 +222,24 @@ describe("OpenAPI schema", () => {

expect(result).toEqual(E.right(expected));
});

it("parses a nullable schema with allOf", () => {
const schema: OpenAPIV3.SchemaObject = {
nullable: true,
allOf: [
{
type: "array",
items: { type: "string" },
},
],
};

const result = parseSchema(schema);
const expected = gen.unionCombinator([
gen.arrayCombinator(gen.stringType),
gen.nullType,
]);

expect(result).toEqual(E.right(expected));
});
});
15 changes: 13 additions & 2 deletions packages/cli/src/parser/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ export function parseSchema(
return parseJsonReference(schema.$ref);
}

if (schema.nullable && schema.allOf) {
return pipe(
parseAllOf(schema.allOf),
E.map((allOf) => gen.unionCombinator([allOf, gen.nullType]))
);
}

return pipe(
parseBaseSchema(schema),
E.map((baseSchema) =>
Expand Down Expand Up @@ -76,7 +83,9 @@ function parseAllOf(
): E.Either<Error, gen.TypeReference> {
return pipe(
parseSchemas(schemas),
E.map((schemas) => gen.intersectionCombinator(schemas))
E.map((schemas) =>
schemas.length === 1 ? schemas[0] : gen.intersectionCombinator(schemas)
)
);
}

Expand All @@ -85,7 +94,9 @@ function parseOneOf(
): E.Either<Error, gen.TypeReference> {
return pipe(
parseSchemas(schemas),
E.map((schemas) => gen.unionCombinator(schemas))
E.map((schemas) =>
schemas.length === 1 ? schemas[0] : gen.unionCombinator(schemas)
)
);
}

Expand Down

0 comments on commit 39494f5

Please sign in to comment.