Skip to content

Commit 7194ad6

Browse files
committed
fix: passes remaining document references where required
1 parent b462c7f commit 7194ad6

39 files changed

+455
-387
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ internal static class EdmModelHelper
2222
/// Adds the derived types references together with their base type reference in the OneOf property of an OpenAPI schema.
2323
/// </summary>
2424
/// <returns>The OpenAPI schema with the list of derived types references and their base type references set in the OneOf property.</returns>
25-
internal static OpenApiSchema GetDerivedTypesReferenceSchema(IEdmStructuredType structuredType, IEdmModel edmModel)
25+
internal static OpenApiSchema GetDerivedTypesReferenceSchema(IEdmStructuredType structuredType, IEdmModel edmModel, OpenApiDocument document)
2626
{
2727
Utils.CheckArgumentNull(structuredType, nameof(structuredType));
2828
Utils.CheckArgumentNull(edmModel, nameof(edmModel));
@@ -40,12 +40,12 @@ internal static OpenApiSchema GetDerivedTypesReferenceSchema(IEdmStructuredType
4040
OneOf = new List<OpenApiSchema>()
4141
};
4242

43-
OpenApiSchema baseTypeSchema = new OpenApiSchemaReference(schemaElement.FullName(), null);
43+
OpenApiSchema baseTypeSchema = new OpenApiSchemaReference(schemaElement.FullName(), document);
4444
schema.OneOf.Add(baseTypeSchema);
4545

4646
foreach (IEdmSchemaElement derivedType in derivedTypes)
4747
{
48-
OpenApiSchema derivedTypeSchema = new OpenApiSchemaReference(derivedType.FullName(), null);
48+
OpenApiSchema derivedTypeSchema = new OpenApiSchemaReference(derivedType.FullName(), document);
4949
schema.OneOf.Add(derivedTypeSchema);
5050
};
5151

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ internal static class OpenApiComponentsGenerator
2020
/// It holds maps of reusable schemas describing message bodies, operation parameters, and responses.
2121
/// </summary>
2222
/// <param name="context">The OData to Open API context.</param>
23+
/// <param name="document">The Open API document.</param>
2324
/// <returns>The created <see cref="OpenApiComponents"/> object.</returns>
24-
public static OpenApiComponents CreateComponents(this ODataContext context)
25+
public static OpenApiComponents CreateComponents(this ODataContext context, OpenApiDocument document)
2526
{
2627
Utils.CheckArgumentNull(context, nameof(context));
2728

@@ -36,19 +37,19 @@ public static OpenApiComponents CreateComponents(this ODataContext context)
3637
// The value of schemas is a map of Schema Objects.
3738
// Each entity type, complex type, enumeration type, and type definition directly
3839
// or indirectly used in the paths field is represented as a name/value pair of the schemas map.
39-
Schemas = context.CreateSchemas(),
40+
Schemas = context.CreateSchemas(document),
4041

4142
// The value of parameters is a map of Parameter Objects.
4243
// It allows defining query options and headers that can be reused across operations of the service.
4344
Parameters = context.CreateParameters(),
4445

4546
// The value of responses is a map of Response Objects.
4647
// It allows defining responses that can be reused across operations of the service.
47-
Responses = context.CreateResponses(),
48+
Responses = context.CreateResponses(document),
4849

4950
// The value of requestBodies is a map of RequestBody Objects.
5051
// It allows refining request bodies that can be reused across operations of the service.
51-
RequestBodies = context.CreateRequestBodies(),
52+
RequestBodies = context.CreateRequestBodies(document),
5253

5354
Examples = context.CreateExamples(),
5455

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,13 @@ public static OpenApiDocument CreateDocument(this ODataContext context)
3838

3939
Servers = context.CreateServers(),
4040

41-
Components = context.CreateComponents(),
42-
4341
SecurityRequirements = null,
4442

4543
ExternalDocs = null,
4644
Tags = context.CreateTags()
4745
};
4846

47+
doc.Components = context.CreateComponents(doc);
4948
doc.Paths = context.CreatePaths(doc);
5049

5150

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

Lines changed: 46 additions & 38 deletions
Large diffs are not rendered by default.

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

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,19 @@ internal static class OpenApiErrorSchemaGenerator
3131
/// The value of each pair is a <see cref="OpenApiSchema"/>.
3232
/// </summary>
3333
/// <param name="context">The OData to Open API context.</param>
34+
/// <param name="document">The Open API document to lookup references.</param>
3435
/// <returns>The string/schema dictionary.</returns>
35-
public static IDictionary<string, OpenApiSchema> CreateODataErrorSchemas(this ODataContext context)
36+
public static IDictionary<string, OpenApiSchema> CreateODataErrorSchemas(this ODataContext context, OpenApiDocument document)
3637
{
3738
Utils.CheckArgumentNull(context, nameof(context));
3839
var rootNamespaceName = context.GetErrorNamespaceName();
3940

4041
return new Dictionary<string, OpenApiSchema>()
4142
{
42-
{ $"{rootNamespaceName}{ODataErrorClassName}", CreateErrorSchema(rootNamespaceName) },
43-
{ $"{rootNamespaceName}{MainErrorClassName}", CreateErrorMainSchema(rootNamespaceName) },
43+
{ $"{rootNamespaceName}{ODataErrorClassName}", CreateErrorSchema(rootNamespaceName, document) },
44+
{ $"{rootNamespaceName}{MainErrorClassName}", CreateErrorMainSchema(rootNamespaceName, document) },
4445
{ $"{rootNamespaceName}{ErrorDetailsClassName}", CreateErrorDetailSchema() },
45-
{ $"{rootNamespaceName}{InnerErrorClassName}", CreateInnerErrorSchema(context) }
46+
{ $"{rootNamespaceName}{InnerErrorClassName}", CreateInnerErrorSchema(context, document) }
4647
};
4748
}
4849

@@ -64,7 +65,8 @@ public static string GetErrorNamespaceName(this ODataContext context) {
6465
/// </summary>
6566
/// <returns>The created <see cref="OpenApiSchema"/>.</returns>
6667
/// <param name="rootNamespaceName">The root namespace name. With a trailing dot.</param>
67-
public static OpenApiSchema CreateErrorSchema(string rootNamespaceName)
68+
/// <param name="document">The Open API document to lookup references.</param>
69+
public static OpenApiSchema CreateErrorSchema(string rootNamespaceName, OpenApiDocument document)
6870
{
6971
return new OpenApiSchema
7072
{
@@ -77,7 +79,7 @@ public static OpenApiSchema CreateErrorSchema(string rootNamespaceName)
7779
{
7880
{
7981
"error",
80-
new OpenApiSchemaReference($"{rootNamespaceName}{MainErrorClassName}", null)
82+
new OpenApiSchemaReference($"{rootNamespaceName}{MainErrorClassName}", document)
8183
}
8284
}
8385
};
@@ -88,8 +90,9 @@ public static OpenApiSchema CreateErrorSchema(string rootNamespaceName)
8890
/// Otherwise, a default inner error type of object will be created.
8991
/// </summary>
9092
/// <param name="context">The OData to Open API context.</param>
93+
/// <param name="document">The Open API document to lookup references.</param>
9194
/// <returns>The inner error schema definition.</returns>
92-
public static OpenApiSchema CreateInnerErrorSchema(ODataContext context)
95+
public static OpenApiSchema CreateInnerErrorSchema(ODataContext context, OpenApiDocument document)
9396
{
9497
Utils.CheckArgumentNull(context, nameof(context));
9598

@@ -98,7 +101,7 @@ public static OpenApiSchema CreateInnerErrorSchema(ODataContext context)
98101
!string.IsNullOrEmpty(rootNamespace) &&
99102
context.Model.FindDeclaredType($"{rootNamespace}.{context.Settings.InnerErrorComplexTypeName}") is IEdmComplexType complexType)
100103
{
101-
return context.CreateSchemaTypeSchema(complexType);
104+
return context.CreateSchemaTypeSchema(complexType, document);
102105
}
103106

104107
return new OpenApiSchema
@@ -112,8 +115,9 @@ public static OpenApiSchema CreateInnerErrorSchema(ODataContext context)
112115
/// Create <see cref="OpenApiSchema"/> for main property of the error.
113116
/// </summary>
114117
/// <param name="rootNamespaceName">The root namespace name. With a trailing dot.</param>
118+
/// <param name="document">The Open API document to lookup references.</param>
115119
/// <returns>The created <see cref="OpenApiSchema"/>.</returns>
116-
public static OpenApiSchema CreateErrorMainSchema(string rootNamespaceName)
120+
public static OpenApiSchema CreateErrorMainSchema(string rootNamespaceName, OpenApiDocument document)
117121
{
118122
return new OpenApiSchema
119123
{
@@ -139,12 +143,12 @@ public static OpenApiSchema CreateErrorMainSchema(string rootNamespaceName)
139143
new OpenApiSchema
140144
{
141145
Type = JsonSchemaType.Array,
142-
Items = new OpenApiSchemaReference($"{rootNamespaceName}{ErrorDetailsClassName}", null)
146+
Items = new OpenApiSchemaReference($"{rootNamespaceName}{ErrorDetailsClassName}", document)
143147
}
144148
},
145149
{
146150
"innerError",
147-
new OpenApiSchemaReference($"{rootNamespaceName}{InnerErrorClassName}", null)
151+
new OpenApiSchemaReference($"{rootNamespaceName}{InnerErrorClassName}", document)
148152
}
149153
}
150154
};

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

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -51,27 +51,32 @@ public static IDictionary<string, OpenApiParameter> CreateParameters(this ODataC
5151
/// </summary>
5252
/// <param name="context">The OData context.</param>
5353
/// <param name="functionImport">The Edm function import.</param>
54+
/// <param name="document">The Open API document to lookup references.</param>
5455
/// <returns>The created list of <see cref="OpenApiParameter"/>.</returns>
55-
public static IList<OpenApiParameter> CreateParameters(this ODataContext context, IEdmFunctionImport functionImport)
56+
public static IList<OpenApiParameter> CreateParameters(this ODataContext context, IEdmFunctionImport functionImport, OpenApiDocument document)
5657
{
5758
Utils.CheckArgumentNull(context, nameof(context));
5859
Utils.CheckArgumentNull(functionImport, nameof(functionImport));
60+
Utils.CheckArgumentNull(document, nameof(document));
5961

60-
return context.CreateParameters(functionImport.Function);
62+
return context.CreateParameters(functionImport.Function, document);
6163
}
6264

6365
/// <summary>
6466
/// Create the list of <see cref="OpenApiParameter"/> for a <see cref="IEdmFunction"/>.
6567
/// </summary>
6668
/// <param name="context">The OData context.</param>
6769
/// <param name="function">The Edm function.</param>
70+
/// <param name="document">The Open API document to lookup references.</param>
6871
/// <param name="parameterNameMapping">The parameter name mapping.</param>
6972
/// <returns>The created list of <see cref="OpenApiParameter"/>.</returns>
7073
public static IList<OpenApiParameter> CreateParameters(this ODataContext context, IEdmFunction function,
74+
OpenApiDocument document,
7175
IDictionary<string, string> parameterNameMapping = null)
7276
{
7377
Utils.CheckArgumentNull(context, nameof(context));
7478
Utils.CheckArgumentNull(function, nameof(function));
79+
Utils.CheckArgumentNull(document, nameof(document));
7580

7681
IList<OpenApiParameter> parameters = new List<OpenApiParameter>();
7782
int skip = function.IsBound ? 1 : 0;
@@ -131,7 +136,7 @@ public static IList<OpenApiParameter> CreateParameters(this ODataContext context
131136
Name = parameterNameMapping == null ? edmParameter.Name : parameterNameMapping[edmParameter.Name],
132137
In = isOptionalParameter ? ParameterLocation.Query : ParameterLocation.Path,
133138
Required = !isOptionalParameter,
134-
Schema = context.CreateEdmTypeSchema(edmParameter.Type)
139+
Schema = context.CreateEdmTypeSchema(edmParameter.Type, document)
135140
};
136141
}
137142

@@ -154,16 +159,19 @@ public static IList<OpenApiParameter> CreateParameters(this ODataContext context
154159
/// </summary>
155160
/// <param name="context">The OData context.</param>
156161
/// <param name="keySegment">The key segment.</param>
162+
/// <param name="document">The Open API document to lookup references.</param>
157163
/// <param name="parameterNameMapping">The parameter name mapping.</param>
158164
/// <returns>The created list of <see cref="OpenApiParameter"/>.</returns>
159165
public static IList<OpenApiParameter> CreateKeyParameters(this ODataContext context, ODataKeySegment keySegment,
166+
OpenApiDocument document,
160167
IDictionary<string, string> parameterNameMapping = null)
161168
{
162169
Utils.CheckArgumentNull(context, nameof(context));
163170
Utils.CheckArgumentNull(keySegment, nameof(keySegment));
171+
Utils.CheckArgumentNull(document, nameof(document));
164172

165173
if (keySegment.IsAlternateKey)
166-
return CreateAlternateKeyParameters(context, keySegment);
174+
return CreateAlternateKeyParameters(context, keySegment, document);
167175

168176
IEdmEntityType entityType = keySegment.EntityType;
169177
IList<IEdmStructuralProperty> keys = entityType.Key().ToList();
@@ -186,7 +194,7 @@ public static IList<OpenApiParameter> CreateKeyParameters(this ODataContext cont
186194
In = ParameterLocation.Path,
187195
Required = true,
188196
Description = $"The unique identifier of {entityType.Name}",
189-
Schema = context.CreateEdmTypeSchema(keys.First().Type)
197+
Schema = context.CreateEdmTypeSchema(keys[0].Type, document)
190198
};
191199

192200
parameter.Extensions.Add(Constants.xMsKeyType, new OpenApiAny(entityType.Name));
@@ -205,7 +213,7 @@ public static IList<OpenApiParameter> CreateKeyParameters(this ODataContext cont
205213
In = ParameterLocation.Path,
206214
Required = true,
207215
Description = $"Property in multi-part unique identifier of {entityType.Name}",
208-
Schema = context.CreateEdmTypeSchema(keyProperty.Type)
216+
Schema = context.CreateEdmTypeSchema(keyProperty.Type, document)
209217
};
210218

211219
if (keySegment.KeyMappings != null)
@@ -227,8 +235,9 @@ public static IList<OpenApiParameter> CreateKeyParameters(this ODataContext cont
227235
/// </summary>
228236
/// <param name="context">The OData context.</param>
229237
/// <param name="keySegment">The key segment.</param>
238+
/// <param name="document">The Open API document to lookup references.</param>
230239
/// <returns>A list of <see cref="OpenApiParameter"/> of alternate key parameters.</returns>
231-
private static IList<OpenApiParameter> CreateAlternateKeyParameters(ODataContext context, ODataSegment keySegment)
240+
private static IList<OpenApiParameter> CreateAlternateKeyParameters(ODataContext context, ODataSegment keySegment, OpenApiDocument document)
232241
{
233242
Debug.Assert(keySegment.Kind == ODataSegmentKind.Key);
234243

@@ -248,7 +257,7 @@ private static IList<OpenApiParameter> CreateAlternateKeyParameters(ODataContext
248257
Name = alternateKey.First().Key,
249258
In = ParameterLocation.Path,
250259
Description = $"Alternate key of {entityType.Name}",
251-
Schema = context.CreateEdmTypeSchema(alternateKey.First().Value.Type),
260+
Schema = context.CreateEdmTypeSchema(alternateKey.First().Value.Type, document),
252261
Required = true
253262
}
254263
);
@@ -266,7 +275,7 @@ private static IList<OpenApiParameter> CreateAlternateKeyParameters(ODataContext
266275
Name = compositekey.Key,
267276
In = ParameterLocation.Path,
268277
Description = $"Property in multi-part alternate key of {entityType.Name}",
269-
Schema = context.CreateEdmTypeSchema(compositekey.Value.Type),
278+
Schema = context.CreateEdmTypeSchema(compositekey.Value.Type, document),
270279
Required = true
271280
}
272281
);
@@ -282,16 +291,17 @@ private static IList<OpenApiParameter> CreateAlternateKeyParameters(ODataContext
282291
/// </summary>
283292
/// <param name="path">The ODataPath</param>
284293
/// <param name="context">The OData context.</param>
294+
/// <param name="document">The Open API document to lookup references.</param>
285295
/// <returns>The created list of <see cref="OpenApiParameter"/></returns>
286-
public static List<OpenApiParameter> CreatePathParameters(this ODataPath path, ODataContext context)
296+
public static List<OpenApiParameter> CreatePathParameters(this ODataPath path, ODataContext context, OpenApiDocument document)
287297
{
288298
List<OpenApiParameter> pathParameters = [];
289299
var parameterMappings = path.CalculateParameterMapping(context.Settings);
290300

291301
foreach (ODataKeySegment keySegment in path.OfType<ODataKeySegment>())
292302
{
293303
IDictionary<string, string> mapping = parameterMappings[keySegment];
294-
pathParameters.AddRange(context.CreateKeyParameters(keySegment, mapping));
304+
pathParameters.AddRange(context.CreateKeyParameters(keySegment, document, mapping));
295305
}
296306

297307
foreach (ODataOperationSegment operationSegment in path.OfType<ODataOperationSegment>())
@@ -303,7 +313,7 @@ public static List<OpenApiParameter> CreatePathParameters(this ODataPath path, O
303313

304314
if (operationSegment.ParameterMappings != null)
305315
{
306-
IList<OpenApiParameter> parameters = context.CreateParameters(function, operationSegment.ParameterMappings);
316+
IList<OpenApiParameter> parameters = context.CreateParameters(function, document, operationSegment.ParameterMappings);
307317
foreach (var parameter in parameters)
308318
{
309319
pathParameters.AppendParameter(parameter);
@@ -312,7 +322,7 @@ public static List<OpenApiParameter> CreatePathParameters(this ODataPath path, O
312322
else
313323
{
314324
IDictionary<string, string> mappings = parameterMappings[operationSegment];
315-
IList<OpenApiParameter> parameters = context.CreateParameters(function, mappings);
325+
IList<OpenApiParameter> parameters = context.CreateParameters(function, document, mappings);
316326
pathParameters.AddRange(parameters);
317327
}
318328
}

0 commit comments

Comments
 (0)