Skip to content

Commit 9f8bd0f

Browse files
committed
Merge branch 'feat/oai-3-2-support' into copilot/fix-3c317057-370e-4d33-8171-b4b6862334fb
2 parents 0b88c3b + bb156b8 commit 9f8bd0f

File tree

31 files changed

+489
-1
lines changed

31 files changed

+489
-1
lines changed

src/Microsoft.OpenApi/Models/Interfaces/IOpenApiExample.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,20 @@ public interface IOpenApiExample : IOpenApiDescribedElement, IOpenApiSummarizedE
2222
/// The value field and externalValue field are mutually exclusive.
2323
/// </summary>
2424
public string? ExternalValue { get; }
25+
26+
/// <summary>
27+
/// Embedded literal example value.
28+
/// The dataValue property and the value property are mutually exclusive.
29+
/// To represent examples of media types that cannot be naturally represented in JSON or YAML,
30+
/// use a string value to contain the example with escaping where necessary.
31+
/// Available in OpenAPI 3.2+, serialized as extension in 3.1 and earlier.
32+
/// </summary>
33+
public JsonNode? DataValue { get; }
34+
35+
/// <summary>
36+
/// A string representation of the example.
37+
/// This is mutually exclusive with the value and dataValue properties.
38+
/// Available in OpenAPI 3.2+, serialized as extension in 3.1 and earlier.
39+
/// </summary>
40+
public string? SerializedValue { get; }
2541
}

src/Microsoft.OpenApi/Models/OpenApiConstants.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,16 @@ public static class OpenApiConstants
345345
/// </summary>
346346
public const string ExternalValue = "externalValue";
347347

348+
/// <summary>
349+
/// Field: DataValue
350+
/// </summary>
351+
public const string DataValue = "dataValue";
352+
353+
/// <summary>
354+
/// Field: SerializedValue
355+
/// </summary>
356+
public const string SerializedValue = "serializedValue";
357+
348358
/// <summary>
349359
/// Field: DollarRef
350360
/// </summary>
@@ -565,6 +575,11 @@ public static class OpenApiConstants
565575
/// </summary>
566576
public const string AuthorizationCode = "authorizationCode";
567577

578+
/// <summary>
579+
/// Field: DeviceAuthorization
580+
/// </summary>
581+
public const string DeviceAuthorization = "deviceAuthorization";
582+
568583
/// <summary>
569584
/// Field: AuthorizationUrl
570585
/// </summary>

src/Microsoft.OpenApi/Models/OpenApiExample.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ public class OpenApiExample : IOpenApiExtensible, IOpenApiExample
2323
/// <inheritdoc/>
2424
public JsonNode? Value { get; set; }
2525

26+
/// <inheritdoc/>
27+
public JsonNode? DataValue { get; set; }
28+
29+
/// <inheritdoc/>
30+
public string? SerializedValue { get; set; }
31+
2632
/// <inheritdoc/>
2733
public IDictionary<string, IOpenApiExtension>? Extensions { get; set; }
2834

@@ -42,6 +48,8 @@ internal OpenApiExample(IOpenApiExample example)
4248
Description = example.Description ?? Description;
4349
Value = example.Value != null ? JsonNodeCloneHelper.Clone(example.Value) : null;
4450
ExternalValue = example.ExternalValue ?? ExternalValue;
51+
DataValue = example.DataValue != null ? JsonNodeCloneHelper.Clone(example.DataValue) : null;
52+
SerializedValue = example.SerializedValue ?? SerializedValue;
4553
Extensions = example.Extensions != null ? new Dictionary<string, IOpenApiExtension>(example.Extensions) : null;
4654
}
4755

