Skip to content

Commit 95058a2

Browse files
committed
fix: do not emit integer as it's invalid OAS
Signed-off-by: Vincent Biret <[email protected]>
1 parent 83aaf31 commit 95058a2

File tree

4 files changed

+27
-22
lines changed

4 files changed

+27
-22
lines changed

src/Microsoft.OpenApi.OData.Reader/Common/Constants.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information.
44
// ------------------------------------------------------------
55

6+
using System;
7+
68
namespace Microsoft.OpenApi.OData.Common
79
{
810
/// <summary>
@@ -168,6 +170,7 @@ internal static class Constants
168170
/// <summary>
169171
/// integer type
170172
/// </summary>
173+
[Obsolete("integer is not a valid OpenAPI type. Use number instead.")]
171174
public static string IntegerType = "integer";
172175

173176
/// <summary>

src/Microsoft.OpenApi.OData.Reader/Generator/OpenApiEdmTypeSchemaGenerator.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ public static OpenApiSchema CreateSchema(this ODataContext context, IEdmPrimitiv
170170
schema.Default = new OpenApiBoolean(false);
171171
break;
172172
case EdmPrimitiveTypeKind.Byte: // byte
173-
schema.Type = Constants.IntegerType;
173+
schema.Type = Constants.NumberType;
174174
schema.Format = "uint8";
175175
break;
176176
case EdmPrimitiveTypeKind.DateTimeOffset: // datetime offset
@@ -231,13 +231,13 @@ public static OpenApiSchema CreateSchema(this ODataContext context, IEdmPrimitiv
231231
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}$";
232232
break;
233233
case EdmPrimitiveTypeKind.Int16:
234-
schema.Type = Constants.IntegerType;
234+
schema.Type = Constants.NumberType;
235235
schema.Format = "int16";
236236
schema.Minimum = Int16.MinValue; // -32768
237237
schema.Maximum = Int16.MaxValue; // 32767
238238
break;
239239
case EdmPrimitiveTypeKind.Int32:
240-
schema.Type = Constants.IntegerType;
240+
schema.Type = Constants.NumberType;
241241
schema.Format = "int32";
242242
schema.Minimum = Int32.MinValue; // -2147483648
243243
schema.Maximum = Int32.MaxValue; // 2147483647
@@ -247,18 +247,18 @@ public static OpenApiSchema CreateSchema(this ODataContext context, IEdmPrimitiv
247247
{
248248
schema.OneOf = new List<OpenApiSchema>
249249
{
250-
new OpenApiSchema { Type = Constants.IntegerType, Format = Constants.Int64Format, Nullable = true },
250+
new OpenApiSchema { Type = Constants.NumberType, Format = Constants.Int64Format, Nullable = true },
251251
new OpenApiSchema { Type = Constants.StringType, Nullable = true }
252252
};
253253
}
254254
else
255255
{
256-
schema.Type = Constants.IntegerType;
256+
schema.Type = Constants.NumberType;
257257
schema.Format = Constants.Int64Format;
258258
}
259259
break;
260260
case EdmPrimitiveTypeKind.SByte:
261-
schema.Type = Constants.IntegerType;
261+
schema.Type = Constants.NumberType;
262262
schema.Format = "int8";
263263
schema.Minimum = SByte.MinValue; // -128
264264
schema.Maximum = SByte.MaxValue; // 127

test/Microsoft.OpenAPI.OData.Reader.Tests/Generator/OpenApiEdmTypeSchemaGeneratorTest.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ public void CreateEdmTypeSchemaReturnSchemaForNullableCollectionPrimitiveType()
151151
""items"": {
152152
""maximum"": 2147483647,
153153
""minimum"": -2147483648,
154-
""type"": ""integer"",
154+
""type"": ""number"",
155155
""format"": ""int32"",
156156
""nullable"": true
157157
}
@@ -358,7 +358,7 @@ public void CreateEdmTypeSchemaReturnSchemaForInt32(bool isNullable)
358358
Assert.Equal(@"{
359359
""maximum"": 2147483647,
360360
""minimum"": -2147483648,
361-
""type"": ""integer"",
361+
""type"": ""number"",
362362
""format"": ""int32"",
363363
""nullable"": true
364364
}".ChangeLineBreaks(), json);
@@ -368,7 +368,7 @@ public void CreateEdmTypeSchemaReturnSchemaForInt32(bool isNullable)
368368
Assert.Equal(@"{
369369
""maximum"": 2147483647,
370370
""minimum"": -2147483648,
371-
""type"": ""integer"",
371+
""type"": ""number"",
372372
""format"": ""int32""
373373
}".ChangeLineBreaks(), json);
374374
}
@@ -444,17 +444,17 @@ public void CreateEdmTypeSchemaReturnSchemaForInt64(bool isNullable, bool IEEE75
444444
Assert.Null(schema.Type);
445445
Assert.NotNull(schema.OneOf);
446446
Assert.Equal(2, schema.OneOf.Count);
447-
var integerSchema = schema.OneOf.FirstOrDefault(x => x.Type.Equals("integer", StringComparison.OrdinalIgnoreCase));
448-
Assert.NotNull(integerSchema);
449-
Assert.True(integerSchema.Nullable);
447+
var numberSchema = schema.OneOf.FirstOrDefault(x => x.Type.Equals("number", StringComparison.OrdinalIgnoreCase));
448+
Assert.NotNull(numberSchema);
449+
Assert.True(numberSchema.Nullable);
450450
var stringSchema = schema.OneOf.FirstOrDefault(x => x.Type.Equals("string", StringComparison.OrdinalIgnoreCase));
451451
Assert.NotNull(stringSchema);
452452
Assert.True(stringSchema.Nullable);
453453
Assert.False(schema.Nullable);
454454
}
455455
else
456456
{
457-
Assert.Equal("integer", schema.Type);
457+
Assert.Equal("number", schema.Type);
458458
Assert.Null(schema.AnyOf);
459459
Assert.Equal(isNullable, schema.Nullable);
460460
}

test/Microsoft.OpenAPI.OData.Reader.Tests/Generator/OpenApiSchemaGeneratorTests.cs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -551,10 +551,12 @@ public void CreateComplexTypeWithBaseSchemaReturnCorrectSchema()
551551
""oneOf"": [
552552
{
553553
""type"": ""number"",
554-
""format"": ""decimal""
554+
""format"": ""decimal"",
555+
""nullable"": true
555556
},
556557
{
557-
""type"": ""string""
558+
""type"": ""string"",
559+
""nullable"": true
558560
}
559561
]
560562
}
@@ -614,7 +616,7 @@ public void CreateEntityTypeWithoutBaseSchemaReturnCorrectSchema()
614616
""Id"": {
615617
""maximum"": 2147483647,
616618
""minimum"": -2147483648,
617-
""type"": ""integer"",
619+
""type"": ""number"",
618620
""format"": ""int32""
619621
},
620622
""Creatures"": {
@@ -627,7 +629,7 @@ public void CreateEntityTypeWithoutBaseSchemaReturnCorrectSchema()
627629
},
628630
""description"": ""Entity type 'Zoo' description."",
629631
""example"": {
630-
""Id"": ""integer (identifier)"",
632+
""Id"": ""number (identifier)"",
631633
""Creatures"": [
632634
{
633635
""@odata.type"": ""NS.Creature""
@@ -705,8 +707,8 @@ public void CreateEntityTypeWithBaseSchemaReturnCorrectSchema()
705707
}
706708
],
707709
""example"": {
708-
""Id"": ""integer (identifier)"",
709-
""Age"": ""integer"",
710+
""Id"": ""number (identifier)"",
711+
""Age"": ""number"",
710712
""Name"": ""string""
711713
}
712714
}"
@@ -753,7 +755,7 @@ public void CreateEntityTypeWithCrossReferenceBaseSchemaReturnCorrectSchema()
753755
Assert.Single(declaredSchema.Properties);
754756
var property = Assert.Single(declaredSchema.Properties);
755757
Assert.Equal("Extra", property.Key);
756-
Assert.Equal("integer", property.Value.Type);
758+
Assert.Equal("number", property.Value.Type);
757759
Assert.Null(property.Value.OneOf);
758760

759761
Assert.Equal("Customer", declaredSchema.Title);
@@ -1071,13 +1073,13 @@ public void NonNullableIntegerPropertyWithDefaultValueWorks()
10711073

10721074
// Assert
10731075
Assert.NotNull(schema);
1074-
Assert.Equal("integer", schema.Type);
1076+
Assert.Equal("number", schema.Type);
10751077

10761078
string json = schema.SerializeAsJson(OpenApiSpecVersion.OpenApi3_0);
10771079
Assert.Equal(@"{
10781080
""maximum"": 2147483647,
10791081
""minimum"": -2147483648,
1080-
""type"": ""integer"",
1082+
""type"": ""number"",
10811083
""format"": ""int32"",
10821084
""default"": -128
10831085
}".ChangeLineBreaks(), json);

0 commit comments

Comments
 (0)