66using System ;
77using System . Collections . Generic ;
88using System . Diagnostics ;
9- using System . Globalization ;
10- using System . Linq ;
11- using System . Text . Json . Nodes ;
129using Microsoft . OData . Edm ;
13- using Microsoft . OpenApi . Exceptions ;
1410using Microsoft . OpenApi . Models ;
1511using Microsoft . OpenApi . OData . Common ;
1612using 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 }
0 commit comments