Skip to content

Commit 62f71bb

Browse files
committed
Use OpenApiAny() for extensions and JsonNodes for examples
1 parent ab7602e commit 62f71bb

35 files changed

+166
-166
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ internal static void AddCustomAttributesToExtensions(this IDictionary<string, IO
158158
{
159159
foreach (var item in atrributesValueMap)
160160
{
161-
extensions.TryAdd(item.Key, new OpenApiString(item.Value));
161+
extensions.TryAdd(item.Key, new OpenApiAny(item.Value));
162162
}
163163
}
164164
}

src/Microsoft.OpenApi.OData.Reader/EdmModelOpenApiExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// ------------------------------------------------------------
1+
// ------------------------------------------------------------
22
// Copyright (c) Microsoft Corporation. All rights reserved.
33
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information.
44
// ------------------------------------------------------------
@@ -48,7 +48,7 @@ public static OpenApiDocument ConvertToOpenApi(this IEdmModel model, OpenApiConv
4848
int index = 1;
4949
foreach (var error in errors)
5050
{
51-
document.Extensions.Add(Constants.xMsEdmModelError + index++, new OpenApiString(error.ToString()));
51+
document.Extensions.Add(Constants.xMsEdmModelError + index++, new OpenApiAny(error.ToString()));
5252
}
5353

5454
return document;

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

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
using System.Collections.Generic;
88
using System.Diagnostics;
99
using System.Linq;
10+
using System.Text.Json.Nodes;
1011
using Microsoft.OData.Edm;
11-
using Microsoft.OpenApi.Any;
1212
using Microsoft.OpenApi.Exceptions;
1313
using Microsoft.OpenApi.Models;
1414
using Microsoft.OpenApi.OData.Common;
@@ -77,7 +77,7 @@ private static OpenApiExample CreateStructuredTypeExample(IEdmStructuredType str
7777
{
7878
OpenApiExample example = new OpenApiExample();
7979

80-
OpenApiObject value = new OpenApiObject();
80+
JsonObject value = new JsonObject();
8181

8282
IEdmEntityType entityType = structuredType as IEdmEntityType;
8383

@@ -87,13 +87,12 @@ private static OpenApiExample CreateStructuredTypeExample(IEdmStructuredType str
8787
// IOpenApiAny item;
8888
IEdmTypeReference propertyType = property.Type;
8989

90-
IOpenApiAny item = GetTypeNameForExample(propertyType);
90+
JsonNode item = GetTypeNameForExample(propertyType);
9191

9292
EdmTypeKind typeKind = propertyType.TypeKind();
93-
if (typeKind == EdmTypeKind.Primitive && item is OpenApiString)
93+
if (typeKind == EdmTypeKind.Primitive && item is JsonValue jsonValue && jsonValue.TryGetValue(out string stringAny))
9494
{
95-
OpenApiString stringAny = item as OpenApiString;
96-
string propertyValue = stringAny.Value;
95+
string propertyValue = stringAny;
9796
if (entityType != null && entityType.Key().Any(k => k.Name == property.Name))
9897
{
9998
propertyValue += " (identifier)";
@@ -102,7 +101,7 @@ private static OpenApiExample CreateStructuredTypeExample(IEdmStructuredType str
102101
{
103102
propertyValue += " (timestamp)";
104103
}
105-
item = new OpenApiString(propertyValue);
104+
item = propertyValue;
106105
}
107106

108107
value.Add(property.Name, item);
@@ -111,66 +110,68 @@ private static OpenApiExample CreateStructuredTypeExample(IEdmStructuredType str
111110
return example;
112111
}
113112

114-
private static IOpenApiAny GetTypeNameForExample(IEdmTypeReference edmTypeReference)
113+
private static JsonNode GetTypeNameForExample(IEdmTypeReference edmTypeReference)
115114
{
116115
switch (edmTypeReference.TypeKind())
117116
{
118117
case EdmTypeKind.Primitive:
119118
if (edmTypeReference.IsBinary())
120119
{
121120
// return new OpenApiBinary(new byte[] { 0x00 }); issue on binary writing
122-
return new OpenApiString(Convert.ToBase64String(new byte[] { 0x00 }));
121+
return Convert.ToBase64String(new byte[] { 0x00 });
123122
}
124123
else if (edmTypeReference.IsBoolean())
125124
{
126-
return new OpenApiBoolean(true);
125+
return true;
127126
}
128127
else if (edmTypeReference.IsByte())
129128
{
130-
return new OpenApiByte(0x00);
129+
return 0x00;
131130
}
132131
else if (edmTypeReference.IsDate())
133132
{
134-
return new OpenApiDate(DateTime.MinValue);
133+
return DateTime.MinValue;
135134
}
136135
else if (edmTypeReference.IsDateTimeOffset())
137136
{
138-
return new OpenApiDateTime(DateTimeOffset.MinValue);
137+
return DateTimeOffset.MinValue;
139138
}
140139
else if (edmTypeReference.IsDecimal() || edmTypeReference.IsDouble())
141140
{
142-
return new OpenApiDouble(0D);
141+
return 0D;
143142
}
144143
else if (edmTypeReference.IsFloating())
145144
{
146-
return new OpenApiFloat(0F);
145+
return 0F;
147146
}
148147
else if (edmTypeReference.IsGuid())
149148
{
150-
return new OpenApiString(Guid.Empty.ToString());
149+
return Guid.Empty.ToString();
151150
}
152151
else if (edmTypeReference.IsInt16() || edmTypeReference.IsInt32())
153152
{
154-
return new OpenApiInteger(0);
153+
return 0;
155154
}
156155
else if (edmTypeReference.IsInt64())
157156
{
158-
return new OpenApiLong(0L);
157+
return 0L;
159158
}
160159
else
161160
{
162-
return new OpenApiString(edmTypeReference.AsPrimitive().PrimitiveDefinition().Name);
161+
return edmTypeReference.AsPrimitive().PrimitiveDefinition().Name;
163162
}
164163

165164
case EdmTypeKind.Entity:
166165
case EdmTypeKind.Complex:
167166
case EdmTypeKind.Enum:
168-
OpenApiObject obj = new OpenApiObject();
169-
obj["@odata.type"] = new OpenApiString(edmTypeReference.FullName());
167+
JsonObject obj = new()
168+
{
169+
["@odata.type"] = edmTypeReference.FullName()
170+
};
170171
return obj;
171172

172173
case EdmTypeKind.Collection:
173-
OpenApiArray array = new OpenApiArray();
174+
JsonArray array = [];
174175
IEdmTypeReference elementType = edmTypeReference.AsCollection().ElementType();
175176
array.Add(GetTypeNameForExample(elementType));
176177
return array;
@@ -180,7 +181,7 @@ private static IOpenApiAny GetTypeNameForExample(IEdmTypeReference edmTypeRefere
180181
return GetTypeNameForExample(new EdmPrimitiveTypeReference(typedef.UnderlyingType, edmTypeReference.IsNullable));
181182

182183
case EdmTypeKind.Untyped:
183-
return new OpenApiObject();
184+
return new JsonObject();
184185

185186
case EdmTypeKind.EntityReference:
186187
default:

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Diagnostics;
88
using System.Linq;
99
using System.Reflection;
10+
using System.Text.Json.Nodes;
1011
using Microsoft.OData.Edm;
1112
using Microsoft.OpenApi.Any;
1213
using Microsoft.OpenApi.Interfaces;
@@ -109,11 +110,11 @@ private static Dictionary<string, IOpenApiExtension> GetExtensions(this ODataCon
109110
{
110111
{
111112
"x-ms-generated-by",
112-
new OpenApiObject
113+
new OpenApiAny(new JsonObject
113114
{
114-
{ "toolName", new OpenApiString("Microsoft.OpenApi.OData") },
115-
{ "toolVersion", new OpenApiString(Assembly.GetExecutingAssembly().GetName().Version.ToString()) }
116-
}
115+
{ "toolName", "Microsoft.OpenApi.OData" },
116+
{ "toolVersion", Assembly.GetExecutingAssembly().GetName().Version.ToString() }
117+
})
117118
}
118119
};
119120
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
using Microsoft.OData.Edm;
88
using Microsoft.OpenApi.OData.Common;
99
using Microsoft.OpenApi.Models;
10-
using Microsoft.OpenApi.Any;
1110
using Microsoft.OpenApi.OData.Edm;
1211
using System.Linq;
1312

@@ -97,7 +96,7 @@ public static IDictionary<string, OpenApiLink> CreateLinks(this ODataContext con
9796
{
9897
link.Parameters[pathKeyName] = new RuntimeExpressionAnyWrapper
9998
{
100-
Any = new OpenApiString("$request.path." + pathKeyName)
99+
Any = "$request.path." + pathKeyName
101100
};
102101
}
103102
}

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

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
1-
// ------------------------------------------------------------
1+
// ------------------------------------------------------------
22
// Copyright (c) Microsoft Corporation. All rights reserved.
33
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information.
44
// ------------------------------------------------------------
55

66
using System.Collections.Generic;
77
using System.Linq;
88
using Microsoft.OData.Edm;
9-
using Microsoft.OpenApi.Any;
109
using Microsoft.OpenApi.Models;
1110
using Microsoft.OpenApi.OData.Common;
1211
using Microsoft.OData.Edm.Vocabularies;
1312
using Microsoft.OpenApi.OData.Edm;
1413
using Microsoft.OpenApi.OData.Vocabulary.Capabilities;
1514
using System.Diagnostics;
1615
using System;
16+
using System.Text.Json.Nodes;
17+
using Microsoft.OpenApi.Any;
18+
using Microsoft.OpenApi.Models.References;
1719

1820
namespace Microsoft.OpenApi.OData.Generator
1921
{
@@ -187,7 +189,7 @@ public static IList<OpenApiParameter> CreateKeyParameters(this ODataContext cont
187189
Schema = context.CreateEdmTypeSchema(keys.First().Type)
188190
};
189191

190-
parameter.Extensions.Add(Constants.xMsKeyType, new OpenApiString(entityType.Name));
192+
parameter.Extensions.Add(Constants.xMsKeyType, new OpenApiAny(entityType.Name));
191193
parameters.Add(parameter);
192194
}
193195
else
@@ -212,7 +214,7 @@ public static IList<OpenApiParameter> CreateKeyParameters(this ODataContext cont
212214
parameter.Description += $", {keyProperty.Name}={quote}{{{parameter.Name}}}{quote}";
213215
}
214216

215-
parameter.Extensions.Add(Constants.xMsKeyType, new OpenApiString(entityType.Name));
217+
parameter.Extensions.Add(Constants.xMsKeyType, new OpenApiAny(entityType.Name));
216218
parameters.Add(parameter);
217219
}
218220
}
@@ -606,7 +608,7 @@ public static OpenApiParameter CreateOrderBy(this ODataContext context, IEdmVoca
606608
return null;
607609
}
608610

609-
IList<IOpenApiAny> orderByItems = new List<IOpenApiAny>();
611+
IList<JsonNode> orderByItems = new List<JsonNode>();
610612
foreach (var property in structuredType.StructuralProperties())
611613
{
612614
if (sort != null && sort.IsNonSortableProperty(property.Name))
@@ -620,17 +622,17 @@ public static OpenApiParameter CreateOrderBy(this ODataContext context, IEdmVoca
620622
{
621623
if (isAscOnly)
622624
{
623-
orderByItems.Add(new OpenApiString(property.Name));
625+
orderByItems.Add(property.Name);
624626
}
625627
else
626628
{
627-
orderByItems.Add(new OpenApiString(property.Name + " desc"));
629+
orderByItems.Add(property.Name + " desc");
628630
}
629631
}
630632
else
631633
{
632-
orderByItems.Add(new OpenApiString(property.Name));
633-
orderByItems.Add(new OpenApiString(property.Name + " desc"));
634+
orderByItems.Add(property.Name);
635+
orderByItems.Add(property.Name + " desc");
634636
}
635637
}
636638

@@ -723,11 +725,11 @@ public static OpenApiParameter CreateSelect(this ODataContext context, IEdmVocab
723725
return null;
724726
}
725727

726-
IList<IOpenApiAny> selectItems = new List<IOpenApiAny>();
728+
IList<JsonNode> selectItems = new List<JsonNode>();
727729

728730
foreach (var property in structuredType.StructuralProperties())
729731
{
730-
selectItems.Add(new OpenApiString(property.Name));
732+
selectItems.Add(property.Name);
731733
}
732734

733735
foreach (var property in structuredType.NavigationProperties())
@@ -737,7 +739,7 @@ public static OpenApiParameter CreateSelect(this ODataContext context, IEdmVocab
737739
continue;
738740
}
739741

740-
selectItems.Add(new OpenApiString(property.Name));
742+
selectItems.Add(property.Name);
741743
}
742744

743745
return new OpenApiParameter
@@ -829,10 +831,7 @@ public static OpenApiParameter CreateExpand(this ODataContext context, IEdmVocab
829831
return null;
830832
}
831833

832-
IList<IOpenApiAny> expandItems = new List<IOpenApiAny>
833-
{
834-
new OpenApiString("*")
835-
};
834+
IList<JsonNode> expandItems = [ "*" ];
836835

837836
foreach (var property in structuredType.NavigationProperties())
838837
{
@@ -841,7 +840,7 @@ public static OpenApiParameter CreateExpand(this ODataContext context, IEdmVocab
841840
continue;
842841
}
843842

844-
expandItems.Add(new OpenApiString(property.Name));
843+
expandItems.Add(property.Name);
845844
}
846845

847846
return new OpenApiParameter

0 commit comments

Comments
 (0)