66using System ;
77using System . Collections . Generic ;
88using System . Diagnostics ;
9+ using System . Globalization ;
910using System . Linq ;
1011using System . Text . Json . Nodes ;
1112using Microsoft . OData . Edm ;
@@ -75,33 +76,24 @@ private static OpenApiExample CreateExample(this ODataContext context, IEdmType
7576
7677 private static OpenApiExample CreateStructuredTypeExample ( IEdmStructuredType structuredType )
7778 {
78- OpenApiExample example = new OpenApiExample ( ) ;
79+ OpenApiExample example = new ( ) ;
7980
80- JsonObject value = new JsonObject ( ) ;
81-
82- IEdmEntityType entityType = structuredType as IEdmEntityType ;
81+ JsonObject value = new ( ) ;
8382
8483 // properties
8584 foreach ( var property in structuredType . DeclaredProperties . OrderBy ( static p => p . Name , StringComparer . Ordinal ) )
8685 {
87- // IOpenApiAny item;
8886 IEdmTypeReference propertyType = property . Type ;
8987
9088 JsonNode item = GetTypeNameForExample ( propertyType ) ;
9189
92- EdmTypeKind typeKind = propertyType . TypeKind ( ) ;
93- if ( typeKind == EdmTypeKind . Primitive && item is JsonValue jsonValue && jsonValue . TryGetValue ( out string stringAny ) )
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 ) ) )
9495 {
95- string propertyValue = stringAny ;
96- if ( entityType != null && entityType . Key ( ) . Any ( k => k . Name == property . Name ) )
97- {
98- propertyValue += " (identifier)" ;
99- }
100- if ( propertyType . IsDateTimeOffset ( ) || propertyType . IsDate ( ) || propertyType . IsTimeOfDay ( ) )
101- {
102- propertyValue += " (timestamp)" ;
103- }
104- item = propertyValue ;
96+ item = $ "{ stringAny } (identifier)";
10597 }
10698
10799 value . Add ( property . Name , item ) ;
@@ -112,81 +104,33 @@ private static OpenApiExample CreateStructuredTypeExample(IEdmStructuredType str
112104
113105 private static JsonNode GetTypeNameForExample ( IEdmTypeReference edmTypeReference )
114106 {
115- switch ( edmTypeReference . TypeKind ( ) )
107+ return edmTypeReference . TypeKind ( ) switch
116108 {
117- case EdmTypeKind . Primitive :
118- if ( edmTypeReference . IsBinary ( ) )
119- {
120- // return new OpenApiBinary(new byte[] { 0x00 }); issue on binary writing
121- return Convert . ToBase64String ( new byte [ ] { 0x00 } ) ;
122- }
123- else if ( edmTypeReference . IsBoolean ( ) )
124- {
125- return true ;
126- }
127- else if ( edmTypeReference . IsByte ( ) )
128- {
129- return 0x00 ;
130- }
131- else if ( edmTypeReference . IsDate ( ) )
132- {
133- return DateTime . MinValue ;
134- }
135- else if ( edmTypeReference . IsDateTimeOffset ( ) )
136- {
137- return DateTimeOffset . MinValue ;
138- }
139- else if ( edmTypeReference . IsDecimal ( ) || edmTypeReference . IsDouble ( ) )
140- {
141- return 0D ;
142- }
143- else if ( edmTypeReference . IsFloating ( ) )
144- {
145- return 0F ;
146- }
147- else if ( edmTypeReference . IsGuid ( ) )
148- {
149- return Guid . Empty . ToString ( ) ;
150- }
151- else if ( edmTypeReference . IsInt16 ( ) || edmTypeReference . IsInt32 ( ) )
152- {
153- return 0 ;
154- }
155- else if ( edmTypeReference . IsInt64 ( ) )
156- {
157- return 0L ;
158- }
159- else
160- {
161- return edmTypeReference . AsPrimitive ( ) . PrimitiveDefinition ( ) . Name ;
162- }
163-
164- case EdmTypeKind . Entity :
165- case EdmTypeKind . Complex :
166- case EdmTypeKind . Enum :
167- JsonObject obj = new ( )
168- {
169- [ "@odata.type" ] = edmTypeReference . FullName ( )
170- } ;
171- return obj ;
172-
173- case EdmTypeKind . Collection :
174- JsonArray array = [ ] ;
175- IEdmTypeReference elementType = edmTypeReference . AsCollection ( ) . ElementType ( ) ;
176- array . Add ( GetTypeNameForExample ( elementType ) ) ;
177- return array ;
178-
179- case EdmTypeKind . TypeDefinition :
180- var typedef = edmTypeReference . AsTypeDefinition ( ) . TypeDefinition ( ) ;
181- return GetTypeNameForExample ( new EdmPrimitiveTypeReference ( typedef . UnderlyingType , edmTypeReference . IsNullable ) ) ;
182-
183- case EdmTypeKind . Untyped :
184- return new JsonObject ( ) ;
185-
186- case EdmTypeKind . EntityReference :
187- default :
188- throw new OpenApiException ( "Not support for the type kind " + edmTypeReference . TypeKind ( ) ) ;
189- }
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 ( ) ) ,
133+ } ;
190134 }
191135 }
192136}
0 commit comments