Skip to content

Commit 288224f

Browse files
committed
Replace OpenApiReference with a proxy reference equivalent
1 parent b1e9cd8 commit 288224f

26 files changed

+138
-514
lines changed

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

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using Microsoft.OData.Edm.Csdl;
1111
using Microsoft.OData.Edm.Vocabularies;
1212
using Microsoft.OpenApi.Models;
13+
using Microsoft.OpenApi.Models.References;
1314
using Microsoft.OpenApi.OData.Edm;
1415
using Microsoft.OpenApi.OData.Vocabulary.Capabilities;
1516

@@ -39,28 +40,12 @@ internal static OpenApiSchema GetDerivedTypesReferenceSchema(IEdmStructuredType
3940
OneOf = new List<OpenApiSchema>()
4041
};
4142

42-
OpenApiSchema baseTypeSchema = new()
43-
{
44-
UnresolvedReference = true,
45-
Reference = new OpenApiReference
46-
{
47-
Type = ReferenceType.Schema,
48-
Id = schemaElement.FullName()
49-
}
50-
};
43+
OpenApiSchema baseTypeSchema = new OpenApiSchemaReference(schemaElement.FullName(), null);
5144
schema.OneOf.Add(baseTypeSchema);
5245

5346
foreach (IEdmSchemaElement derivedType in derivedTypes)
5447
{
55-
OpenApiSchema derivedTypeSchema = new()
56-
{
57-
UnresolvedReference = true,
58-
Reference = new OpenApiReference
59-
{
60-
Type = ReferenceType.Schema,
61-
Id = derivedType.FullName()
62-
}
63-
};
48+
OpenApiSchema derivedTypeSchema = new OpenApiSchemaReference(derivedType.FullName(), null);
6449
schema.OneOf.Add(derivedTypeSchema);
6550
};
6651

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

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -455,12 +455,7 @@ private static OpenApiSchema CreateEnumTypeSchema(this ODataContext context, IEd
455455
{
456456
schema.Type = null;
457457
schema.AnyOf = null;
458-
schema.Reference = new OpenApiReference
459-
{
460-
Type = ReferenceType.Schema,
461-
Id = typeReference.Definition.FullTypeName()
462-
};
463-
schema.UnresolvedReference = true;
458+
schema = new OpenApiSchemaReference(typeReference.Definition.FullTypeName(), null);
464459
schema.Nullable = typeReference.IsNullable;
465460
}
466461

@@ -504,12 +499,7 @@ private static OpenApiSchema CreateStructuredTypeSchema(this ODataContext contex
504499
{
505500
schema.Type = null;
506501
schema.AnyOf = null;
507-
schema.Reference = new OpenApiReference
508-
{
509-
Type = ReferenceType.Schema,
510-
Id = typeReference.Definition.FullTypeName()
511-
};
512-
schema.UnresolvedReference = true;
502+
schema = new OpenApiSchemaReference(typeReference.Definition.FullTypeName(), null);
513503
schema.Nullable = typeReference.IsNullable;
514504
}
515505

@@ -548,15 +538,9 @@ private static OpenApiSchema CreateTypeDefinitionSchema(this ODataContext contex
548538
{
549539
schema.Type = null;
550540
schema.AnyOf = null;
551-
schema.Reference = new OpenApiReference
552-
{
553-
Type = ReferenceType.Schema,
554-
Id = reference.Definition.FullTypeName()
555-
};
556-
schema.UnresolvedReference = true;
541+
schema = new OpenApiSchemaReference(reference.Definition.FullTypeName(), null);
557542
schema.Nullable = reference.IsNullable;
558-
}
559-
543+
}
560544

561545
return schema;
562546
}

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

Lines changed: 2 additions & 1 deletion
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
// ------------------------------------------------------------
@@ -11,6 +11,7 @@
1111
using Microsoft.OpenApi.OData.Common;
1212
using Microsoft.OpenApi.OData.Edm;
1313
using Microsoft.OpenApi.MicrosoftExtensions;
14+
using Microsoft.OpenApi.Models.References;
1415

1516
namespace Microsoft.OpenApi.OData.Generator
1617
{

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

Lines changed: 6 additions & 26 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
// ------------------------------------------------------------
@@ -362,11 +362,7 @@ public static OpenApiParameter CreateTop(this ODataContext context, IEdmVocabula
362362
bool? top = context.Model.GetBoolean(target, CapabilitiesConstants.TopSupported);
363363
if (top == null || top.Value)
364364
{
365-
return new OpenApiParameter
366-
{
367-
UnresolvedReference = true,
368-
Reference = new OpenApiReference { Type = ReferenceType.Parameter, Id = "top" }
369-
};
365+
return new OpenApiParameterReference("top", null);
370366
}
371367

372368
return null;
@@ -404,11 +400,7 @@ public static OpenApiParameter CreateSkip(this ODataContext context, IEdmVocabul
404400
bool? skip = context.Model.GetBoolean(target, CapabilitiesConstants.SkipSupported);
405401
if (skip == null || skip.Value)
406402
{
407-
return new OpenApiParameter
408-
{
409-
UnresolvedReference = true,
410-
Reference = new OpenApiReference { Type = ReferenceType.Parameter, Id = "skip" }
411-
};
403+
return new OpenApiParameterReference("skip", null);
412404
}
413405

414406
return null;
@@ -446,11 +438,7 @@ public static OpenApiParameter CreateSearch(this ODataContext context, IEdmVocab
446438
SearchRestrictionsType search = context.Model.GetRecord<SearchRestrictionsType>(target, CapabilitiesConstants.SearchRestrictions);
447439
if (search == null || search.IsSearchable)
448440
{
449-
return new OpenApiParameter
450-
{
451-
UnresolvedReference = true,
452-
Reference = new OpenApiReference { Type = ReferenceType.Parameter, Id = "search" }
453-
};
441+
return new OpenApiParameterReference("search", null);
454442
}
455443

456444
return null;
@@ -487,11 +475,7 @@ public static OpenApiParameter CreateCount(this ODataContext context, IEdmVocabu
487475
CountRestrictionsType count = context.Model.GetRecord<CountRestrictionsType>(target, CapabilitiesConstants.CountRestrictions);
488476
if (count == null || count.IsCountable)
489477
{
490-
return new OpenApiParameter
491-
{
492-
UnresolvedReference = true,
493-
Reference = new OpenApiReference { Type = ReferenceType.Parameter, Id = "count" }
494-
};
478+
return new OpenApiParameterReference("count", null);
495479
}
496480

497481
return null;
@@ -529,11 +513,7 @@ public static OpenApiParameter CreateFilter(this ODataContext context, IEdmVocab
529513
FilterRestrictionsType filter = context.Model.GetRecord<FilterRestrictionsType>(target, CapabilitiesConstants.FilterRestrictions);
530514
if (filter == null || filter.IsFilterable)
531515
{
532-
return new OpenApiParameter
533-
{
534-
UnresolvedReference = true,
535-
Reference = new OpenApiReference { Type = ReferenceType.Parameter, Id = "filter" }
536-
};
516+
return new OpenApiParameterReference("filter", null);
537517
}
538518

539519
return null;

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

Lines changed: 4 additions & 19 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
// ------------------------------------------------------------
@@ -9,6 +9,7 @@
99
using Microsoft.OpenApi.Models;
1010
using Microsoft.OpenApi.OData.Edm;
1111
using Microsoft.OpenApi.OData.Common;
12+
using Microsoft.OpenApi.Models.References;
1213

1314
namespace Microsoft.OpenApi.OData.Generator
1415
{
@@ -125,15 +126,7 @@ public static IDictionary<string, OpenApiRequestBody> CreateRequestBodies(this O
125126
/// <returns>The created <see cref="OpenApiRequestBody"/></returns>
126127
private static OpenApiRequestBody CreateRefPostRequestBody()
127128
{
128-
OpenApiSchema schema = new()
129-
{
130-
UnresolvedReference = true,
131-
Reference = new OpenApiReference
132-
{
133-
Type = ReferenceType.Schema,
134-
Id = Constants.ReferenceCreateSchemaName
135-
}
136-
};
129+
OpenApiSchema schema = new OpenApiSchemaReference(Constants.ReferenceCreateSchemaName, null);
137130
return new OpenApiRequestBody
138131
{
139132
Required = true,
@@ -156,15 +149,7 @@ private static OpenApiRequestBody CreateRefPostRequestBody()
156149
/// <returns>The created <see cref="OpenApiRequestBody"/></returns>
157150
private static OpenApiRequestBody CreateRefPutRequestBody()
158151
{
159-
OpenApiSchema schema = new()
160-
{
161-
UnresolvedReference = true,
162-
Reference = new OpenApiReference
163-
{
164-
Type = ReferenceType.Schema,
165-
Id = Constants.ReferenceUpdateSchemaName
166-
}
167-
};
152+
OpenApiSchema schema = new OpenApiSchemaReference(Constants.ReferenceUpdateSchemaName, null);
168153

169154
return new OpenApiRequestBody
170155
{

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

Lines changed: 11 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Linq;
88
using Microsoft.OData.Edm;
99
using Microsoft.OpenApi.Models;
10+
using Microsoft.OpenApi.Models.References;
1011
using Microsoft.OpenApi.OData.Common;
1112
using Microsoft.OpenApi.OData.Edm;
1213
using Microsoft.OpenApi.OData.Vocabulary.Core;
@@ -22,40 +23,14 @@ internal static class OpenApiResponseGenerator
2223
new Dictionary<string, OpenApiResponse>
2324
{
2425
{ Constants.StatusCodeDefault,
25-
new OpenApiResponse
26-
{
27-
UnresolvedReference = true,
28-
Reference = new OpenApiReference
29-
{
30-
Type = ReferenceType.Response,
31-
Id = Constants.Error
32-
}
33-
}
26+
new OpenApiResponseReference(Constants.Error, null)
3427
},
3528

3629
{ Constants.StatusCode204, new OpenApiResponse { Description = Constants.Success} },
3730
{ Constants.StatusCode201, new OpenApiResponse { Description = Constants.Created} },
3831
{ Constants.StatusCodeClass2XX, new OpenApiResponse { Description = Constants.Success} },
39-
{ Constants.StatusCodeClass4XX, new OpenApiResponse
40-
{
41-
UnresolvedReference = true,
42-
Reference = new OpenApiReference
43-
{
44-
Type = ReferenceType.Response,
45-
Id = Constants.Error
46-
}
47-
}
48-
},
49-
{ Constants.StatusCodeClass5XX, new OpenApiResponse
50-
{
51-
UnresolvedReference = true,
52-
Reference = new OpenApiReference
53-
{
54-
Type = ReferenceType.Response,
55-
Id = Constants.Error
56-
}
57-
}
58-
}
32+
{ Constants.StatusCodeClass4XX, new OpenApiResponseReference(Constants.Error, null)},
33+
{ Constants.StatusCodeClass5XX, new OpenApiResponseReference(Constants.Error, null)}
5934
};
6035

6136
/// <summary>
@@ -156,15 +131,7 @@ public static OpenApiResponses CreateResponses(this ODataContext context, IEdmOp
156131
{
157132
responses.Add(
158133
context.Settings.UseSuccessStatusCodeRange ? Constants.StatusCodeClass2XX : Constants.StatusCode200,
159-
new OpenApiResponse
160-
{
161-
UnresolvedReference = true,
162-
Reference = new OpenApiReference()
163-
{
164-
Type = ReferenceType.Response,
165-
Id = $"{operation.Name}Response"
166-
}
167-
}
134+
new OpenApiResponseReference($"{operation.Name}Response", null)
168135
);
169136
}
170137
else
@@ -212,16 +179,9 @@ public static OpenApiResponse CreateOperationResponse(this ODataContext context,
212179
{
213180
AllOf = new List<OpenApiSchema>
214181
{
215-
new OpenApiSchema
216-
{
217-
UnresolvedReference = true,
218-
Reference = new OpenApiReference
219-
{
220-
Type = ReferenceType.Schema,
221-
Id = operation.IsDeltaFunction() ? Constants.BaseDeltaFunctionResponse // @odata.nextLink + @odata.deltaLink
222-
: Constants.BaseCollectionPaginationCountResponse // @odata.nextLink + @odata.count
223-
}
224-
},
182+
new OpenApiSchemaReference(operation.IsDeltaFunction() ? Constants.BaseDeltaFunctionResponse // @odata.nextLink + @odata.deltaLink
183+
: Constants.BaseCollectionPaginationCountResponse // @odata.nextLink + @odata.count)
184+
,null),
225185
baseSchema
226186
}
227187
};
@@ -315,15 +275,7 @@ private static OpenApiResponse CreateCollectionResponse(string typeName)
315275
Constants.ApplicationJsonMediaType,
316276
new OpenApiMediaType
317277
{
318-
Schema = new OpenApiSchema
319-
{
320-
UnresolvedReference = true,
321-
Reference = new OpenApiReference
322-
{
323-
Type = ReferenceType.Schema,
324-
Id = $"{typeName}{Constants.CollectionSchemaSuffix}"
325-
}
326-
}
278+
Schema = new OpenApiSchemaReference($"{typeName}{Constants.CollectionSchemaSuffix}", null)
327279
}
328280
}
329281
}
@@ -332,14 +284,7 @@ private static OpenApiResponse CreateCollectionResponse(string typeName)
332284

333285
private static OpenApiResponse CreateCountResponse()
334286
{
335-
OpenApiSchema schema = new()
336-
{
337-
UnresolvedReference = true,
338-
Reference = new() {
339-
Type = ReferenceType.Schema,
340-
Id = Constants.DollarCountSchemaName
341-
}
342-
};
287+
OpenApiSchema schema = new OpenApiSchemaReference(Constants.DollarCountSchemaName, null);
343288
return new OpenApiResponse
344289
{
345290
Description = "The count of the resource",
@@ -368,15 +313,7 @@ private static OpenApiResponse CreateErrorResponse(this ODataContext context)
368313
Constants.ApplicationJsonMediaType,
369314
new OpenApiMediaType
370315
{
371-
Schema = new OpenApiSchema
372-
{
373-
UnresolvedReference = true,
374-
Reference = new OpenApiReference
375-
{
376-
Type = ReferenceType.Schema,
377-
Id = $"{errorNamespaceName}{OpenApiErrorSchemaGenerator.ODataErrorClassName}"
378-
}
379-
}
316+
Schema = new OpenApiSchemaReference($"{errorNamespaceName}{OpenApiErrorSchemaGenerator.ODataErrorClassName}", null)
380317
}
381318
}
382319
}

0 commit comments

Comments
 (0)