@@ -33,17 +33,17 @@ public static OpenApiSchema CreateEdmTypeSchema(this ODataContext context, IEdmT
33
33
Utils . CheckArgumentNull ( edmTypeReference , nameof ( edmTypeReference ) ) ;
34
34
35
35
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.
39
39
case EdmTypeKind . Collection :
40
40
41
41
IEdmTypeReference typeRef = edmTypeReference . AsCollection ( ) . ElementType ( ) ;
42
42
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
+
47
47
return new OpenApiSchema
48
48
{
49
49
Type = "array" ,
@@ -133,7 +133,8 @@ public static OpenApiSchema CreateSchema(this ODataContext context, IEdmPrimitiv
133
133
}
134
134
135
135
// 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 ;
137
138
}
138
139
139
140
return schema ;
@@ -169,7 +170,7 @@ public static OpenApiSchema CreateSchema(this ODataContext context, IEdmPrimitiv
169
170
schema . Default = new OpenApiBoolean ( false ) ;
170
171
break ;
171
172
case EdmPrimitiveTypeKind . Byte : // byte
172
- schema . Type = Constants . IntegerType ;
173
+ schema . Type = Constants . NumberType ;
173
174
schema . Format = "uint8" ;
174
175
break ;
175
176
case EdmPrimitiveTypeKind . DateTimeOffset : // datetime offset
@@ -182,8 +183,8 @@ public static OpenApiSchema CreateSchema(this ODataContext context, IEdmPrimitiv
182
183
{
183
184
schema . OneOf = new List < OpenApiSchema >
184
185
{
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 } ,
187
188
} ;
188
189
}
189
190
else
@@ -211,8 +212,8 @@ public static OpenApiSchema CreateSchema(this ODataContext context, IEdmPrimitiv
211
212
case EdmPrimitiveTypeKind . Single : // single
212
213
schema . OneOf = new List < OpenApiSchema >
213
214
{
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 } ,
216
217
new OpenApiSchema
217
218
{
218
219
UnresolvedReference = true ,
@@ -230,13 +231,13 @@ public static OpenApiSchema CreateSchema(this ODataContext context, IEdmPrimitiv
230
231
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}$" ;
231
232
break ;
232
233
case EdmPrimitiveTypeKind . Int16 :
233
- schema . Type = Constants . IntegerType ;
234
+ schema . Type = Constants . NumberType ;
234
235
schema . Format = "int16" ;
235
236
schema . Minimum = Int16 . MinValue ; // -32768
236
237
schema . Maximum = Int16 . MaxValue ; // 32767
237
238
break ;
238
239
case EdmPrimitiveTypeKind . Int32 :
239
- schema . Type = Constants . IntegerType ;
240
+ schema . Type = Constants . NumberType ;
240
241
schema . Format = "int32" ;
241
242
schema . Minimum = Int32 . MinValue ; // -2147483648
242
243
schema . Maximum = Int32 . MaxValue ; // 2147483647
@@ -246,18 +247,18 @@ public static OpenApiSchema CreateSchema(this ODataContext context, IEdmPrimitiv
246
247
{
247
248
schema . OneOf = new List < OpenApiSchema >
248
249
{
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 }
251
252
} ;
252
253
}
253
254
else
254
255
{
255
- schema . Type = Constants . IntegerType ;
256
+ schema . Type = Constants . NumberType ;
256
257
schema . Format = Constants . Int64Format ;
257
258
}
258
259
break ;
259
260
case EdmPrimitiveTypeKind . SByte :
260
- schema . Type = Constants . IntegerType ;
261
+ schema . Type = Constants . NumberType ;
261
262
schema . Format = "int8" ;
262
263
schema . Minimum = SByte . MinValue ; // -128
263
264
schema . Maximum = SByte . MaxValue ; // 127
@@ -469,47 +470,47 @@ private static OpenApiSchema CreateEnumTypeSchema(this ODataContext context, IEd
469
470
private static OpenApiSchema CreateStructuredTypeSchema ( this ODataContext context , IEdmStructuredTypeReference typeReference , bool isTypeCollection = false )
470
471
{
471
472
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 ;
513
514
}
514
515
515
516
return schema ;
0 commit comments