Skip to content

Commit a17d529

Browse files
committed
fix: aligns to add pattern for example, components, security schemes, paths
Signed-off-by: Vincent Biret <[email protected]>
1 parent 8c1efe9 commit a17d529

12 files changed

+271
-213
lines changed

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,20 @@ internal static class OpenApiComponentsGenerator
2121
/// </summary>
2222
/// <param name="context">The OData to Open API context.</param>
2323
/// <param name="document">The Open API document.</param>
24-
/// <returns>The created <see cref="OpenApiComponents"/> object.</returns>
25-
public static OpenApiComponents CreateComponents(this ODataContext context, OpenApiDocument document)
24+
public static void AddComponentsToDocument(this ODataContext context, OpenApiDocument document)
2625
{
2726
Utils.CheckArgumentNull(context, nameof(context));
27+
Utils.CheckArgumentNull(document, nameof(document));
2828

2929
context.AddSchemasToDocument(document);
3030
context.AddParametersToDocument(document);
3131
context.AddResponsesToDocument(document);
3232
context.AddRequestBodiesToDocument(document);
33-
//TODO convert all other create methods to add
34-
context.CreateExamples(document);
35-
context.CreateSecuritySchemes();
33+
context.AddExamplesToDocument(document);
34+
context.AddSecuritySchemesToDocument(document);
3635
document.Components.Links = null;
3736
document.Components.Callbacks = null;
3837
document.Components.Extensions = null;
39-
return document.Components;
4038
}
4139
}
4240
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ public static OpenApiDocument CreateDocument(this ODataContext context)
4343
ExternalDocs = null,
4444
};
4545

46-
doc.Components = context.CreateComponents(doc);
47-
doc.Paths = context.CreatePaths(doc);
46+
context.AddComponentsToDocument(doc);
47+
context.AddPathsToDocument(doc);
4848
doc.Tags = context.CreateTags(); // order matters so the operation generators have populated the tags
4949

5050

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

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System;
77
using System.Collections.Generic;
88
using System.Diagnostics;
9+
using System.Linq;
910
using Microsoft.OData.Edm;
1011
using Microsoft.OpenApi.Models;
1112
using Microsoft.OpenApi.OData.Common;
@@ -23,37 +24,27 @@ internal static class OpenApiExampleGenerator
2324
/// </summary>
2425
/// <param name="context">The OData to Open API context.</param>
2526
/// <param name="document">The Open API document.</param>
26-
/// <returns>The created <see cref="OpenApiExample"/> dictionary.</returns>
27-
public static IDictionary<string, OpenApiExample> CreateExamples(this ODataContext context, OpenApiDocument document)
27+
public static void AddExamplesToDocument(this ODataContext context, OpenApiDocument document)
2828
{
2929
Utils.CheckArgumentNull(context, nameof(context));
3030
Utils.CheckArgumentNull(document, nameof(document));
3131

32-
IDictionary<string, OpenApiExample> examples = new Dictionary<string, OpenApiExample>();
3332

3433
// Each entity type, complex type, enumeration type, and type definition directly
3534
// or indirectly used in the paths field is represented as a name / value pair of the schemas map.
3635
// Ideally this would be driven off the types used in the paths, but in practice, it is simply
3736
// all of the types present in the model.
38-
IEnumerable<IEdmSchemaElement> elements = context.Model.GetAllElements();
37+
var elements = context.Model.GetAllElements()
38+
.Where(static x => x.SchemaElementKind is EdmSchemaElementKind.TypeDefinition)
39+
.OfType<IEdmType>();
3940

4041
foreach (var element in elements)
4142
{
42-
switch (element.SchemaElementKind)
43+
if (context.CreateExample(element, document) is OpenApiExample example)
4344
{
44-
case EdmSchemaElementKind.TypeDefinition when element is IEdmType reference: // Type definition
45-
{
46-
OpenApiExample example = context.CreateExample(reference, document);
47-
if (example != null)
48-
{
49-
examples.Add(reference.FullTypeName(), example);
50-
}
51-
}
52-
break;
45+
document.AddComponent(element.FullTypeName(), example);
5346
}
5447
}
55-
56-
return examples;
5748
}
5849