@@ -84,6 +92,32 @@ private void SerializeInternal(IOpenApiWriter writer, OpenApiSpecVersion version
8492
// externalValue
8593
writer.WriteProperty(OpenApiConstants.ExternalValue, ExternalValue);
8694

95+
// dataValue - serialize as native field in v3.2+, as extension in earlier versions
96+
if (DataValue is not null)
97+
{
98+
if (version >= OpenApiSpecVersion.OpenApi3_2)
99+
{
100+
writer.WriteRequiredObject(OpenApiConstants.DataValue, DataValue, (w, v) => w.WriteAny(v));
101+
}
102+
else
103+
{
104+
writer.WriteRequiredObject(OpenApiConstants.ExtensionFieldNamePrefix + "oai-" + OpenApiConstants.DataValue, DataValue, (w, v) => w.WriteAny(v));
105+
}
106+
}
107+
108+
// serializedValue - serialize as native field in v3.2+, as extension in earlier versions
109+
if (SerializedValue is not null)
110+
{
111+
if (version >= OpenApiSpecVersion.OpenApi3_2)
112+
{
113+
writer.WriteProperty(OpenApiConstants.SerializedValue, SerializedValue);
114+
}
115+
else
116+
{
117+
writer.WriteProperty(OpenApiConstants.ExtensionFieldNamePrefix + "oai-" + OpenApiConstants.SerializedValue, SerializedValue);
118+
}
119+
}
120+
87121
// extensions
88122
writer.WriteExtensions(Extensions, version);
89123

src/Microsoft.OpenApi/Models/OpenApiOAuthFlows.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ public class OpenApiOAuthFlows : IOpenApiSerializable, IOpenApiExtensible
3131
/// </summary>
3232
public OpenApiOAuthFlow? AuthorizationCode { get; set; }
3333

34+
/// <summary>
35+
/// Configuration for the OAuth Device Authorization flow.
36+
/// </summary>
37+
public OpenApiOAuthFlow? DeviceAuthorization { get; set; }
38+
3439
/// <summary>
3540
/// Specification Extensions.
3641
/// </summary>
@@ -51,6 +56,7 @@ public OpenApiOAuthFlows(OpenApiOAuthFlows oAuthFlows)
5156
Password = oAuthFlows?.Password != null ? new(oAuthFlows.Password) : null;
5257
ClientCredentials = oAuthFlows?.ClientCredentials != null ? new(oAuthFlows.ClientCredentials) : null;
5358
AuthorizationCode = oAuthFlows?.AuthorizationCode != null ? new(oAuthFlows.AuthorizationCode) : null;
59+
DeviceAuthorization = oAuthFlows?.DeviceAuthorization != null ? new(oAuthFlows.DeviceAuthorization) : null;
5460
Extensions = oAuthFlows?.Extensions != null ? new Dictionary<string, IOpenApiExtension>(oAuthFlows.Extensions) : null;
5561
}
5662

@@ -106,6 +112,22 @@ private void SerializeInternal(IOpenApiWriter writer, OpenApiSpecVersion version
106112
AuthorizationCode,
107113
callback);
108114

115+
// deviceAuthorization - only for v3.2+, otherwise as extension
116+
if (version >= OpenApiSpecVersion.OpenApi3_2)
117+
{
118+
writer.WriteOptionalObject(
119+
OpenApiConstants.DeviceAuthorization,
120+
DeviceAuthorization,
121+
callback);
122+
}
123+
else if (DeviceAuthorization is not null)
124+
{
125+
writer.WriteOptionalObject(
126+
OpenApiConstants.ExtensionFieldNamePrefix + "oai-" + OpenApiConstants.DeviceAuthorization,
127+
DeviceAuthorization,
128+
callback);
129+
}
130+
109131
// extensions
110132
writer.WriteExtensions(Extensions, version);
111133

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ public string? Summary
5555
/// <inheritdoc/>
5656
public JsonNode? Value { get => Target?.Value; }
5757

