Skip to content

Commit 1b65d40

Browse files
authored
Merge pull request #2091 from microsoft/fix/headers-reference
fix/headers reference
2 parents 16ba957 + 0cb4ccb commit 1b65d40

35 files changed

+321
-246
lines changed

src/Microsoft.OpenApi.Hidi/StatsVisitor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public override void Visit(OpenApiSchema schema)
2727

2828
public int HeaderCount { get; set; }
2929

30-
public override void Visit(IDictionary<string, OpenApiHeader> headers)
30+
public override void Visit(IDictionary<string, IOpenApiHeader> headers)
3131
{
3232
HeaderCount++;
3333
}

src/Microsoft.OpenApi.Workbench/StatsVisitor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public override void Visit(OpenApiSchema schema)
2727

2828
public int HeaderCount { get; set; }
2929

30-
public override void Visit(IDictionary<string, OpenApiHeader> headers)
30+
public override void Visit(IDictionary<string, IOpenApiHeader> headers)
3131
{
3232
HeaderCount++;
3333
}

src/Microsoft.OpenApi/Extensions/OpenApiReferencableExtensions.cs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,24 @@ private static IOpenApiReferenceable ResolveReferenceOnResponseElement(
9292
string mapKey,
9393
JsonPointer pointer)
9494
{
95-
switch (propertyName)
95+
if (!string.IsNullOrEmpty(mapKey))
9696
{
97-
case OpenApiConstants.Headers when mapKey != null:
98-
return responseElement.Headers[mapKey];
99-
case OpenApiConstants.Links when mapKey != null:
100-
return responseElement.Links[mapKey];
101-
default:
102-
throw new OpenApiException(string.Format(SRResource.InvalidReferenceId, pointer));
97+
if (OpenApiConstants.Headers.Equals(propertyName, StringComparison.Ordinal) &&
98+
responseElement?.Headers != null &&
99+
responseElement.Headers.TryGetValue(mapKey, out var headerElement) &&
100+
headerElement is IOpenApiReferenceable referenceable)
101+
{
102+
return referenceable;
103+
}
104+
if (OpenApiConstants.Links.Equals(propertyName, StringComparison.Ordinal) &&
105+
responseElement?.Links != null &&
106+
responseElement.Links.TryGetValue(mapKey, out var linkElement) &&
107+
linkElement is IOpenApiReferenceable referenceable2)
108+
{
109+
return referenceable2;
110+
}
103111
}
112+
throw new OpenApiException(string.Format(SRResource.InvalidReferenceId, pointer));
104113
}
105114
}
106115
}

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

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,10 @@
33
namespace Microsoft.OpenApi.Models.Interfaces;
44