5950
private static OpenApiExample CreateExample(this ODataContext context, IEdmType edmType, OpenApiDocument document)

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

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,17 @@ internal static class OpenApiPathItemGenerator
2323
/// </summary>
2424
/// <param name="context">The OData context.</param>
2525
/// <param name="document">The Open API document to use to lookup references.</param>
26-
/// <returns>The created map of <see cref="OpenApiPathItem"/>.</returns>
27-
public static IDictionary<string, OpenApiPathItem> CreatePathItems(this ODataContext context, OpenApiDocument document)
26+
public static void AddPathItemsToDocument(this ODataContext context, OpenApiDocument document)
2827
{
2928
Utils.CheckArgumentNull(context, nameof(context));
29+
Utils.CheckArgumentNull(document, nameof(document));
3030

31-
IDictionary<string, OpenApiPathItem> pathItems = new Dictionary<string, OpenApiPathItem>();
3231
if (context.EntityContainer == null)
3332
{
34-
return pathItems;
33+
return;
3534
}
3635

36+
document.Paths ??= [];
3737
OpenApiConvertSettings settings = context.Settings.Clone();
3838
settings.EnableKeyAsSegment = context.KeyAsSegment;
3939
foreach (ODataPath path in context.AllPaths)
@@ -50,7 +50,7 @@ public static IDictionary<string, OpenApiPathItem> CreatePathItems(this ODataCon
5050
continue;
5151
}
5252

53-
pathItems.TryAddPath(context, path, pathItem);
53+
document.Paths.TryAddPath(context, path, pathItem);
5454
}
5555

5656
if (settings.ShowRootPath)
@@ -73,10 +73,8 @@ public static IDictionary<string, OpenApiPathItem> CreatePathItems(this ODataCon
7373
}
7474
}
7575
};
76-
pathItems.Add("/", rootPath);
76+
document.Paths.Add("/", rootPath);
7777
}
78-
79-
return pathItems;
8078
}
8179

8280
private static IDictionary<string, OpenApiLink> CreateRootLinks(IEdmEntityContainer entityContainer)

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

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,15 @@ internal static class OpenApiPathsGenerator
2323
/// </summary>
2424
/// <param name="context">The OData context.</param>
2525
/// <param name="document">The Open API document to use to lookup references.</param>
26-
/// <returns>The created <see cref="OpenApiPaths"/> object.</returns>
27-
public static OpenApiPaths CreatePaths(this ODataContext context, OpenApiDocument document)
26+
public static void AddPathsToDocument(this ODataContext context, OpenApiDocument document)
2827
{
2928
Utils.CheckArgumentNull(context, nameof(context));
3029
Utils.CheckArgumentNull(document, nameof(document));
3130

3231
// Due to the power and flexibility of OData a full representation of all service capabilities
3332
// in the Paths Object is typically not feasible, so this mapping only describes the minimum
3433
// information desired in the Paths Object.
35-
OpenApiPaths paths = [];
36-
foreach (var item in context.CreatePathItems(document))
37-
{
38-
paths.Add(item.Key, item.Value);
39-
}
40-
41-
return paths;
34+
context.AddPathItemsToDocument(document);
4235
}
4336
}
4437
}

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

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,15 @@ internal static class OpenApiSecuritySchemeGenerator
2424
/// The name of each pair is the name of authorization. The value of each pair is a <see cref="OpenApiSecurityScheme"/>.
2525
/// </summary>
2626
/// <param name="context">The OData to Open API context.</param>
27-
/// <returns>The string/security scheme dictionary.</returns>
28-
public static IDictionary<string, OpenApiSecurityScheme> CreateSecuritySchemes(this ODataContext context)
27+
/// <param name="document">The Open API document.</param>
28+
public static void AddSecuritySchemesToDocument(this ODataContext context, OpenApiDocument document)
2929
{
3030
Utils.CheckArgumentNull(context, nameof(context));
31+
Utils.CheckArgumentNull(document, nameof(document));
3132

32-
if (context.Model == null || context.Model.EntityContainer == null)
33+
if (context.Model == null || context.Model.EntityContainer == null || context.Model.GetAuthorizations(context.EntityContainer) is not {} authorizations)
3334
{
34-
return null;
35-
}
36-
37-
IDictionary<string, OpenApiSecurityScheme> securitySchemes = new Dictionary<string, OpenApiSecurityScheme>();
38-
var authorizations = context.Model.GetAuthorizations(context.EntityContainer);
39-
if (authorizations == null)
40-
{
41-
return securitySchemes;
35+
return;
4236
}
4337

4438
foreach (var authorization in authorizations)
@@ -68,10 +62,8 @@ public static IDictionary<string, OpenApiSecurityScheme> CreateSecuritySchemes(t
6862
break;
6963
}
7064

71-
securitySchemes[authorization.Name] = scheme;
65+
document.AddComponent(authorization.Name, scheme);
7266
}
73-
74-
return securitySchemes;
7567
}
7668

