Skip to content

Commit 553061a

Browse files
authored
Merge pull request #511 from microsoft/hotfix/1.2.3
Hotfix/1.2.3
2 parents be6a700 + 66e4d87 commit 553061a

11 files changed

+94
-6
lines changed

src/Microsoft.OpenApi.Readers/Microsoft.OpenApi.Readers.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<Company>Microsoft</Company>
1111
<Title>Microsoft.OpenApi.Readers</Title>
1212
<PackageId>Microsoft.OpenApi.Readers</PackageId>
13-
<Version>1.2.2</Version>
13+
<Version>1.2.3</Version>
1414
<Description>OpenAPI.NET Readers for JSON and YAML documents</Description>
1515
<Copyright>© Microsoft Corporation. All rights reserved.</Copyright>
1616
<PackageTags>OpenAPI .NET</PackageTags>

src/Microsoft.OpenApi.Readers/OpenApiTextReaderReader.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public OpenApiDocument Read(TextReader input, out OpenApiDiagnostic diagnostic)
4545
catch (YamlException ex)
4646
{
4747
diagnostic = new OpenApiDiagnostic();
48-
diagnostic.Errors.Add(new OpenApiError($"#char={ex.Start.Line}", ex.Message));
48+
diagnostic.Errors.Add(new OpenApiError($"#line={ex.Start.Line}", ex.Message));
4949
return new OpenApiDocument();
5050
}
5151

src/Microsoft.OpenApi.Readers/Services/OpenApiReferenceResolver.cs

+10
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public override void Visit(OpenApiComponents components)
5252
ResolveMap(components.Examples);
5353
ResolveMap(components.Schemas);
5454
ResolveMap(components.SecuritySchemes);
55+
ResolveMap(components.Headers);
5556
}
5657

5758
public override void Visit(IDictionary<string, OpenApiCallback> callbacks)
@@ -99,6 +100,15 @@ public override void Visit(OpenApiResponses responses)
99100
ResolveMap(responses);
100101
}
101102

103+
/// <summary>
104+
/// Resolve all references to headers
105+
/// </summary>
106+
/// <param name="headers"></param>
107+
public override void Visit(IDictionary<string, OpenApiHeader> headers)
108+
{
109+
ResolveMap(headers);
110+
}
111+
102112
/// <summary>
103113
/// Resolve all references to SecuritySchemes
104114
/// </summary>

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System.Collections.Generic;
55
using System.Linq;
6+
using Microsoft.OpenApi.Any;
67
using Microsoft.OpenApi.Extensions;
78
using Microsoft.OpenApi.Models;
89
using Microsoft.OpenApi.Readers.ParseNodes;
@@ -164,6 +165,7 @@ private static OpenApiRequestBody CreateFormBody(ParsingContext context, List<Op
164165
{
165166
var schema = v.Schema;
166167
schema.Description = v.Description;
168+
schema.Extensions = v.Extensions;
167169
return schema;
168170
}),
169171
Required = new HashSet<string>(formParameters.Where(p => p.Required).Select(p => p.Name))
@@ -201,9 +203,11 @@ internal static OpenApiRequestBody CreateRequestBody(
201203
v => new OpenApiMediaType
202204
{
203205
Schema = bodyParameter.Schema
204-
})
206+
}),
207+
Extensions = bodyParameter.Extensions
205208
};
206209

210+
requestBody.Extensions[OpenApiConstants.BodyName] = new OpenApiString(bodyParameter.Name);
207211
return requestBody;
208212
}
209213

src/Microsoft.OpenApi/Microsoft.OpenApi.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<Company>Microsoft</Company>
1111
<Title>Microsoft.OpenApi</Title>
1212
<PackageId>Microsoft.OpenApi</PackageId>
13-
<Version>1.2.2</Version>
13+
<Version>1.2.3</Version>
1414
<Description>.NET models with JSON and YAML writers for OpenAPI specification</Description>
1515
<Copyright>© Microsoft Corporation. All rights reserved.</Copyright>
1616
<PackageTags>OpenAPI .NET</PackageTags>

src/Microsoft.OpenApi/Models/OpenApiConstants.cs

+5
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,11 @@ public static class OpenApiConstants
560560
/// </summary>
561561
public const string DefaultDescription = "Default Description";
562562

563+
/// <summary>
564+
/// Field: BodyName extensions
565+
/// </summary>
566+
public const string BodyName = "x-bodyName";
567+
563568
/// <summary>
564569
/// Field: version3_0_0
565570
/// </summary>

src/Microsoft.OpenApi/Models/OpenApiOperation.cs

