Skip to content

Commit 4c6642e

Browse files
committed
update tests for nullable anyOf and oneOfs
1 parent 96bc6d8 commit 4c6642e

File tree

1 file changed

+81
-9
lines changed

1 file changed

+81
-9
lines changed

test/src/Convertor.spec.js

Lines changed: 81 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ const listOfBannedSchemas = require("../schemas/SchemasThatCannotBeConverted/lis
5656
// anyOf/oneOf Nulls
5757
const oneOfNull = require("../schemas/ofNulls/oneOfNull.json");
5858
const anyOfNull = require("../schemas/ofNulls/anyOfNull.json");
59+
const moreThanoneOfNull = require("../schemas/ofNulls/moreThanOneoneOf.json");
60+
const moreThananyOfNull = require("../schemas/ofNulls/moreThanOneanyOf.json");
5961
// anyOf/oneOf Nulls
6062
const allOfProperties = require("../schemas/propertiesOutsideOf/allOf.json");
6163
const oneOfProperties = require("../schemas/propertiesOutsideOf/oneOf.json");
@@ -712,13 +714,18 @@ describe("Convertor", () => {
712714
it("should convert an anyOf with a type of null", async function () {
713715
const newConvertor = new Convertor(anyOfNull);
714716
const result = newConvertor.convert("basic");
715-
expect(result.schemas.basic.properties.payment).to.have.property(
717+
718+
expect(result.schemas.basic.properties.payment).to.not.have.property(
716719
"anyOf"
717720
);
718-
expect(result.schemas.basic.properties.payment.anyOf).to.be.an("array");
719-
expect(
720-
result.schemas.basic.properties.payment.anyOf.length
721-
).to.be.equal(1);
721+
expect(result.schemas.basic.properties.payment).to.have.property(
722+
"type",
723+
"string"
724+
);
725+
expect(result.schemas.basic.properties.payment).to.have.property(
726+
"nullable",
727+
true
728+
);
722729

723730
const cloned = JSON.parse(JSON.stringify(basicOpenAPI));
724731
Object.assign(cloned, { components: result });
@@ -732,13 +739,78 @@ describe("Convertor", () => {
732739
it("should convert a oneOf with a type of null", async function () {
733740
const newConvertor = new Convertor(oneOfNull);
734741
const result = newConvertor.convert("basic");
742+
expect(result.schemas.basic.properties.payment).to.not.have.property(
743+
"oneOf"
744+
);
745+
expect(result.schemas.basic.properties.payment).to.have.property(
746+
"type",
747+
"string"
748+
);
749+
expect(result.schemas.basic.properties.payment).to.have.property(
750+
"nullable",
751+
true
752+
);
753+
754+
const cloned = JSON.parse(JSON.stringify(basicOpenAPI));
755+
Object.assign(cloned, { components: result });
756+
expect(cloned).to.have.property("components");
757+
expect(cloned.components).to.have.property("schemas");
758+
expect(cloned.components.schemas).to.have.property("basic");
759+
let valid = await validator.validateInner(cloned, {});
760+
expect(valid).to.be.true;
761+
});
762+
763+
it("should convert an anyOf with a type of null and more than one non null type", async function () {
764+
const newConvertor = new Convertor(moreThananyOfNull);
765+
const result = newConvertor.convert("basic");
766+
767+
expect(result.schemas.basic.properties.payment).to.have.property(
768+
"anyOf"
769+
);
770+
expect(result.schemas.basic.properties.payment.anyOf).to.have.lengthOf(
771+
2
772+
);
773+
const stringAnyOf =
774+
result.schemas.basic.properties.payment.anyOf.filter(
775+
(schema) => schema.type === "string"
776+
);
777+
expect(stringAnyOf[0]).to.have.property("nullable", true);
778+
779+
const integerAnyOf =
780+
result.schemas.basic.properties.payment.anyOf.filter(
781+
(schema) => schema.type === "integer"
782+
);
783+
expect(integerAnyOf[0]).to.have.property("nullable", true);
784+
785+
const cloned = JSON.parse(JSON.stringify(basicOpenAPI));
786+
Object.assign(cloned, { components: result });
787+
expect(cloned).to.have.property("components");
788+
expect(cloned.components).to.have.property("schemas");
789+
expect(cloned.components.schemas).to.have.property("basic");
790+
let valid = await validator.validateInner(cloned, {});
791+
expect(valid).to.be.true;
792+
});
793+
794+
it("should convert a oneOf with a type of null and more than one non null type", async function () {
795+
const newConvertor = new Convertor(moreThanoneOfNull);
796+
const result = newConvertor.convert("basic");
735797
expect(result.schemas.basic.properties.payment).to.have.property(
736798
"oneOf"
737799
);
738-
expect(result.schemas.basic.properties.payment.oneOf).to.be.an("array");
739-
expect(
740-
result.schemas.basic.properties.payment.oneOf.length
741-
).to.be.equal(1);
800+
expect(result.schemas.basic.properties.payment.oneOf).to.have.lengthOf(
801+
2
802+
);
803+
const stringOneOf =
804+
result.schemas.basic.properties.payment.oneOf.filter(
805+
(schema) => schema.type === "string"
806+
);
807+
expect(stringOneOf[0]).to.have.property("nullable", true);
808+
809+
const booleanOneOf =
810+
result.schemas.basic.properties.payment.oneOf.filter(
811+
(schema) => schema.type === "boolean"
812+
);
813+
expect(booleanOneOf[0]).to.have.property("nullable", true);
742814

743815
const cloned = JSON.parse(JSON.stringify(basicOpenAPI));
744816
Object.assign(cloned, { components: result });

0 commit comments

Comments
 (0)