55
/// <summary>
6-
/// Describes an element that has a summary and description.
6+
/// Describes an element that has a description.
77
/// </summary>
88
public interface IOpenApiDescribedElement : IOpenApiElement
99
{
10-
/// <summary>
11-
/// Short description for the example.
12-
/// </summary>
13-
public string Summary { get; set; }
14-
1510
/// <summary>
1611
/// Long description for the example.
1712
/// CommonMark syntax MAY be used for rich text representation.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace Microsoft.OpenApi.Models.Interfaces;
77
/// Defines the base properties for the example object.
88
/// This interface is provided for type assertions but should not be implemented by package consumers beyond automatic mocking.
99
/// </summary>
10-
public interface IOpenApiExample : IOpenApiDescribedElement, IOpenApiSerializable, IOpenApiReadOnlyExtensible
10+
public interface IOpenApiExample : IOpenApiDescribedElement, IOpenApiSummarizedElement, IOpenApiSerializable, IOpenApiReadOnlyExtensible
1111
{
1212
/// <summary>
1313
/// Embedded literal example. The value field and externalValue field are mutually
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
2+
using System.Collections.Generic;
3+
using System.Text.Json.Nodes;
4+
using Microsoft.OpenApi.Interfaces;
5+
6+
namespace Microsoft.OpenApi.Models.Interfaces;
7+
8+
/// <summary>
9+
/// Defines the base properties for the headers object.
10+
/// This interface is provided for type assertions but should not be implemented by package consumers beyond automatic mocking.
11+
/// </summary>
12+
public interface IOpenApiHeader : IOpenApiDescribedElement, IOpenApiSerializable, IOpenApiReadOnlyExtensible
13+
{
14+
/// <summary>
15+
/// Determines whether this header is mandatory.
16+
/// </summary>
17+
public bool Required { get; }
18+
19+
/// <summary>
20+
/// Specifies that a header is deprecated and SHOULD be transitioned out of usage.
21+
/// </summary>
22+
public bool Deprecated { get; }
23+
24+
/// <summary>
25+
/// Sets the ability to pass empty-valued headers.
26+
/// </summary>
27+
public bool AllowEmptyValue { get; }
28+
29+
/// <summary>
30+
/// Describes how the header value will be serialized depending on the type of the header value.
31+
/// </summary>
32+
public ParameterStyle? Style { get; }
33+
34+
/// <summary>
35+
/// When this is true, header values of type array or object generate separate parameters
36+
/// for each value of the array or key-value pair of the map.
37+
/// </summary>
38+
public bool Explode { get; }
39+
40+
/// <summary>
41+
/// Determines whether the header value SHOULD allow reserved characters, as defined by RFC3986.
42+
/// </summary>
43+
public bool AllowReserved { get; }
44+
45+
/// <summary>
46+
/// The schema defining the type used for the request body.
47+
/// </summary>
48+
public OpenApiSchema Schema { get; }
49+
50+
/// <summary>
51+
/// Example of the media type.
52+
/// </summary>
53+
public JsonNode Example { get; }
54+
55+
/// <summary>
56+
/// Examples of the media type.
57+
/// </summary>
58+
public IDictionary<string, IOpenApiExample> Examples { get; }
59+
60+
/// <summary>
61+
/// A map containing the representations for the header.
62+
/// </summary>
63+
public IDictionary<string, OpenApiMediaType> Content { get; }
64+
65+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using Microsoft.OpenApi.Interfaces;
2+
3+
namespace Microsoft.OpenApi.Models.Interfaces;
4+
/// <summary>
5+
/// Describes an element that has a summary.
6+
/// </summary>
7+
public interface IOpenApiSummarizedElement : IOpenApiElement
8+
{
9+
/// <summary>
10+
/// Short description for the example.
11+
/// </summary>
12+
public string Summary { get; set; }
13+
}

src/Microsoft.OpenApi/Models/OpenApiComponents.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ public class OpenApiComponents : IOpenApiSerializable, IOpenApiExtensible
4545
new Dictionary<string, OpenApiRequestBody>();
4646

4747
/// <summary>
48-
/// An object to hold reusable <see cref="OpenApiHeader"/> Objects.
48+
/// An object to hold reusable <see cref="IOpenApiHeader"/> Objects.
4949
/// </summary>
50-
public IDictionary<string, OpenApiHeader>? Headers { get; set; } = new Dictionary<string, OpenApiHeader>();
50+
public IDictionary<string, IOpenApiHeader>? Headers { get; set; } = new Dictionary<string, IOpenApiHeader>();
5151

5252
/// <summary>
5353
/// An object to hold reusable <see cref="OpenApiSecurityScheme"/> Objects.
@@ -90,7 +90,7 @@ public OpenApiComponents(OpenApiComponents? components)
9090
Parameters = components?.Parameters != null ? new Dictionary<string, OpenApiParameter>(components.Parameters) : null;
9191
Examples = components?.Examples != null ? new Dictionary<string, IOpenApiExample>(components.Examples) : null;
9292
RequestBodies = components?.RequestBodies != null ? new Dictionary<string, OpenApiRequestBody>(components.RequestBodies) : null;
93-
Headers = components?.Headers != null ? new Dictionary<string, OpenApiHeader>(components.Headers) : null;
93+
Headers = components?.Headers != null ? new Dictionary<string, IOpenApiHeader>(components.Headers) : null;
9494
SecuritySchemes = components?.SecuritySchemes != null ? new Dictionary<string, OpenApiSecurityScheme>(components.SecuritySchemes) : null;
9595
Links = components?.Links != null ? new Dictionary<string, OpenApiLink>(components.Links) : null;
9696
Callbacks = components?.Callbacks != null ? new Dictionary<string, IOpenApiCallback>(components.Callbacks) : null;

src/Microsoft.OpenApi/Models/OpenApiDocument.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -620,7 +620,7 @@ public bool AddComponent<T>(string id, T componentToRegister)
620620
Components.Examples.Add(id, openApiExample);
621621
break;
622622
case OpenApiHeader openApiHeader:
623-
Components.Headers ??= new Dictionary<string, OpenApiHeader>();
623+
Components.Headers ??= new Dictionary<string, IOpenApiHeader>();
624624
Components.Headers.Add(id, openApiHeader);
625625
break;
626626
case OpenApiSecurityScheme openApiSecurityScheme:

src/Microsoft.OpenApi/Models/OpenApiEncoding.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Collections.Generic;
66
using Microsoft.OpenApi.Extensions;
77
using Microsoft.OpenApi.Interfaces;
8+
using Microsoft.OpenApi.Models.Interfaces;
89
using Microsoft.OpenApi.Writers;
910

1011
namespace Microsoft.OpenApi.Models
@@ -24,7 +25,7 @@ public class OpenApiEncoding : IOpenApiSerializable, IOpenApiExtensible
2425
/// <summary>
2526
/// A map allowing additional information to be provided as headers.
2627
/// </summary>
27-
public IDictionary<string, OpenApiHeader> Headers { get; set; } = new Dictionary<string, OpenApiHeader>();
28+
public IDictionary<string, IOpenApiHeader> Headers { get; set; } = new Dictionary<string, IOpenApiHeader>();
2829

2930
/// <summary>
3031
/// Describes how a specific property value will be serialized depending on its type.
@@ -64,7 +65,7 @@ public OpenApiEncoding() { }
6465
public OpenApiEncoding(OpenApiEncoding encoding)
6566
{
6667
ContentType = encoding?.ContentType ?? ContentType;
67-
Headers = encoding?.Headers != null ? new Dictionary<string, OpenApiHeader>(encoding.Headers) : null;
68+
Headers = encoding?.Headers != null ? new Dictionary<string, IOpenApiHeader>(encoding.Headers) : null;
6869
Style = encoding?.Style ?? Style;
6970
Explode = encoding?.Explode ?? Explode;
7071
AllowReserved = encoding?.AllowReserved ?? AllowReserved;

0 commit comments

Comments
 (0)