7769
private static void AppendApiKey(OpenApiSecurityScheme scheme, ApiKey apiKey)

test/Microsoft.OpenAPI.OData.Reader.Tests/Generator/OpenApiComponentsGeneratorTests.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using Microsoft.OpenApi.Models;
99
using Microsoft.OpenApi.OData.Edm;
1010
using Microsoft.OpenApi.OData.Tests;
11+
using Moq;
1112
using Xunit;
1213

1314
namespace Microsoft.OpenApi.OData.Generator.Tests
@@ -20,9 +21,11 @@ public void CreateComponentsThrowArgumentNullContext()
2021
// Arrange
2122
ODataContext context = null;
2223
OpenApiDocument openApiDocument = new();
24+
var mockModel = new Mock<IEdmModel>().Object;
2325

2426
// Act & Assert
25-
Assert.Throws<ArgumentNullException>("context", () => context.CreateComponents(openApiDocument));
27+
Assert.Throws<ArgumentNullException>("context", () => context.AddComponentsToDocument(openApiDocument));
28+
Assert.Throws<ArgumentNullException>("document", () => new ODataContext(mockModel).AddComponentsToDocument(null));
2629
}
2730

2831
[Fact]
@@ -34,7 +37,8 @@ public void CreateComponentsReturnsForEmptyModel()
3437
OpenApiDocument openApiDocument = new();
3538

3639
// Act
37-
var components = context.CreateComponents(openApiDocument);
40+
context.AddComponentsToDocument(openApiDocument);
41+
var components = openApiDocument.Components;
3842

3943
// Assert
4044
Assert.NotNull(components);

test/Microsoft.OpenAPI.OData.Reader.Tests/Generator/OpenApiPathItemGeneratorTests.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public void CreatePathItemsThrowArgumentNullContext()
2727
ODataContext context = null;
2828

2929
// Act & Assert
30-
Assert.Throws<ArgumentNullException>("context", () => context.CreatePathItems(openApiDocument));
30+
Assert.Throws<ArgumentNullException>("context", () => context.AddPathItemsToDocument(openApiDocument));
3131
}
3232

3333
[Fact]
@@ -39,7 +39,8 @@ public void CreatePathItemsReturnsForEmptyModel()
3939
ODataContext context = new ODataContext(model);
4040

4141
// Act
42-
var pathItems = context.CreatePathItems(openApiDocument);
42+
context.AddPathItemsToDocument(openApiDocument);
43+
var pathItems = openApiDocument.Paths;
4344

