Skip to content

Commit

Permalink
Merge pull request #5557 from microsoft/andrueastman/odataPrimitives
Browse files Browse the repository at this point in the history
fixes incompatibility with new odata primitive format.
  • Loading branch information
andrueastman authored Oct 8, 2024
2 parents 338b5a6 + 266227f commit dc20236
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed

- Fixed incompatibility with detecting odata primitives after conversion library updates at [OpenAPI.NET.OData#581](https://github.com/microsoft/OpenAPI.NET.OData/issues/581);
- Fixed cyclic dependencies in generated Go code. [#2834](https://github.com/microsoft/kiota/issues/2834)
- Fixed a bug where default output folder is created on plugin edit and generate commands. [#5510](https://github.com/microsoft/kiota/issues/5429)

Expand Down
18 changes: 17 additions & 1 deletion src/Kiota.Builder/Extensions/OpenApiSchemaExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public static bool IsExclusiveUnion(this OpenApiSchema? schema)
"number",
"integer",
};
public static bool IsODataPrimitiveType(this OpenApiSchema schema)
private static bool IsODataPrimitiveTypeBackwardCompatible(this OpenApiSchema schema)
{
return schema.IsExclusiveUnion() &&
schema.OneOf.Count == 3 &&
Expand All @@ -146,6 +146,22 @@ public static bool IsODataPrimitiveType(this OpenApiSchema schema)
schema.AnyOf.Count(static x => oDataTypes.Contains(x.Type)) == 1 &&
schema.AnyOf.Count(static x => "string".Equals(x.Type, StringComparison.OrdinalIgnoreCase)) == 1;
}
public static bool IsODataPrimitiveType(this OpenApiSchema schema)
{
return schema.IsExclusiveUnion() &&
schema.OneOf.Count == 3 &&
schema.OneOf.Count(static x => "string".Equals(x.Type, StringComparison.OrdinalIgnoreCase) && (x.Enum?.Any() ?? false)) == 1 &&
schema.OneOf.Count(static x => oDataTypes.Contains(x.Type)) == 1 &&
schema.OneOf.Count(static x => "string".Equals(x.Type, StringComparison.OrdinalIgnoreCase)) == 2
||
schema.IsInclusiveUnion() &&
schema.AnyOf.Count == 3 &&
schema.AnyOf.Count(static x => "string".Equals(x.Type, StringComparison.OrdinalIgnoreCase) && (x.Enum?.Any() ?? false)) == 1 &&
schema.AnyOf.Count(static x => oDataTypes.Contains(x.Type)) == 1 &&
schema.AnyOf.Count(static x => "string".Equals(x.Type, StringComparison.OrdinalIgnoreCase)) == 2
||
schema.IsODataPrimitiveTypeBackwardCompatible();
}
public static bool IsEnum(this OpenApiSchema schema)
{
if (schema is null) return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -785,4 +785,66 @@ public void IsEnumDoesNotMaskUnions()
};
Assert.False(schema.IsEnum());
}
[Fact]
public void IsOdataPrimitive()
{
var schema = new OpenApiSchema
{
OneOf = new List<OpenApiSchema>
{
new ()
{
Type = "number",
Format = "double",
Nullable = true
},
new ()
{
Type = "string",
Nullable = true
},
new ()
{
Enum = new List<IOpenApiAny>()
{
new OpenApiString("INF"),
new OpenApiString("INF"),
new OpenApiString("NaN"),
},
Type = "string",
Nullable = true
}
}
};
Assert.True(schema.IsODataPrimitiveType());
}
[Fact]
public void IsOdataPrimitiveBackwardCompatible()
{
var schema = new OpenApiSchema
{
OneOf = new List<OpenApiSchema>
{
new ()
{
Type = "number",
Format = "double",
},
new ()
{
Type = "string",
},
new ()
{
Enum = new List<IOpenApiAny>()
{
new OpenApiString("INF"),
new OpenApiString("INF"),
new OpenApiString("NaN"),
}
}
}
};
Assert.True(schema.IsODataPrimitiveType());
}
}

0 comments on commit dc20236

Please sign in to comment.