Skip to content

Commit 7a77498

Browse files
authored
Merge pull request #366 from VitaliyKurokhtin/vvk/component-headers
Supporting all header fields inside components/headers
2 parents 7921f28 + 1d2eee6 commit 7a77498

File tree

4 files changed

+134
-1
lines changed

4 files changed

+134
-1
lines changed

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

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ internal static partial class OpenApiV3Deserializer
3333
o.Deprecated = bool.Parse(n.GetScalarValue());
3434
}
3535
},
36+
{
37+
"allowEmptyValue", (o, n) =>
38+
{
39+
o.AllowEmptyValue = bool.Parse(n.GetScalarValue());
40+
}
41+
},
3642
{
3743
"allowReserved", (o, n) =>
3844
{
@@ -45,12 +51,30 @@ internal static partial class OpenApiV3Deserializer
4551
o.Style = n.GetScalarValue().GetEnumFromDisplayName<ParameterStyle>();
4652
}
4753
},
54+
{
55+
"explode", (o, n) =>
56+
{
57+
o.Explode = bool.Parse(n.GetScalarValue());
58+
}
59+
},
4860
{
4961
"schema", (o, n) =>
5062
{
5163
o.Schema = LoadSchema(n);
5264
}
53-
}
65+
},
66+
{
67+
"examples", (o, n) =>
68+
{
69+
o.Examples = n.CreateMap(LoadExample);
70+
}
71+
},
72+
{
73+
"example", (o, n) =>
74+
{
75+
o.Example = n.CreateAny();
76+
}
77+
},
5478
};
5579

5680
private static readonly PatternFieldMap<OpenApiHeader> _headerPatternFields = new PatternFieldMap<OpenApiHeader>

test/Microsoft.OpenApi.Readers.Tests/Microsoft.OpenApi.Readers.Tests.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,9 @@
118118
<EmbeddedResource Include="V3Tests\Samples\OpenApiDocument\minimalDocument.yaml">
119119
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
120120
</EmbeddedResource>
121+
<EmbeddedResource Include="V3Tests\Samples\OpenApiDocument\apiWithFullHeaderComponent.yaml">
122+
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
123+
</EmbeddedResource>
121124
<EmbeddedResource Include="V3Tests\Samples\OpenApiDocument\petStore.yaml">
122125
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
123126
</EmbeddedResource>

test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiDocumentTests.cs

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using System.Linq;
99
using System.Threading;
1010
using FluentAssertions;
11+
using Microsoft.OpenApi.Any;
1112
using Microsoft.OpenApi.Models;
1213
using Microsoft.OpenApi.Validations;
1314
using Microsoft.OpenApi.Validations.Rules;
@@ -1204,5 +1205,76 @@ public void GlobalSecurityRequirementShouldReferenceSecurityScheme()
12041205
Assert.Same(securityRequirement.Keys.First(), openApiDoc.Components.SecuritySchemes.First().Value);
12051206
}
12061207
}
1208+
1209+
[Fact]
1210+
public void HeaderParameterShouldAllowExample()
1211+
{
1212+
using (var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "apiWithFullHeaderComponent.yaml")))
1213+
{
1214+
var openApiDoc = new OpenApiStreamReader().Read(stream, out var diagnostic);
1215+
1216+
var exampleHeader = openApiDoc.Components?.Headers?["example-header"];
1217+
Assert.NotNull(exampleHeader);
1218+
exampleHeader.ShouldBeEquivalentTo(
1219+
new OpenApiHeader()
1220+
{
1221+
Description = "Test header with example",
1222+
Required = true,
1223+
Deprecated = true,
1224+
AllowEmptyValue = true,
1225+
AllowReserved = true,
1226+
Style = ParameterStyle.Simple,
1227+
Explode = true,
1228+
Example = new OpenApiString("99391c7e-ad88-49ec-a2ad-99ddcb1f7721"),
1229+
Schema = new OpenApiSchema()
1230+
{
1231+
Type = "string",
1232+
Format = "uuid"
1233+
},
1234+
Reference = new OpenApiReference()
1235+
{
1236+
Type = ReferenceType.Header,
1237+
Id = "example-header"
1238+
}
1239+
});
1240+
1241+
var examplesHeader = openApiDoc.Components?.Headers?["examples-header"];
1242+
Assert.NotNull(examplesHeader);
1243+
examplesHeader.ShouldBeEquivalentTo(
1244+
new OpenApiHeader()
1245+
{
1246+
Description = "Test header with example",
1247+
Required = true,
1248+
Deprecated = true,
1249+
AllowEmptyValue = true,
1250+
AllowReserved = true,
1251+
Style = ParameterStyle.Simple,
1252+
Explode = true,
1253+
Examples = new Dictionary<string, OpenApiExample>()
1254+
{
1255+
{ "uuid1", new OpenApiExample()
1256+
{
1257+
Value = new OpenApiString("99391c7e-ad88-49ec-a2ad-99ddcb1f7721")
1258+
}
1259+
},
1260+
{ "uuid2", new OpenApiExample()
1261+
{
1262+
Value = new OpenApiString("99391c7e-ad88-49ec-a2ad-99ddcb1f7721")
1263+
}
1264+
}
1265+
},
1266+
Schema = new OpenApiSchema()
1267+
{
1268+
Type = "string",
1269+
Format = "uuid"
1270+
},
1271+
Reference = new OpenApiReference()
1272+
{
1273+
Type = ReferenceType.Header,
1274+
Id = "examples-header"
1275+
}
1276+
});
1277+
}
1278+
}
12071279
}
12081280
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
openapi: '3.0.0'
2+
info:
3+
version: '1.0.0'
4+
title: Header components
5+
components:
6+
headers:
7+
example-header:
8+
description: Test header with example
9+
required: true
10+
deprecated: true
11+
allowEmptyValue: true
12+
allowReserved: true
13+
style: simple
14+
explode: true
15+
example: "99391c7e-ad88-49ec-a2ad-99ddcb1f7721"
16+
schema:
17+
type: string
18+
format: uuid
19+
examples-header:
20+
description: Test header with example
21+
required: true
22+
deprecated: true
23+
allowEmptyValue: true
24+
allowReserved: true
25+
style: simple
26+
explode: true
27+
examples:
28+
uuid1:
29+
value: "99391c7e-ad88-49ec-a2ad-99ddcb1f7721"
30+
uuid2:
31+
value: "99391c7e-ad88-49ec-a2ad-99ddcb1f7721"
32+
schema:
33+
type: string
34+
format: uuid

0 commit comments

Comments
 (0)