Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<Company>Microsoft</Company>
<Title>Microsoft.OpenApi.Readers</Title>
<PackageId>Microsoft.OpenApi.Readers</PackageId>
<Version>1.2.2</Version>
<Version>1.2.3</Version>
<Description>OpenAPI.NET Readers for JSON and YAML documents</Description>
<Copyright>© Microsoft Corporation. All rights reserved.</Copyright>
<PackageTags>OpenAPI .NET</PackageTags>
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.OpenApi.Readers/OpenApiTextReaderReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public OpenApiDocument Read(TextReader input, out OpenApiDiagnostic diagnostic)
catch (YamlException ex)
{
diagnostic = new OpenApiDiagnostic();
diagnostic.Errors.Add(new OpenApiError($"#char={ex.Start.Line}", ex.Message));
diagnostic.Errors.Add(new OpenApiError($"#line={ex.Start.Line}", ex.Message));
return new OpenApiDocument();
}

Expand Down
10 changes: 10 additions & 0 deletions src/Microsoft.OpenApi.Readers/Services/OpenApiReferenceResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public override void Visit(OpenApiComponents components)
ResolveMap(components.Examples);
ResolveMap(components.Schemas);
ResolveMap(components.SecuritySchemes);
ResolveMap(components.Headers);
}

public override void Visit(IDictionary<string, OpenApiCallback> callbacks)
Expand Down Expand Up @@ -99,6 +100,15 @@ public override void Visit(OpenApiResponses responses)
ResolveMap(responses);
}

/// <summary>
/// Resolve all references to headers
/// </summary>
/// <param name="headers"></param>
public override void Visit(IDictionary<string, OpenApiHeader> headers)
{
ResolveMap(headers);
}

/// <summary>
/// Resolve all references to SecuritySchemes
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Collections.Generic;
using System.Linq;
using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Extensions;
using Microsoft.OpenApi.Models;
using Microsoft.OpenApi.Readers.ParseNodes;
Expand Down Expand Up @@ -164,6 +165,7 @@ private static OpenApiRequestBody CreateFormBody(ParsingContext context, List<Op
{
var schema = v.Schema;
schema.Description = v.Description;
schema.Extensions = v.Extensions;
return schema;
}),
Required = new HashSet<string>(formParameters.Where(p => p.Required).Select(p => p.Name))
Expand Down Expand Up @@ -201,9 +203,11 @@ internal static OpenApiRequestBody CreateRequestBody(
v => new OpenApiMediaType
{
Schema = bodyParameter.Schema
})
}),
Extensions = bodyParameter.Extensions
};

requestBody.Extensions[OpenApiConstants.BodyName] = new OpenApiString(bodyParameter.Name);
return requestBody;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.OpenApi/Microsoft.OpenApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<Company>Microsoft</Company>
<Title>Microsoft.OpenApi</Title>
<PackageId>Microsoft.OpenApi</PackageId>
<Version>1.2.2</Version>
<Version>1.2.3</Version>
<Description>.NET models with JSON and YAML writers for OpenAPI specification</Description>
<Copyright>© Microsoft Corporation. All rights reserved.</Copyright>
<PackageTags>OpenAPI .NET</PackageTags>
Expand Down
5 changes: 5 additions & 0 deletions src/Microsoft.OpenApi/Models/OpenApiConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,11 @@ public static class OpenApiConstants
/// </summary>
public const string DefaultDescription = "Default Description";

/// <summary>
/// Field: BodyName extensions
/// </summary>
public const string BodyName = "x-bodyName";

/// <summary>
/// Field: version3_0_0
/// </summary>
Expand Down
19 changes: 17 additions & 2 deletions src/Microsoft.OpenApi/Models/OpenApiOperation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -228,29 +228,44 @@ public void SerializeAsV2(IOpenApiWriter writer)
{
foreach (var property in RequestBody.Content.First().Value.Schema.Properties)
{
var paramName = property.Key;
var paramSchema = property.Value;
if (paramSchema.Type == "string" && paramSchema.Format == "binary") {
paramSchema.Type = "file";
paramSchema.Format = null;
}
parameters.Add(
new OpenApiFormDataParameter
{
Description = property.Value.Description,
Name = property.Key,
Schema = property.Value,
Required = RequestBody.Content.First().Value.Schema.Required.Contains(property.Key)

});
}
}
else
{
var content = RequestBody.Content.Values.FirstOrDefault();

var bodyParameter = new OpenApiBodyParameter
{
Description = RequestBody.Description,
// V2 spec actually allows the body to have custom name.
// Our library does not support this at the moment.
// To allow round-tripping we use an extension to hold the name
Name = "body",
Schema = content?.Schema ?? new OpenApiSchema(),
Required = RequestBody.Required
Required = RequestBody.Required,
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.
};

if (bodyParameter.Extensions.ContainsKey(OpenApiConstants.BodyName))
{
bodyParameter.Name = (RequestBody.Extensions[OpenApiConstants.BodyName] as OpenApiString)?.Value ?? "body";
bodyParameter.Extensions.Remove(OpenApiConstants.BodyName);
}

parameters.Add(bodyParameter);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</EmbeddedResource>
<EmbeddedResource Include="V3Tests\Samples\OpenApiExample\explicitString.yaml" />
<EmbeddedResource Include="V3Tests\Samples\OpenApiResponse\responseWithHeaderReference.yaml" />
<EmbeddedResource Include="V3Tests\Samples\OpenApiInfo\basicInfo.yaml">
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</EmbeddedResource>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using FluentAssertions;
using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Extensions;
using Microsoft.OpenApi.Interfaces;
using Microsoft.OpenApi.Models;
using Microsoft.OpenApi.Readers.ParseNodes;
using Microsoft.OpenApi.Readers.V2;
Expand Down Expand Up @@ -180,6 +181,9 @@ public class OpenApiOperationTests
Type = "object"
}
}
},
Extensions = {
[OpenApiConstants.BodyName] = new OpenApiString("petObject")
}
},
Responses = new OpenApiResponses
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

using System.IO;
using System.Linq;
using FluentAssertions;
using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Models;
using Microsoft.OpenApi.Readers.ParseNodes;
using Microsoft.OpenApi.Readers.V3;
using Xunit;

namespace Microsoft.OpenApi.Readers.Tests.V3Tests
{
[Collection("DefaultSettings")]
public class OpenApiResponseTests
{
private const string SampleFolderPath = "V3Tests/Samples/OpenApiResponse/";

[Fact]
public void ResponseWithReferencedHeaderShouldReferenceComponent()
{
using (var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "responseWithHeaderReference.yaml")))
{
var openApiDoc = new OpenApiStreamReader().Read(stream, out var diagnostic);

var response = openApiDoc.Components.Responses["Test"];

Assert.Same(response.Headers.First().Value, openApiDoc.Components.Headers.First().Value);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
openapi: 3.0.0
info:
title: Example of response referencing a header
version: 1.0.0
components:
headers:
X-Test:
description: Test
schema:
type: string
responses:
Test:
description: Test Repsonse
headers:
X-Test:
$ref: '#/components/headers/X-Test'