Skip to content

Commit 78b65b5

Browse files
committed
Merge remote-tracking branch 'origin/mk/integrate-json-schema-library' into mk/integrate-json-schema-library
2 parents 76a41de + 8072bad commit 78b65b5

14 files changed

+61
-86
lines changed

src/Microsoft.OpenApi.Readers/SchemaTypeConverter.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) Microsoft Corporation. All rights reserved.
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT license.
33

44
using System;
@@ -10,8 +10,7 @@ internal static class SchemaTypeConverter
1010
{
1111
internal static SchemaValueType ConvertToSchemaValueType(string value)
1212
{
13-
value = value.ToLowerInvariant();
14-
return value switch
13+
return value.ToLowerInvariant() switch
1514
{
1615
"string" => SchemaValueType.String,
1716
"number" or "double" => SchemaValueType.Number,

src/Microsoft.OpenApi.Readers/V3/OpenApiParameterDeserializer.cs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,9 @@ internal static partial class OpenApiV3Deserializer
2929
{
3030
var inString = n.GetScalarValue();
3131

32-
if ( Enum.GetValues(typeof(ParameterLocation)).Cast<ParameterLocation>()
32+
o.In = Enum.GetValues(typeof(ParameterLocation)).Cast<ParameterLocation>()
3333
.Select( e => e.GetDisplayName() )
34-
.Contains(inString) )
35-
{
36-
o.In = n.GetScalarValue().GetEnumFromDisplayName<ParameterLocation>();
37-
}
38-
else
39-
{
40-
o.In = null;
41-
}
34+
.Contains(inString) ? n.GetScalarValue().GetEnumFromDisplayName<ParameterLocation>() : null;
4235
}
4336
},
4437
{

src/Microsoft.OpenApi.Readers/V31/OpenApiParameterDeserializer.cs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,10 @@ internal static partial class OpenApiV31Deserializer
2525
"in", (o, n) =>
2626
{
2727
var inString = n.GetScalarValue();
28-
29-
if ( Enum.GetValues(typeof(ParameterLocation)).Cast<ParameterLocation>()
28+
o.In = Enum.GetValues(typeof(ParameterLocation)).Cast<ParameterLocation>()
3029
.Select( e => e.GetDisplayName() )
31-
.Contains(inString) )
32-
{
33-
o.In = n.GetScalarValue().GetEnumFromDisplayName<ParameterLocation>();
34-
}
35-
else
36-
{
37-
o.In = null;
38-
}
30+
.Contains(inString) ? n.GetScalarValue().GetEnumFromDisplayName<ParameterLocation>() : null;
31+
3932
}
4033
},
4134
{

src/Microsoft.OpenApi.Readers/V31/OpenApiV31Deserializer.cs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -134,14 +134,9 @@ public static OpenApiAny LoadAny(ParseNode node)
134134

135135
private static IOpenApiExtension LoadExtension(string name, ParseNode node)
136136
{
137-
if (node.Context.ExtensionParsers.TryGetValue(name, out var parser))
138-
{
139-
return parser(node.CreateAny(), OpenApiSpecVersion.OpenApi3_1);
140-
}
141-
else
142-
{
143-
return node.CreateAny();
144-
}
137+
return node.Context.ExtensionParsers.TryGetValue(name, out var parser)
138+
? parser(node.CreateAny(), OpenApiSpecVersion.OpenApi3_1)
139+
: node.CreateAny();
145140
}
146141

147142
private static string LoadString(ParseNode node)

src/Microsoft.OpenApi/Extensions/JsonSchemaExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public static string GetSummary(this JsonSchema schema)
7575
/// <returns></returns>
7676
public static IDictionary<string, IOpenApiExtension> GetExtensions(this JsonSchema schema)
7777
{
78-
return (Dictionary<string, IOpenApiExtension>)(schema.TryGetKeyword<ExtensionsKeyword>(ExtensionsKeyword.Name, out var k) ? k.Extensions! : null);
78+
return schema.TryGetKeyword<ExtensionsKeyword>(ExtensionsKeyword.Name, out var k) ? k.Extensions! : null;
7979
}
8080
}
8181
}

src/Microsoft.OpenApi/Helpers/SchemaSerializerHelper.cs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
// Copyright (c) Microsoft Corporation. All rights reserved.
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT license.
33

44
using System.Collections.Generic;
5+
using System.Linq;
56
using Json.Schema;
67
using Microsoft.OpenApi.Extensions;
78
using Microsoft.OpenApi.Interfaces;
@@ -100,13 +101,10 @@ private static string RetrieveFormatFromNestedSchema(IReadOnlyCollection<JsonSch
100101
{
101102
if (schema != null)
102103
{
103-
foreach (var item in schema)
104-
{
105-
if (!string.IsNullOrEmpty(item.GetFormat()?.Key))
106-
{
107-
return item.GetFormat().Key;
108-
}
109-
}
104+
return schema
105+
.Where(item => !string.IsNullOrEmpty(item.GetFormat()?.Key))
106+
.Select(item => item.GetFormat().Key)
107+
.FirstOrDefault();
110108
}
111109

112110
return null;

src/Microsoft.OpenApi/Models/OpenApiHeader.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ namespace Microsoft.OpenApi.Models
1919
/// </summary>
2020
public class OpenApiHeader : IOpenApiSerializable, IOpenApiReferenceable, IOpenApiExtensible, IEffective<OpenApiHeader>
2121
{
22+
protected JsonSchema _schema;
23+
2224
/// <summary>
2325
/// Indicates if object is populated with data or is just a reference to the data
2426
/// </summary>
@@ -66,9 +68,13 @@ public class OpenApiHeader : IOpenApiSerializable, IOpenApiReferenceable, IOpenA
6668
public virtual bool AllowReserved { get; set; }
6769

6870
/// <summary>
69-
/// The schema defining the type used for the header.
71+
/// The schema defining the type used for the request body.
7072
/// </summary>
71-
public virtual JsonSchema Schema { get; set; }
73+
public virtual JsonSchema Schema
74+
{
75+
get => _schema;
76+
set => _schema = value;
77+
}
7278

7379
/// <summary>
7480
/// Example of the media type.
@@ -109,7 +115,7 @@ public OpenApiHeader(OpenApiHeader header)
109115
Style = header?.Style ?? Style;
110116
Explode = header?.Explode ?? Explode;
111117
AllowReserved = header?.AllowReserved ?? AllowReserved;
112-
Schema = JsonNodeCloneHelper.CloneJsonSchema(Schema);
118+
_schema = JsonNodeCloneHelper.CloneJsonSchema(header?.Schema);
113119
Example = JsonNodeCloneHelper.Clone(header?.Example);
114120
Examples = header?.Examples != null ? new Dictionary<string, OpenApiExample>(header.Examples) : null;
115121
Content = header?.Content != null ? new Dictionary<string, OpenApiMediaType>(header.Content) : null;

src/Microsoft.OpenApi/Models/OpenApiMediaType.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,16 @@ namespace Microsoft.OpenApi.Models
1717
/// </summary>
1818
public class OpenApiMediaType : IOpenApiSerializable, IOpenApiExtensible
1919
{
20+
private JsonSchema _schema;
21+
2022
/// <summary>
2123
/// The schema defining the type used for the request body.
2224
/// </summary>
23-
public virtual JsonSchema Schema { get; set; }
25+
public virtual JsonSchema Schema
26+
{
27+
get => _schema;
28+
set => _schema = value;
29+
}
2430

2531
/// <summary>
2632
/// Example of the media type.
@@ -57,7 +63,7 @@ public OpenApiMediaType() { }
5763
/// </summary>
5864
public OpenApiMediaType(OpenApiMediaType mediaType)
5965
{
60-
Schema = JsonNodeCloneHelper.CloneJsonSchema(Schema);
66+
_schema = JsonNodeCloneHelper.CloneJsonSchema(mediaType?.Schema);
6167
Example = JsonNodeCloneHelper.Clone(mediaType?.Example);
6268
Examples = mediaType?.Examples != null ? new Dictionary<string, OpenApiExample>(mediaType.Examples) : null;
6369
Encoding = mediaType?.Encoding != null ? new Dictionary<string, OpenApiEncoding>(mediaType.Encoding) : null;

src/Microsoft.OpenApi/Models/OpenApiParameter.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public class OpenApiParameter : IOpenApiSerializable, IOpenApiReferenceable, IEf
2020
{
2121
private bool? _explode;
2222
private ParameterStyle? _style;
23+
protected JsonSchema _schema;
2324

2425
/// <summary>
2526
/// Indicates if object is populated with data or is just a reference to the data
@@ -107,7 +108,11 @@ public virtual bool Explode
107108
/// <summary>
108109
/// The schema defining the type used for the request body.
109110
/// </summary>
110-
public virtual JsonSchema Schema { get; set; }
111+
public virtual JsonSchema Schema
112+
{
113+
get => _schema;
114+
set => _schema = value;
115+
}
111116

112117
/// <summary>
113118
/// Examples of the media type. Each example SHOULD contain a value
@@ -163,7 +168,7 @@ public OpenApiParameter(OpenApiParameter parameter)
163168
Style = parameter?.Style ?? Style;
164169
Explode = parameter?.Explode ?? Explode;
165170
AllowReserved = parameter?.AllowReserved ?? AllowReserved;
166-
Schema = JsonNodeCloneHelper.CloneJsonSchema(Schema);
171+
_schema = JsonNodeCloneHelper.CloneJsonSchema(parameter?.Schema);
167172
Examples = parameter?.Examples != null ? new Dictionary<string, OpenApiExample>(parameter.Examples) : null;
168173
Example = JsonNodeCloneHelper.Clone(parameter?.Example);
169174
Content = parameter?.Content != null ? new Dictionary<string, OpenApiMediaType>(parameter.Content) : null;

src/Microsoft.OpenApi/Models/References/OpenApiHeaderReference.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public override string Description
7373
public override bool AllowEmptyValue { get => Target.AllowEmptyValue; set => Target.AllowEmptyValue = value; }
7474

7575
/// <inheritdoc/>
76-
public override JsonSchema Schema { get => Target.Schema; set => Target.Schema = value; }
76+
public override JsonSchema Schema { get => _schema; set => _schema = value; }
7777

7878
/// <inheritdoc/>
7979
public override ParameterStyle? Style { get => Target.Style; set => Target.Style = value; }

0 commit comments

Comments
 (0)