58+
/// <inheritdoc/>
59+
public JsonNode? DataValue { get => Target?.DataValue; }
60+
61+
/// <inheritdoc/>
62+
public string? SerializedValue { get => Target?.SerializedValue; }
63+
5864
/// <inheritdoc/>
5965
public override IOpenApiExample CopyReferenceAsTargetElementWithOverrides(IOpenApiExample source)
6066
{

src/Microsoft.OpenApi/Reader/V3/OpenApiExampleDeserializer.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ internal static partial class OpenApiV3Deserializer
3434
private static readonly PatternFieldMap<OpenApiExample> _examplePatternFields =
3535
new()
3636
{
37+
{s => s.Equals("x-oai-dataValue", StringComparison.OrdinalIgnoreCase), (o, p, n, _) => o.DataValue = n.CreateAny()},
38+
{s => s.Equals("x-oai-serializedValue", StringComparison.OrdinalIgnoreCase), (o, p, n, _) => o.SerializedValue = n.GetScalarValue()},
3739
{s => s.StartsWith(OpenApiConstants.ExtensionFieldNamePrefix, StringComparison.OrdinalIgnoreCase), (o, p, n, _) => o.AddExtension(p, LoadExtension(p,n))}
3840
};
3941

src/Microsoft.OpenApi/Reader/V3/OpenApiOAuthFlowsDeserializer.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ internal static partial class OpenApiV3Deserializer
2323
private static readonly PatternFieldMap<OpenApiOAuthFlows> _oAuthFlowsPatternFields =
2424
new()
2525
{
26+
{s => s.Equals("x-oai-deviceAuthorization", StringComparison.OrdinalIgnoreCase), (o, p, n, t) => o.DeviceAuthorization = LoadOAuthFlow(n, t)},
2627
{s => s.StartsWith(OpenApiConstants.ExtensionFieldNamePrefix, StringComparison.OrdinalIgnoreCase), (o, p, n, _) => o.AddExtension(p, LoadExtension(p,n))}
2728
};
2829

src/Microsoft.OpenApi/Reader/V31/OpenApiExampleDeserializer.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ internal static partial class OpenApiV31Deserializer
4040
private static readonly PatternFieldMap<OpenApiExample> _examplePatternFields =
4141
new()
4242
{
43+
{s => s.Equals("x-oai-dataValue", StringComparison.OrdinalIgnoreCase), (o, p, n, _) => o.DataValue = n.CreateAny()},
44+
{s => s.Equals("x-oai-serializedValue", StringComparison.OrdinalIgnoreCase), (o, p, n, _) => o.SerializedValue = n.GetScalarValue()},
4345
{s => s.StartsWith(OpenApiConstants.ExtensionFieldNamePrefix, StringComparison.OrdinalIgnoreCase), (o, p, n, _) => o.AddExtension(p, LoadExtension(p,n))}
4446
};
4547

src/Microsoft.OpenApi/Reader/V31/OpenApiOAuthFlowsDeserializer.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ internal static partial class OpenApiV31Deserializer
2020
private static readonly PatternFieldMap<OpenApiOAuthFlows> _oAuthFlowsPatternFields =
2121
new()
2222
{
23+
{s => s.Equals("x-oai-deviceAuthorization", StringComparison.OrdinalIgnoreCase), (o, p, n, t) => o.DeviceAuthorization = LoadOAuthFlow(n, t)},
2324
{s => s.StartsWith(OpenApiConstants.ExtensionFieldNamePrefix, StringComparison.OrdinalIgnoreCase), (o, p, n, _) => o.AddExtension(p, LoadExtension(p,n))}
2425
};
2526

src/Microsoft.OpenApi/Reader/V32/OpenApiExampleDeserializer.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,18 @@ internal static partial class OpenApiV32Deserializer
3434
o.ExternalValue = n.GetScalarValue();
3535
}
3636
},
37+
{
38+
"dataValue", (o, n, _) =>
39+
{
40+
o.DataValue = n.CreateAny();
41+
}
42+
},
43+
{
44+
"serializedValue", (o, n, _) =>
45+
{
46+
o.SerializedValue = n.GetScalarValue();
47+
}
48+
},
3749

3850
};
3951

0 commit comments

Comments
 (0)