Skip to content

Commit 3e5cd0f

Browse files
committed
Fetch the $ref summary and descriptions and populate them in the deserialized OpenApiReference equivalent
1 parent e9e5af8 commit 3e5cd0f

13 files changed

+144
-23
lines changed

src/Microsoft.OpenApi.Readers/Interface/IOpenApiVersionService.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@ internal interface IOpenApiVersionService
1919
/// </summary>
2020
/// <param name="reference">The reference string.</param>
2121
/// <param name="type">The type of the reference.</param>
22+
/// <param name="summary">The summary of the reference.</param>
23+
/// <param name="description">A reference description</param>
2224
/// <returns>The <see cref="OpenApiReference"/> object or null.</returns>
23-
OpenApiReference ConvertToOpenApiReference(string reference, ReferenceType? type);
25+
OpenApiReference ConvertToOpenApiReference(string reference, ReferenceType? type, string summary = null, string description = null);
2426

2527
/// <summary>
2628
/// Loads an OpenAPI Element from a document fragment
@@ -36,5 +38,13 @@ internal interface IOpenApiVersionService
3638
/// <param name="rootNode">RootNode containing the information to be converted into an OpenAPI Document</param>
3739
/// <returns>Instance of OpenApiDocument populated with data from rootNode</returns>
3840
OpenApiDocument LoadDocument(RootNode rootNode);
41+
42+
/// <summary>
43+
/// Gets the description and summary scalar values in a reference object for V3.1 support
44+
/// </summary>
45+
/// <param name="mapNode">A YamlMappingNode.</param>
46+
/// <param name="scalarValue">The scalar value we're parsing.</param>
47+
/// <returns>The resulting node value.</returns>
48+
string GetReferenceScalarValues(MapNode mapNode, string scalarValue);
3949
}
4050
}

src/Microsoft.OpenApi.Readers/ParseNodes/MapNode.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,13 +181,13 @@ public override string GetRaw()
181181
return x.Serialize(_node);
182182
}
183183

184-
public T GetReferencedObject<T>(ReferenceType referenceType, string referenceId)
184+
public T GetReferencedObject<T>(ReferenceType referenceType, string referenceId, string summary = null, string description = null)
185185
where T : IOpenApiReferenceable, new()
186186
{
187187
return new T()
188188
{
189189
UnresolvedReference = true,
190-
Reference = Context.VersionService.ConvertToOpenApiReference(referenceId, referenceType)
190+
Reference = Context.VersionService.ConvertToOpenApiReference(referenceId, referenceType, summary, description)
191191
};
192192
}
193193

src/Microsoft.OpenApi.Readers/V2/OpenApiV2VersionService.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ private static ReferenceType GetReferenceTypeV2FromName(string referenceType)
134134
/// <summary>
135135
/// Parse the string to a <see cref="OpenApiReference"/> object.
136136
/// </summary>
137-
public OpenApiReference ConvertToOpenApiReference(string reference, ReferenceType? type)
137+
public OpenApiReference ConvertToOpenApiReference(string reference, ReferenceType? type, string summary = null, string description = null)
138138
{
139139
if (!string.IsNullOrWhiteSpace(reference))
140140
{
@@ -221,5 +221,11 @@ public T LoadElement<T>(ParseNode node) where T : IOpenApiElement
221221
{
222222
return (T)_loaders[typeof(T)](node);
223223
}
224+
225+
/// <inheritdoc />
226+
public string GetReferenceScalarValues(MapNode mapNode, string scalarValue)
227+
{
228+
throw new NotImplementedException();
229+
}
224230
}
225231
}

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

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

