@@ -33,17 +33,17 @@ public static OpenApiSchema CreateEdmTypeSchema(this ODataContext context, IEdmT
3333 Utils . CheckArgumentNull ( edmTypeReference , nameof ( edmTypeReference ) ) ;
3434
3535 switch ( edmTypeReference . TypeKind ( ) )
36- {
37- // Collection-valued structural and navigation are represented as Schema Objects of type array.
38- // The value of the items keyword is a Schema Object specifying the type of the items.
36+ {
37+ // Collection-valued structural and navigation are represented as Schema Objects of type array.
38+ // The value of the items keyword is a Schema Object specifying the type of the items.
3939 case EdmTypeKind . Collection :
4040
4141 IEdmTypeReference typeRef = edmTypeReference . AsCollection ( ) . ElementType ( ) ;
4242 OpenApiSchema schema ;
43- schema = typeRef . TypeKind ( ) == EdmTypeKind . Complex || typeRef . TypeKind ( ) == EdmTypeKind . Entity
44- ? context . CreateStructuredTypeSchema ( typeRef . AsStructured ( ) , true )
45- : context . CreateEdmTypeSchema ( typeRef ) ;
46-
43+ schema = typeRef . TypeKind ( ) == EdmTypeKind . Complex || typeRef . TypeKind ( ) == EdmTypeKind . Entity
44+ ? context . CreateStructuredTypeSchema ( typeRef . AsStructured ( ) , true )
45+ : context . CreateEdmTypeSchema ( typeRef ) ;
46+
4747 return new OpenApiSchema
4848 {
4949 Type = "array" ,
@@ -133,7 +133,8 @@ public static OpenApiSchema CreateSchema(this ODataContext context, IEdmPrimitiv
133133 }
134134
135135 // Nullable properties are marked with the keyword nullable and a value of true.
136- schema . Nullable = primitiveType . IsNullable ? true : false ;
136+ // nullable cannot be true when type is empty, often common in anyof/allOf since individual entries are nullable
137+ schema . Nullable = ! string . IsNullOrEmpty ( schema . Type ) && primitiveType . IsNullable ;
137138 }
138139
139140 return schema ;
@@ -169,7 +170,7 @@ public static OpenApiSchema CreateSchema(this ODataContext context, IEdmPrimitiv
169170 schema . Default = new OpenApiBoolean ( false ) ;
170171 break ;
171172 case EdmPrimitiveTypeKind . Byte : // byte
172- schema . Type = Constants . IntegerType ;
173+ schema . Type = Constants . NumberType ;
173174 schema . Format = "uint8" ;
174175 break ;
175176 case EdmPrimitiveTypeKind . DateTimeOffset : // datetime offset
@@ -182,8 +183,8 @@ public static OpenApiSchema CreateSchema(this ODataContext context, IEdmPrimitiv
182183 {
183184 schema . OneOf = new List < OpenApiSchema >
184185 {
185- new OpenApiSchema { Type = Constants . NumberType , Format = Constants . DecimalFormat } ,
186- new OpenApiSchema { Type = Constants . StringType } ,
186+ new OpenApiSchema { Type = Constants . NumberType , Format = Constants . DecimalFormat , Nullable = true } ,
187+ new OpenApiSchema { Type = Constants . StringType , Nullable = true } ,
187188 } ;
188189 }
189190 else
@@ -195,8 +196,8 @@ public static OpenApiSchema CreateSchema(this ODataContext context, IEdmPrimitiv
195196 case EdmPrimitiveTypeKind . Double : // double
196197 schema . OneOf = new List < OpenApiSchema >
197198 {
198- new OpenApiSchema { Type = Constants . NumberType , Format = "double" } ,
199- new OpenApiSchema { Type = Constants . StringType } ,
199+ new OpenApiSchema { Type = Constants . NumberType , Format = "double" , Nullable = true } ,
200+ new OpenApiSchema { Type = Constants . StringType , Nullable = true } ,
200201 new OpenApiSchema
201202 {
202203 UnresolvedReference = true ,
@@ -211,8 +212,8 @@ public static OpenApiSchema CreateSchema(this ODataContext context, IEdmPrimitiv
211212 case EdmPrimitiveTypeKind . Single : // single
212213 schema . OneOf = new List < OpenApiSchema >
213214 {
214- new OpenApiSchema { Type = Constants . NumberType , Format = "float" } ,
215- new OpenApiSchema { Type = Constants . StringType } ,
215+ new OpenApiSchema { Type = Constants . NumberType , Format = "float" , Nullable = true } ,
216+ new OpenApiSchema { Type = Constants . StringType , Nullable = true } ,
216217 new OpenApiSchema
217218 {
218219 UnresolvedReference = true ,
@@ -230,13 +231,13 @@ public static OpenApiSchema CreateSchema(this ODataContext context, IEdmPrimitiv
230231 schema . Pattern = "^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$" ;
231232 break ;
232233 case EdmPrimitiveTypeKind . Int16 :
233- schema . Type = Constants . IntegerType ;
234+ schema . Type = Constants . NumberType ;
234235 schema . Format = "int16" ;
235236 schema . Minimum = Int16 . MinValue ; // -32768
236237 schema . Maximum = Int16 . MaxValue ; // 32767
237238 break ;
238239 case EdmPrimitiveTypeKind . Int32 :
239- schema . Type = Constants . IntegerType ;
240+ schema . Type = Constants . NumberType ;
240241 schema . Format = "int32" ;
241242 schema . Minimum = Int32 . MinValue ; // -2147483648
242243 schema . Maximum = Int32 . MaxValue ; // 2147483647
@@ -246,18 +247,18 @@ public static OpenApiSchema CreateSchema(this ODataContext context, IEdmPrimitiv
246247 {
247248 schema . OneOf = new List < OpenApiSchema >
248249 {
249- new OpenApiSchema { Type = Constants . IntegerType , Format = Constants . Int64Format } ,
250- new OpenApiSchema { Type = Constants . StringType }
250+ new OpenApiSchema { Type = Constants . NumberType , Format = Constants . Int64Format , Nullable = true } ,
251+ new OpenApiSchema { Type = Constants . StringType , Nullable = true }
251252 } ;
252253 }
253254 else
254255 {
255- schema . Type = Constants . IntegerType ;
256+ schema . Type = Constants . NumberType ;
256257 schema . Format = Constants . Int64Format ;
257258 }
258259 break ;
259260 case EdmPrimitiveTypeKind . SByte :
260- schema . Type = Constants . IntegerType ;
261+ schema . Type = Constants . NumberType ;
261262 schema . Format = "int8" ;
262263 schema . Minimum = SByte . MinValue ; // -128
263264 schema . Maximum = SByte . MaxValue ; // 127
@@ -469,47 +470,47 @@ private static OpenApiSchema CreateEnumTypeSchema(this ODataContext context, IEd
469470 private static OpenApiSchema CreateStructuredTypeSchema ( this ODataContext context , IEdmStructuredTypeReference typeReference , bool isTypeCollection = false )
470471 {
471472 Debug . Assert ( context != null ) ;
472- Debug . Assert ( typeReference != null ) ;
473-
474- OpenApiSchema schema = new OpenApiSchema ( ) ;
475-
476- // AnyOf will only be valid openApi for version 3
477- // otherwise the reference should be set directly
478- // as per OASIS documentation for openApi version 2
479- // Collections of structured types cannot be nullable
480- if ( typeReference . IsNullable && ! isTypeCollection &&
481- ( context . Settings . OpenApiSpecVersion >= OpenApiSpecVersion . OpenApi3_0 ) )
482- {
483- schema . Reference = null ;
484- schema . AnyOf = new List < OpenApiSchema >
485- {
486- new OpenApiSchema
487- {
488- UnresolvedReference = true ,
489- Reference = new OpenApiReference
490- {
491- Type = ReferenceType . Schema ,
492- Id = typeReference . Definition . FullTypeName ( )
493- }
494- } ,
495- new OpenApiSchema
496- {
497- Type = "object" ,
498- Nullable = true
499- }
500- } ;
501- }
502- else
503- {
504- schema . Type = null ;
505- schema . AnyOf = null ;
506- schema . Reference = new OpenApiReference
507- {
508- Type = ReferenceType . Schema ,
509- Id = typeReference . Definition . FullTypeName ( )
510- } ;
511- schema . UnresolvedReference = true ;
512- schema . Nullable = typeReference . IsNullable ;
473+ Debug . Assert ( typeReference != null ) ;
474+
475+ OpenApiSchema schema = new OpenApiSchema ( ) ;
476+
477+ // AnyOf will only be valid openApi for version 3
478+ // otherwise the reference should be set directly
479+ // as per OASIS documentation for openApi version 2
480+ // Collections of structured types cannot be nullable
481+ if ( typeReference . IsNullable && ! isTypeCollection &&
482+ ( context . Settings . OpenApiSpecVersion >= OpenApiSpecVersion . OpenApi3_0 ) )
483+ {
484+ schema . Reference = null ;
485+ schema . AnyOf = new List < OpenApiSchema >
486+ {
487+ new OpenApiSchema
488+ {
489+ UnresolvedReference = true ,
490+ Reference = new OpenApiReference
491+ {
492+ Type = ReferenceType . Schema ,
493+ Id = typeReference . Definition . FullTypeName ( )
494+ }
495+ } ,
496+ new OpenApiSchema
497+ {
498+ Type = "object" ,
499+ Nullable = true
500+ }
501+ } ;
502+ }
503+ else
504+ {
505+ schema . Type = null ;
506+ schema . AnyOf = null ;
507+ schema . Reference = new OpenApiReference
508+ {
509+ Type = ReferenceType . Schema ,
510+ Id = typeReference . Definition . FullTypeName ( )
511+ } ;
512+ schema . UnresolvedReference = true ;
513+ schema . Nullable = typeReference . IsNullable ;
513514 }
514515
515516 return schema ;
0 commit comments