4445
// Assert
4546
Assert.NotNull(pathItems);
@@ -62,7 +63,8 @@ public void CreatePathItemsReturnsForBasicModel(bool useAnnotationToGeneratePath
6263
ODataContext context = new ODataContext(model, settings);
6364

6465
// Act
65-
var pathItems = context.CreatePathItems(openApiDocument);
66+
context.AddPathItemsToDocument(openApiDocument);
67+
var pathItems = openApiDocument.Paths;
6668

6769
// Assert
6870
Assert.NotNull(pathItems);
@@ -149,7 +151,8 @@ public void CreatePathItemsReturnsForEscapeFunctionModel(bool enableEscaped, boo
149151
ODataContext context = new ODataContext(model, settings);
150152

151153
// Act
152-
var pathItems = context.CreatePathItems(openApiDocument);
154+
context.AddPathItemsToDocument(openApiDocument);
155+
var pathItems = openApiDocument.Paths;
153156

154157
// Assert
155158
Assert.NotNull(pathItems);
@@ -205,7 +208,8 @@ public void CreatePathItemsDoesNotAddPathItemEntryForPathItemsWithNoOperations()
205208
ODataContext context = new(model);
206209

207210
// Act
208-
var pathItems = context.CreatePathItems(openApiDocument);
211+
context.AddPathItemsToDocument(openApiDocument);
212+
var pathItems = openApiDocument.Paths;
209213

210214
// Assert
211215
Assert.NotNull(pathItems);

test/Microsoft.OpenAPI.OData.Reader.Tests/Generator/OpenApiPathsGeneratorTests.cs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,24 @@
88
using Microsoft.OpenApi.Models;
99
using Microsoft.OpenApi.OData.Edm;
1010
using Microsoft.OpenApi.OData.Tests;
11+
using Moq;
1112
using Xunit;
1213

1314
namespace Microsoft.OpenApi.OData.Generator.Tests
1415
{
1516
public class OpenApiPathsGeneratorTest
1617
{
17-
private OpenApiConvertSettings _settings = new OpenApiConvertSettings();
18-
1918
[Fact]
2019
public void CreatePathsThrowArgumentNullContext()
2120
{
2221
// Arrange
2322
OpenApiDocument openApiDocument = new();
2423
ODataContext context = null;
24+
var mockModel = new Mock<IEdmModel>().Object;
2525

2626
// Act & Assert
27-
Assert.Throws<ArgumentNullException>("context", () => context.CreatePaths(openApiDocument));
27+
Assert.Throws<ArgumentNullException>("context", () => context.AddPathsToDocument(openApiDocument));
28+
Assert.Throws<ArgumentNullException>("document", () => new ODataContext(mockModel).AddPathsToDocument(null));
2829
}
2930

3031
[Fact]
@@ -36,7 +37,8 @@ public void CreatePathsReturnsForEmptyModel()
3637
ODataContext context = new ODataContext(model);
3738

3839
// Act
39-
var paths = context.CreatePaths(openApiDocument);
40+
context.AddPathsToDocument(openApiDocument);
41+
var paths = openApiDocument.Paths;
4042

4143
// Assert
4244
Assert.NotNull(paths);
@@ -59,7 +61,8 @@ public void CreatePathsReturnsForBasicModel(bool useAnnotationToGeneratePath, in
5961
ODataContext context = new ODataContext(model, settings);
6062

6163
// Act
62-
var paths = context.CreatePaths(openApiDocument);
64+
context.AddPathsToDocument(openApiDocument);
65+
var paths = openApiDocument.Paths;
6366

6467
// Assert
6568
Assert.NotNull(paths);
@@ -120,7 +123,8 @@ public void CreatePathsReturnsForBasicModelWithPrefix(bool useAnnotationToGenera
120123
ODataContext context = new ODataContext(model, settings);
121124

122125
// Act
123-
var paths = context.CreatePaths(openApiDocument);
126+
context.AddPathsToDocument(openApiDocument);
127+
var paths = openApiDocument.Paths;
124128

125129
// Assert
126130
Assert.NotNull(paths);
@@ -178,7 +182,8 @@ public void CreatePathsReturnsForContractModelWithHierarhicalClass()
178182
ODataContext context = new ODataContext(model, settings);
179183

180184
// Act
181-
var paths = context.CreatePaths(openApiDocument);
185+
context.AddPathsToDocument(openApiDocument);
186+
var paths = openApiDocument.Paths;
182187

183188
// Assert
184189
Assert.NotNull(paths);

test/Microsoft.OpenAPI.OData.Reader.Tests/Generator/OpenApiSecuritySchemeGeneratorTests.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,11 @@ public async Task CreateSecuritySchemesWorksForAuthorizationsOnEntitySetContaine
2626
{
2727
// Arrange
2828
ODataContext context = new ODataContext(GetEdmModel());
29+
OpenApiDocument openApiDocument = new();
2930

3031
// Act
31-
var schemes = context.CreateSecuritySchemes();
32+
context.AddSecuritySchemesToDocument(openApiDocument);
33+
var schemes = openApiDocument.Components.SecuritySchemes;
3234

3335
// Assert
3436
Assert.NotNull(schemes);

0 commit comments

Comments
 (0)