4+
using System.Linq;
45
using Microsoft.OpenApi.Extensions;
56
using Microsoft.OpenApi.Models;
67
using Microsoft.OpenApi.Readers.ParseNodes;
@@ -51,11 +52,19 @@ internal static partial class OpenApiV3Deserializer
5152
public static OpenApiExample LoadExample(ParseNode node)
5253
{
5354
var mapNode = node.CheckMapNode("example");
55+
string description = null;
56+
string summary = null;
5457

5558
var pointer = mapNode.GetReferencePointer();
5659
if (pointer != null)
5760
{
58-
return mapNode.GetReferencedObject<OpenApiExample>(ReferenceType.Example, pointer);
61+
if (mapNode.Count() > 1)
62+
{
63+
description = node.Context.VersionService.GetReferenceScalarValues(mapNode, OpenApiConstants.Description);
64+
summary = node.Context.VersionService.GetReferenceScalarValues(mapNode, OpenApiConstants.Summary);
65+
}
66+
67+
return mapNode.GetReferencedObject<OpenApiExample>(ReferenceType.Example, pointer, summary, description);
5968
}
6069

6170
var example = new OpenApiExample();

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

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

4+
using System.Linq;
45
using Microsoft.OpenApi.Extensions;
56
using Microsoft.OpenApi.Models;
67
using Microsoft.OpenApi.Readers.ParseNodes;
@@ -85,11 +86,18 @@ internal static partial class OpenApiV3Deserializer
8586
public static OpenApiHeader LoadHeader(ParseNode node)
8687
{
8788
var mapNode = node.CheckMapNode("header");
89+
string description = null;
90+
string summary = null;
8891

8992
var pointer = mapNode.GetReferencePointer();
9093
if (pointer != null)
9194
{
92-
return mapNode.GetReferencedObject<OpenApiHeader>(ReferenceType.Header, pointer);
95+
if (mapNode.Count() > 1)
96+
{
97+
description = node.Context.VersionService.GetReferenceScalarValues(mapNode, OpenApiConstants.Description);
98+
summary = node.Context.VersionService.GetReferenceScalarValues(mapNode, OpenApiConstants.Summary);
99+
}
100+
return mapNode.GetReferencedObject<OpenApiHeader>(ReferenceType.Header, pointer, summary, description);
93101
}
94102

95103
var header = new OpenApiHeader();

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

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

4+
using System.Linq;
45
using Microsoft.OpenApi.Extensions;
56
using Microsoft.OpenApi.Models;
67
using Microsoft.OpenApi.Readers.ParseNodes;
@@ -57,11 +58,18 @@ public static OpenApiLink LoadLink(ParseNode node)
5758
{
5859
var mapNode = node.CheckMapNode("link");
5960
var link = new OpenApiLink();
61+
string description = null;
62+
string summary = null;
6063

6164
var pointer = mapNode.GetReferencePointer();
6265
if (pointer != null)
6366
{
64-
return mapNode.GetReferencedObject<OpenApiLink>(ReferenceType.Link, pointer);
67+
if (mapNode.Count() > 1)
68+
{
69+
description = node.Context.VersionService.GetReferenceScalarValues(mapNode, OpenApiConstants.Description);
70+
summary = node.Context.VersionService.GetReferenceScalarValues(mapNode, OpenApiConstants.Summary);
71+
}
72+
return mapNode.GetReferencedObject<OpenApiLink>(ReferenceType.Link, pointer, summary, description);
6573
}
6674

6775
ParseMap(mapNode, link, _linkFixedFields, _linkPatternFields);

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,18 @@ internal static partial class OpenApiV3Deserializer
142142
public static OpenApiParameter LoadParameter(ParseNode node)
143143
{
144144
var mapNode = node.CheckMapNode("parameter");
145+
string description = null;
146+
string summary = null;
145147

146148
var pointer = mapNode.GetReferencePointer();
147149
if (pointer != null)
148150
{
149-
return mapNode.GetReferencedObject<OpenApiParameter>(ReferenceType.Parameter, pointer);
151+
if (mapNode.Count() > 1)
152+
{
153+
description = node.Context.VersionService.GetReferenceScalarValues(mapNode, OpenApiConstants.Description);
154+
summary = node.Context.VersionService.GetReferenceScalarValues(mapNode, OpenApiConstants.Summary);
155+
}
156+
return mapNode.GetReferencedObject<OpenApiParameter>(ReferenceType.Parameter, pointer, summary, description);
150157
}
151158

152159
var parameter = new OpenApiParameter();

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

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

4+
using System.Linq;
45
using Microsoft.OpenApi.Extensions;
56
using Microsoft.OpenApi.Models;
67
using Microsoft.OpenApi.Readers.ParseNodes;
@@ -55,15 +56,22 @@ internal static partial class OpenApiV3Deserializer
5556
public static OpenApiPathItem LoadPathItem(ParseNode node)
5657
{
5758
var mapNode = node.CheckMapNode("PathItem");
59+
string description = null;
60+
string summary = null;
5861

5962
var pointer = mapNode.GetReferencePointer();
6063

6164
if (pointer != null)
6265
{
66+
if (mapNode.Count() > 1)
67+
{
68+
description = node.Context.VersionService.GetReferenceScalarValues(mapNode, OpenApiConstants.Description);
69+
summary = node.Context.VersionService.GetReferenceScalarValues(mapNode, OpenApiConstants.Summary);
70+
}
6371
return new OpenApiPathItem()
6472
{
6573
UnresolvedReference = true,
66-
Reference = node.Context.VersionService.ConvertToOpenApiReference(pointer, ReferenceType.PathItem)
74+
Reference = node.Context.VersionService.ConvertToOpenApiReference(pointer, ReferenceType.PathItem, summary, description)
6775
};
6876
}
6977

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

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

4+
using System.Linq;
45
using Microsoft.OpenApi.Extensions;
56
using Microsoft.OpenApi.Models;
67
using Microsoft.OpenApi.Readers.ParseNodes;
@@ -45,11 +46,18 @@ internal static partial class OpenApiV3Deserializer
4546
public static OpenApiRequestBody LoadRequestBody(ParseNode node)
4647
{
4748
var mapNode = node.CheckMapNode("requestBody");
49+
string description = null;
50+
string summary = null;
4851

4952
var pointer = mapNode.GetReferencePointer();
5053
if (pointer != null)
5154
{
52-
return mapNode.GetReferencedObject<OpenApiRequestBody>(ReferenceType.RequestBody, pointer);
55+
if (mapNode.Count() > 1)
56+
{
57+
description = node.Context.VersionService.GetReferenceScalarValues(mapNode, OpenApiConstants.Description);
58+
summary = node.Context.VersionService.GetReferenceScalarValues(mapNode, OpenApiConstants.Summary);
59+
}
60+
return mapNode.GetReferencedObject<OpenApiRequestBody>(ReferenceType.RequestBody, pointer, summary, description);
5361
}
5462

5563
var requestBody = new OpenApiRequestBody();

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the MIT license.
33

44
using System.Collections.Generic;
5+
using System.Linq;
56
using Microsoft.OpenApi.Extensions;
67
using Microsoft.OpenApi.Models;
78
using Microsoft.OpenApi.Readers.ParseNodes;
@@ -51,11 +52,18 @@ internal static partial class OpenApiV3Deserializer
5152
public static OpenApiResponse LoadResponse(ParseNode node)
5253
{
5354
var mapNode = node.CheckMapNode("response");
55+
string description = null;
56+
string summary = null;
5457

5558
var pointer = mapNode.GetReferencePointer();
5659
if (pointer != null)
5760
{
58-
return mapNode.GetReferencedObject<OpenApiResponse>(ReferenceType.Response, pointer);
61+
if (mapNode.Count() > 1)
62+
{
63+
description = node.Context.VersionService.GetReferenceScalarValues(mapNode, OpenApiConstants.Description);
64+
summary = node.Context.VersionService.GetReferenceScalarValues(mapNode, OpenApiConstants.Summary);
65+
}
66+
return mapNode.GetReferencedObject<OpenApiResponse>(ReferenceType.Response, pointer, summary, description);
5967
}
6068

6169
var response = new OpenApiResponse();

0 commit comments

Comments
 (0)