+17-2
Original file line numberDiff line numberDiff line change
@@ -228,29 +228,44 @@ public void SerializeAsV2(IOpenApiWriter writer)
228228
{
229229
foreach (var property in RequestBody.Content.First().Value.Schema.Properties)
230230
{
231+
var paramName = property.Key;
232+
var paramSchema = property.Value;
233+
if (paramSchema.Type == "string" && paramSchema.Format == "binary") {
234+
paramSchema.Type = "file";
235+
paramSchema.Format = null;
236+
}
231237
parameters.Add(
232238
new OpenApiFormDataParameter
233239
{
234240
Description = property.Value.Description,
235241
Name = property.Key,
236242
Schema = property.Value,
237243
Required = RequestBody.Content.First().Value.Schema.Required.Contains(property.Key)
244+
238245
});
239246
}
240247
}
241248
else
242249
{
243250
var content = RequestBody.Content.Values.FirstOrDefault();
251+
244252
var bodyParameter = new OpenApiBodyParameter
245253
{
246254
Description = RequestBody.Description,
247255
// V2 spec actually allows the body to have custom name.
248-
// Our library does not support this at the moment.
256+
// To allow round-tripping we use an extension to hold the name
249257
Name = "body",
250258
Schema = content?.Schema ?? new OpenApiSchema(),
251-
Required = RequestBody.Required
259+
Required = RequestBody.Required,
260+
Extensions = RequestBody.Extensions.ToDictionary(k => k.Key, v => v.Value) // Clone extensions so we can remove the x-bodyName extensions from the output V2 model.
252261
};
253262

263+
if (bodyParameter.Extensions.ContainsKey(OpenApiConstants.BodyName))
264+
{
265+
bodyParameter.Name = (RequestBody.Extensions[OpenApiConstants.BodyName] as OpenApiString)?.Value ?? "body";
266+
bodyParameter.Extensions.Remove(OpenApiConstants.BodyName);
267+
}
268+
254269
parameters.Add(bodyParameter);
255270
}
256271
}

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

+1
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@
150150
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
151151
</EmbeddedResource>
152152
<EmbeddedResource Include="V3Tests\Samples\OpenApiExample\explicitString.yaml" />
153+
<EmbeddedResource Include="V3Tests\Samples\OpenApiResponse\responseWithHeaderReference.yaml" />
153154
<EmbeddedResource Include="V3Tests\Samples\OpenApiInfo\basicInfo.yaml">
154155
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
155156
</EmbeddedResource>

test/Microsoft.OpenApi.Readers.Tests/V2Tests/OpenApiOperationTests.cs

+4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using FluentAssertions;
88
using Microsoft.OpenApi.Any;
99
using Microsoft.OpenApi.Extensions;
10+
using Microsoft.OpenApi.Interfaces;
1011
using Microsoft.OpenApi.Models;
1112
using Microsoft.OpenApi.Readers.ParseNodes;
1213
using Microsoft.OpenApi.Readers.V2;
@@ -180,6 +181,9 @@ public class OpenApiOperationTests
180181
Type = "object"
181182
}
182183
}
184+
},
185+
Extensions = {
186+
[OpenApiConstants.BodyName] = new OpenApiString("petObject")
183187
}
184188
},
185189
Responses = new OpenApiResponses
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license.
3+
4+
using System.IO;
5+
using System.Linq;
6+
using FluentAssertions;
7+
using Microsoft.OpenApi.Any;
8+
using Microsoft.OpenApi.Models;
9+
using Microsoft.OpenApi.Readers.ParseNodes;
10+
using Microsoft.OpenApi.Readers.V3;
11+
using Xunit;
12+
13+
namespace Microsoft.OpenApi.Readers.Tests.V3Tests
14+
{
15+
[Collection("DefaultSettings")]
16+
public class OpenApiResponseTests
17+
{
18+
private const string SampleFolderPath = "V3Tests/Samples/OpenApiResponse/";
19+
20+
[Fact]
21+
public void ResponseWithReferencedHeaderShouldReferenceComponent()
22+
{
23+
using (var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "responseWithHeaderReference.yaml")))
24+
{
25+
var openApiDoc = new OpenApiStreamReader().Read(stream, out var diagnostic);
26+
27+
var response = openApiDoc.Components.Responses["Test"];
28+
29+
Assert.Same(response.Headers.First().Value, openApiDoc.Components.Headers.First().Value);
30+
}
31+
}
32+
}
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
openapi: 3.0.0
2+
info:
3+
title: Example of response referencing a header
4+
version: 1.0.0
5+
components:
6+
headers:
7+
X-Test:
8+
description: Test
9+
schema:
10+
type: string
11+
responses:
12+
Test:
13+
description: Test Repsonse
14+
headers:
15+
X-Test:
16+
$ref: '#/components/headers/X-Test'

0 commit comments

Comments
 (0)