Skip to content

Commit c06fde0

Browse files
committed
fix: regroups example generation logic
Signed-off-by: Vincent Biret <[email protected]>
1 parent 3d76e85 commit c06fde0

File tree

4 files changed

+63
-120
lines changed

4 files changed

+63
-120
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public static OpenApiComponents CreateComponents(this ODataContext context, Open
3131
document.Components.Parameters = context.CreateParameters();
3232
document.Components.Responses = context.CreateResponses(document);
3333
document.Components.RequestBodies = context.CreateRequestBodies(document);
34-
document.Components.Examples = context.CreateExamples();
34+
document.Components.Examples = context.CreateExamples(document);
3535
document.Components.SecuritySchemes = context.CreateSecuritySchemes();
3636
document.Components.Links = null;
3737
document.Components.Callbacks = null;

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

Lines changed: 12 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,7 @@
66
using System;
77
using System.Collections.Generic;
88
using System.Diagnostics;
9-
using System.Globalization;
10-
using System.Linq;
11-
using System.Text.Json.Nodes;
129
using Microsoft.OData.Edm;
13-
using Microsoft.OpenApi.Exceptions;
1410
using Microsoft.OpenApi.Models;
1511
using Microsoft.OpenApi.OData.Common;
1612
using Microsoft.OpenApi.OData.Edm;
@@ -26,10 +22,12 @@ internal static class OpenApiExampleGenerator
2622
/// Create the dictionary of <see cref="OpenApiExample"/> object.
2723
/// </summary>
2824
/// <param name="context">The OData to Open API context.</param>
25+
/// <param name="document">The Open API document.</param>
2926
/// <returns>The created <see cref="OpenApiExample"/> dictionary.</returns>
30-
public static IDictionary<string, OpenApiExample> CreateExamples(this ODataContext context)
27+
public static IDictionary<string, OpenApiExample> CreateExamples(this ODataContext context, OpenApiDocument document)
3128
{
3229
Utils.CheckArgumentNull(context, nameof(context));
30+
Utils.CheckArgumentNull(document, nameof(document));
3331

3432
IDictionary<string, OpenApiExample> examples = new Dictionary<string, OpenApiExample>();
3533

@@ -43,10 +41,9 @@ public static IDictionary<string, OpenApiExample> CreateExamples(this ODataConte
4341
{
4442
switch (element.SchemaElementKind)
4543
{
46-
case EdmSchemaElementKind.TypeDefinition: // Type definition
44+
case EdmSchemaElementKind.TypeDefinition when element is IEdmType reference: // Type definition
4745
{
48-
IEdmType reference = (IEdmType)element;
49-
OpenApiExample example = context.CreateExample(reference);
46+
OpenApiExample example = context.CreateExample(reference, document);
5047
if (example != null)
5148
{
5249
examples.Add(reference.FullTypeName(), example);
@@ -59,77 +56,19 @@ public static IDictionary<string, OpenApiExample> CreateExamples(this ODataConte
5956
return examples;
6057
}
6158

62-
private static OpenApiExample CreateExample(this ODataContext context, IEdmType edmType)
59+
private static OpenApiExample CreateExample(this ODataContext context, IEdmType edmType, OpenApiDocument document)
6360
{
6461
Debug.Assert(context != null);
6562
Debug.Assert(edmType != null);
6663

67-
switch (edmType.TypeKind)
64+
return edmType.TypeKind switch
6865
{
69-
case EdmTypeKind.Complex: // complex type
70-
case EdmTypeKind.Entity: // entity type
71-
return CreateStructuredTypeExample((IEdmStructuredType)edmType);
72-
}
73-
74-
return null;
75-
}
76-
77-
private static OpenApiExample CreateStructuredTypeExample(IEdmStructuredType structuredType)
78-
{
79-
OpenApiExample example = new();
80-
81-
JsonObject value = new();
82-
83-
// properties
84-
foreach (var property in structuredType.DeclaredProperties.OrderBy(static p => p.Name, StringComparer.Ordinal))
85-
{
86-
IEdmTypeReference propertyType = property.Type;
87-
88-
JsonNode item = GetTypeNameForExample(propertyType);
89-
90-
if (propertyType.TypeKind() == EdmTypeKind.Primitive &&
91-
item is JsonValue jsonValue &&
92-
jsonValue.TryGetValue(out string stringAny) &&
93-
structuredType is IEdmEntityType entityType &&
94-
entityType.Key().Any(k => StringComparer.Ordinal.Equals(k.Name, property.Name)))
66+
// complex type
67+
EdmTypeKind.Complex or EdmTypeKind.Entity when edmType is IEdmStructuredType edmStructuredType => new()
9568
{
96-
item = $"{stringAny} (identifier)";
97-
}
98-
99-
value.Add(property.Name, item);
100-
}
101-
example.Value = value;
102-
return example;
103-
}
104-
105-
private static JsonNode GetTypeNameForExample(IEdmTypeReference edmTypeReference)
106-
{
107-
return edmTypeReference.TypeKind() switch
108-
{
109-
// return new OpenApiBinary(new byte[] { 0x00 }); issue on binary writing
110-
EdmTypeKind.Primitive when edmTypeReference.IsBinary() => Convert.ToBase64String(new byte[] { 0x00 }),
111-
EdmTypeKind.Primitive when edmTypeReference.IsBoolean() => true,
112-
EdmTypeKind.Primitive when edmTypeReference.IsByte() => 0x00,
113-
EdmTypeKind.Primitive when edmTypeReference.IsDate() => DateTime.MinValue,
114-
EdmTypeKind.Primitive when edmTypeReference.IsDateTimeOffset() => DateTimeOffset.MinValue,
115-
EdmTypeKind.Primitive when edmTypeReference.IsDecimal() || edmTypeReference.IsDouble() => 0D,
116-
EdmTypeKind.Primitive when edmTypeReference.IsFloating() => 0F,
117-
EdmTypeKind.Primitive when edmTypeReference.IsGuid() => Guid.Empty.ToString(),
118-
EdmTypeKind.Primitive when edmTypeReference.IsInt16() || edmTypeReference.IsInt32() => 0,
119-
EdmTypeKind.Primitive when edmTypeReference.IsInt64() => 0L,
120-
EdmTypeKind.Primitive => edmTypeReference.AsPrimitive().PrimitiveDefinition().Name,
121-
EdmTypeKind.Entity or EdmTypeKind.Complex or EdmTypeKind.Enum => new JsonObject()
122-
{//TODO this is wrong for enums, and should instead use one of the enum members
123-
[Constants.OdataType] = edmTypeReference.FullName()
124-
},
125-
126-
EdmTypeKind.Collection => new JsonArray(GetTypeNameForExample(edmTypeReference.AsCollection().ElementType())),
127-
128-
EdmTypeKind.TypeDefinition => GetTypeNameForExample(new EdmPrimitiveTypeReference(edmTypeReference.AsTypeDefinition().TypeDefinition().UnderlyingType, edmTypeReference.IsNullable)),
129-
130-
EdmTypeKind.Untyped => new JsonObject(),
131-
132-
_ => throw new OpenApiException("Not support for the type kind " + edmTypeReference.TypeKind()),
69+
Value = OpenApiSchemaGenerator.CreateStructuredTypePropertiesExample(context, edmStructuredType, document),
70+
},
71+
_ => null,
13372
};
13473
}
13574
}

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

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,7 @@ private static OpenApiSchema CreateStructuredTypeSchema(this ODataContext contex
607607
}
608608
}
609609

610-
private static JsonObject CreateStructuredTypePropertiesExample(ODataContext context, IEdmStructuredType structuredType, OpenApiDocument document)
610+
internal static JsonObject CreateStructuredTypePropertiesExample(ODataContext context, IEdmStructuredType structuredType, OpenApiDocument document)
611611
{
612612
JsonObject example = [];
613613

@@ -618,19 +618,13 @@ private static JsonObject CreateStructuredTypePropertiesExample(ODataContext con
618618

619619
JsonNode item = GetTypeNameForExample(context, propertyType, document);
620620

621-
EdmTypeKind typeKind = propertyType.TypeKind();
622-
if (typeKind == EdmTypeKind.Primitive && item is JsonValue jsonValue && jsonValue.TryGetValue(out string stringAny))
621+
if (propertyType.TypeKind() == EdmTypeKind.Primitive &&
622+
item is JsonValue jsonValue &&
623+
jsonValue.TryGetValue(out string stringAny) &&
624+
structuredType is IEdmEntityType entityType &&
625+
entityType.Key().Any(k => StringComparer.Ordinal.Equals(k.Name, property.Name)))
623626
{
624-
string value = stringAny;
625-
if (structuredType is IEdmEntityType entityType && entityType.Key().Any(k => k.Name == property.Name))
626-
{
627-
value += " (identifier)";
628-
}
629-
if (propertyType.IsDateTimeOffset() || propertyType.IsDate() || propertyType.IsTimeOfDay())
630-
{
631-
value += " (timestamp)";
632-
}
633-
item = value;
627+
item = $"{stringAny} (identifier)";
634628
}
635629

636630
example.Add(property.Name, item);
@@ -669,6 +663,17 @@ private static JsonNode GetTypeNameForExample(ODataContext context, IEdmTypeRefe
669663
{
670664
return edmTypeReference.TypeKind() switch
671665
{
666+
// return new OpenApiBinary(new byte[] { 0x00 }); issue on binary writing
667+
EdmTypeKind.Primitive when edmTypeReference.IsBinary() => Convert.ToBase64String(new byte[] { 0x00 }),
668+
EdmTypeKind.Primitive when edmTypeReference.IsBoolean() => true,
669+
EdmTypeKind.Primitive when edmTypeReference.IsByte() => 0x00,
670+
EdmTypeKind.Primitive when edmTypeReference.IsDate() => DateTime.MinValue,
671+
EdmTypeKind.Primitive when edmTypeReference.IsDateTimeOffset() => DateTimeOffset.MinValue,
672+
EdmTypeKind.Primitive when edmTypeReference.IsDecimal() || edmTypeReference.IsDouble() => 0D,
673+
EdmTypeKind.Primitive when edmTypeReference.IsFloating() => 0F,
674+
EdmTypeKind.Primitive when edmTypeReference.IsGuid() => Guid.Empty.ToString(),
675+
EdmTypeKind.Primitive when edmTypeReference.IsInt16() || edmTypeReference.IsInt32() => 0,
676+
EdmTypeKind.Primitive when edmTypeReference.IsInt64() => 0L,
672677
EdmTypeKind.Primitive => GetTypeNameForPrimitive(context, edmTypeReference, document),
673678

674679
EdmTypeKind.Entity or EdmTypeKind.Complex or EdmTypeKind.Enum => new JsonObject()
@@ -677,6 +682,7 @@ private static JsonNode GetTypeNameForExample(ODataContext context, IEdmTypeRefe
677682
},
678683

679684
EdmTypeKind.Collection => new JsonArray(GetTypeNameForExample(context, edmTypeReference.AsCollection().ElementType(), document)),
685+
EdmTypeKind.TypeDefinition => GetTypeNameForExample(context, new EdmPrimitiveTypeReference(edmTypeReference.AsTypeDefinition().TypeDefinition().UnderlyingType, edmTypeReference.IsNullable), document),
680686
EdmTypeKind.Untyped => new JsonObject(),
681687
_ => throw new OpenApiException("Not support for the type kind " + edmTypeReference.TypeKind()),
682688
};

0 commit comments

Comments
 (0)