@@ -17,6 +17,7 @@ const basic = require("../schemas/basic.json");
17
17
const invalidFieldOne = require ( "../schemas/invalidFields/invalidField.json" ) ;
18
18
// null property type
19
19
const nullProperty = require ( "../schemas/nullProperties/nullProperty.json" ) ;
20
+ const nullAndTypeProperty = require ( "../schemas/nullProperties/nullAndType.json" ) ;
20
21
// array types
21
22
const arrayType = require ( "../schemas/arrayTypes/arrayType.json" ) ;
22
23
const arrayTypeWithNull = require ( "../schemas/arrayTypes/arrayTypeIncludingNull.json" ) ;
@@ -55,6 +56,8 @@ const listOfBannedSchemas = require("../schemas/SchemasThatCannotBeConverted/lis
55
56
// anyOf/oneOf Nulls
56
57
const oneOfNull = require ( "../schemas/ofNulls/oneOfNull.json" ) ;
57
58
const anyOfNull = require ( "../schemas/ofNulls/anyOfNull.json" ) ;
59
+ const moreThanoneOfNull = require ( "../schemas/ofNulls/moreThanOneoneOf.json" ) ;
60
+ const moreThananyOfNull = require ( "../schemas/ofNulls/moreThanOneanyOf.json" ) ;
58
61
// anyOf/oneOf Nulls
59
62
const allOfProperties = require ( "../schemas/propertiesOutsideOf/allOf.json" ) ;
60
63
const oneOfProperties = require ( "../schemas/propertiesOutsideOf/oneOf.json" ) ;
@@ -178,24 +181,24 @@ describe("Convertor", () => {
178
181
let valid = await validator . validateInner ( cloned , { } ) ;
179
182
expect ( valid ) . to . be . true ;
180
183
} ) ;
181
- } ) ;
182
184
183
- describe ( "arrays of types" , ( ) => {
184
- it ( "should convert properties that have an array of types to a oneOf" , async function ( ) {
185
- const newConvertor = new Convertor ( arrayType ) ;
185
+ it ( `should set types as nullable when null is provided along with a type` , async function ( ) {
186
+ const newConvertor = new Convertor ( nullAndTypeProperty ) ;
186
187
const result = newConvertor . convert ( "basic" ) ;
188
+
189
+ expect ( result . schemas . basic . properties . typedProperty ) . to . have . property (
190
+ "type"
191
+ ) ;
192
+ expect ( result . schemas . basic . properties . typedProperty ) . to . have . property (
193
+ "type" ,
194
+ "string"
195
+ ) ;
196
+ expect ( result . schemas . basic . properties . typedProperty ) . to . have . property (
197
+ "nullable"
198
+ ) ;
187
199
expect (
188
- result . schemas . basic . properties . arrayTypeProperty
189
- ) . to . not . have . property ( "type" ) ;
190
- expect (
191
- result . schemas . basic . properties . arrayTypeProperty
192
- ) . to . have . property ( "oneOf" ) ;
193
- expect (
194
- result . schemas . basic . properties . arrayTypeProperty . oneOf
195
- ) . to . be . an ( "array" ) ;
196
- expect (
197
- result . schemas . basic . properties . arrayTypeProperty . oneOf . length
198
- ) . to . be . equal ( 2 ) ;
200
+ result . schemas . basic . properties . typedProperty . nullable
201
+ ) . to . be . equal ( true ) ;
199
202
200
203
const cloned = JSON . parse ( JSON . stringify ( basicOpenAPI ) ) ;
201
204
Object . assign ( cloned , { components : result } ) ;
@@ -205,9 +208,11 @@ describe("Convertor", () => {
205
208
let valid = await validator . validateInner ( cloned , { } ) ;
206
209
expect ( valid ) . to . be . true ;
207
210
} ) ;
211
+ } ) ;
208
212
209
- it ( "should convert properties that have an array of types to a oneOf with null fields" , async function ( ) {
210
- const newConvertor = new Convertor ( arrayTypeWithNull ) ;
213
+ describe ( "arrays of types" , ( ) => {
214
+ it ( "should convert properties that have an array of types to a oneOf" , async function ( ) {
215
+ const newConvertor = new Convertor ( arrayType ) ;
211
216
const result = newConvertor . convert ( "basic" ) ;
212
217
expect (
213
218
result . schemas . basic . properties . arrayTypeProperty
@@ -220,13 +225,7 @@ describe("Convertor", () => {
220
225
) . to . be . an ( "array" ) ;
221
226
expect (
222
227
result . schemas . basic . properties . arrayTypeProperty . oneOf . length
223
- ) . to . be . equal ( 1 ) ;
224
- expect (
225
- result . schemas . basic . properties . arrayTypeProperty . oneOf [ 0 ] . type
226
- ) . to . be . equal ( "string" ) ;
227
- expect (
228
- result . schemas . basic . properties . arrayTypeProperty . oneOf [ 0 ] . nullable
229
- ) . to . be . equal ( true ) ;
228
+ ) . to . be . equal ( 2 ) ;
230
229
231
230
const cloned = JSON . parse ( JSON . stringify ( basicOpenAPI ) ) ;
232
231
Object . assign ( cloned , { components : result } ) ;
@@ -715,13 +714,18 @@ describe("Convertor", () => {
715
714
it ( "should convert an anyOf with a type of null" , async function ( ) {
716
715
const newConvertor = new Convertor ( anyOfNull ) ;
717
716
const result = newConvertor . convert ( "basic" ) ;
718
- expect ( result . schemas . basic . properties . payment ) . to . have . property (
717
+
718
+ expect ( result . schemas . basic . properties . payment ) . to . not . have . property (
719
719
"anyOf"
720
720
) ;
721
- expect ( result . schemas . basic . properties . payment . anyOf ) . to . be . an ( "array" ) ;
722
- expect (
723
- result . schemas . basic . properties . payment . anyOf . length
724
- ) . 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
+ ) ;
725
729
726
730
const cloned = JSON . parse ( JSON . stringify ( basicOpenAPI ) ) ;
727
731
Object . assign ( cloned , { components : result } ) ;
@@ -735,13 +739,78 @@ describe("Convertor", () => {
735
739
it ( "should convert a oneOf with a type of null" , async function ( ) {
736
740
const newConvertor = new Convertor ( oneOfNull ) ;
737
741
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" ) ;
738
797
expect ( result . schemas . basic . properties . payment ) . to . have . property (
739
798
"oneOf"
740
799
) ;
741
- expect ( result . schemas . basic . properties . payment . oneOf ) . to . be . an ( "array" ) ;
742
- expect (
743
- result . schemas . basic . properties . payment . oneOf . length
744
- ) . 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 ) ;
745
814
746
815
const cloned = JSON . parse ( JSON . stringify ( basicOpenAPI ) ) ;
747
816
Object . assign ( cloned , { components : result } ) ;
0 commit comments