-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathOpenApiExampleGenerator.cs
More file actions
63 lines (56 loc) · 2.66 KB
/
OpenApiExampleGenerator.cs
File metadata and controls
63 lines (56 loc) · 2.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// ------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information.
// ------------------------------------------------------------
using System.Diagnostics;
using System.Linq;
using Microsoft.OData.Edm;
using Microsoft.OpenApi.OData.Common;
using Microsoft.OpenApi.OData.Edm;
namespace Microsoft.OpenApi.OData.Generator
{
/// <summary>
/// Extension methods to create <see cref="OpenApiExample"/> by <see cref="ODataContext"/>.
/// </summary>
internal static class OpenApiExampleGenerator
{
/// <summary>
/// Create the dictionary of <see cref="OpenApiExample"/> object.
/// </summary>
/// <param name="context">The OData to Open API context.</param>
/// <param name="document">The Open API document.</param>
public static void AddExamplesToDocument(this ODataContext context, OpenApiDocument document)
{
Utils.CheckArgumentNull(context, nameof(context));
Utils.CheckArgumentNull(document, nameof(document));
// Each entity type, complex type, enumeration type, and type definition directly
// or indirectly used in the paths field is represented as a name / value pair of the schemas map.
// Ideally this would be driven off the types used in the paths, but in practice, it is simply
// all of the types present in the model.
var elements = context.Model.GetAllElements()
.Where(static x => x.SchemaElementKind is EdmSchemaElementKind.TypeDefinition)
.OfType<IEdmType>();
foreach (var element in elements)
{
if (context.CreateExample(element, document) is OpenApiExample example)
{
document.AddComponent(element.FullTypeName(), example);
}
}
}
private static OpenApiExample? CreateExample(this ODataContext context, IEdmType edmType, OpenApiDocument document)
{
Debug.Assert(context != null);
Debug.Assert(edmType != null);
return edmType.TypeKind switch
{
// complex type
EdmTypeKind.Complex or EdmTypeKind.Entity when edmType is IEdmStructuredType edmStructuredType => new()
{
Value = OpenApiSchemaGenerator.CreateStructuredTypePropertiesExample(context, edmStructuredType, document),
},
_ => null,
};
}
}
}