Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
11 changes: 11 additions & 0 deletions src/Microsoft.OpenApi/Models/OpenApiEncoding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Collections.Generic;
using System.Linq;

namespace Microsoft.OpenApi
{
Expand All @@ -27,6 +28,12 @@ public class OpenApiEncoding : IOpenApiSerializable, IOpenApiExtensible
/// </summary>
public IDictionary<string, IOpenApiHeader>? Headers { get; set; }

/// <summary>
/// A map of property names to their encoding information.
/// The key is the property name and the value is the encoding object.
/// </summary>
public IDictionary<string, OpenApiEncoding>? Encoding { get; set; }

/// <summary>
/// Describes how a specific property value will be serialized depending on its type.
/// </summary>
Expand Down Expand Up @@ -70,6 +77,7 @@ public OpenApiEncoding(OpenApiEncoding encoding)
{
ContentType = encoding?.ContentType ?? ContentType;
Headers = encoding?.Headers != null ? new Dictionary<string, IOpenApiHeader>(encoding.Headers) : null;
Encoding = encoding?.Encoding != null ? new Dictionary<string, OpenApiEncoding>(encoding.Encoding.ToDictionary(kvp => kvp.Key, kvp => new OpenApiEncoding(kvp.Value))) : null;
Style = encoding?.Style ?? Style;
Explode = encoding?._explode;
AllowReserved = encoding?.AllowReserved ?? AllowReserved;
Expand Down Expand Up @@ -119,6 +127,9 @@ private void SerializeInternal(IOpenApiWriter writer, OpenApiSpecVersion version
// headers
writer.WriteOptionalMap(OpenApiConstants.Headers, Headers, callback);

// encoding
writer.WriteOptionalMap(OpenApiConstants.Encoding, Encoding, callback);

// style
writer.WriteProperty(OpenApiConstants.Style, Style?.GetDisplayName());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ internal static partial class OpenApiV32Deserializer
o.Headers = n.CreateMap(LoadHeader, t);
}
},
{
"encoding", (o, n, t) =>
{
o.Encoding = n.CreateMap(LoadEncoding, t);
}
},
{
"style", (o, n, _) =>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.IO;
using System.Threading.Tasks;
using Microsoft.OpenApi.Reader;
using System.Collections.Generic;
using Xunit;

namespace Microsoft.OpenApi.Readers.Tests.V32Tests
Expand All @@ -29,5 +30,26 @@ public async Task ParseEncodingWithAllowReservedShouldSucceed()
AllowReserved = true
}, encoding);
}

[Fact]
public async Task ParseEncodingWithNestedEncodingShouldSucceed()
{
// Act
var encoding = await OpenApiModelFactory.LoadAsync<OpenApiEncoding>(Path.Combine(SampleFolderPath, "encodingWithNestedEncoding.yaml"), OpenApiSpecVersion.OpenApi3_2, new(), SettingsFixture.ReaderSettings);

// Assert
Assert.NotNull(encoding);
Assert.Equal("application/json", encoding.ContentType);
Assert.NotNull(encoding.Headers);
Assert.Single(encoding.Headers);
Assert.NotNull(encoding.Encoding);
Assert.Equal(2, encoding.Encoding.Count);
Assert.True(encoding.Encoding.ContainsKey("nestedField"));
Assert.Equal("application/xml", encoding.Encoding["nestedField"].ContentType);
Assert.Equal(ParameterStyle.Form, encoding.Encoding["nestedField"].Style);
Assert.True(encoding.Encoding["nestedField"].Explode);
Assert.True(encoding.Encoding.ContainsKey("anotherField"));
Assert.Equal("text/plain", encoding.Encoding["anotherField"].ContentType);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.2.0.md#encodingObject
contentType: application/json
headers:
X-Rate-Limit-Limit:
description: The number of allowed requests in the current period
schema:
type: integer
encoding:
nestedField:
contentType: application/xml
style: form
explode: true
anotherField:
contentType: text/plain
92 changes: 92 additions & 0 deletions test/Microsoft.OpenApi.Tests/Models/OpenApiEncodingTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

using System.Collections.Generic;
using System.Threading.Tasks;
using Xunit;

Expand Down Expand Up @@ -126,5 +127,96 @@ public async Task WhenExplodeIsSetOutputShouldHaveExplode(bool? expectedExplode,
expected = expected.MakeLineBreaksEnvironmentNeutral();
Assert.Equal(actual, expected);
}

[Fact]
public async Task SerializeEncodingWithNestedEncodingAsV32JsonWorks()
{
// Arrange
var encoding = new OpenApiEncoding
{
ContentType = "application/json",
Encoding = new Dictionary<string, OpenApiEncoding>
{
["nestedField"] = new OpenApiEncoding
{
ContentType = "application/xml",
Style = ParameterStyle.Form,
Explode = true
},
["anotherField"] = new OpenApiEncoding
{
ContentType = "text/plain"
}
}
};

var expected =
"""
{
"contentType": "application/json",
"encoding": {
"nestedField": {
"contentType": "application/xml",
"style": "form",
"explode": true
},
"anotherField": {
"contentType": "text/plain"
}
}
}
""";

// Act
var actual = await encoding.SerializeAsJsonAsync(OpenApiSpecVersion.OpenApi3_2);

// Assert
actual = actual.MakeLineBreaksEnvironmentNeutral();
expected = expected.MakeLineBreaksEnvironmentNeutral();
Assert.Equal(expected, actual);
}

[Fact]
public async Task SerializeEncodingWithNestedEncodingAsV32YamlWorks()
{
// Arrange
var encoding = new OpenApiEncoding
{
ContentType = "application/json",
Encoding = new Dictionary<string, OpenApiEncoding>
{
["nestedField"] = new OpenApiEncoding
{
ContentType = "application/xml",
Style = ParameterStyle.Form,
Explode = true
},
["anotherField"] = new OpenApiEncoding
{
ContentType = "text/plain"
}
}
};

var expected =
"""
contentType: application/json
encoding:
nestedField:
contentType: application/xml
style: form
explode: true
anotherField:
contentType: text/plain
""";

// Act
var actual = await encoding.SerializeAsYamlAsync(OpenApiSpecVersion.OpenApi3_2);

// Assert
actual = actual.MakeLineBreaksEnvironmentNeutral();
expected = expected.MakeLineBreaksEnvironmentNeutral();
Assert.Equal(expected, actual);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,7 @@ namespace Microsoft.OpenApi
public OpenApiEncoding(Microsoft.OpenApi.OpenApiEncoding encoding) { }
public bool? AllowReserved { get; set; }
public string? ContentType { get; set; }
public System.Collections.Generic.IDictionary<string, Microsoft.OpenApi.OpenApiEncoding>? Encoding { get; set; }
public bool? Explode { get; set; }
public System.Collections.Generic.IDictionary<string, Microsoft.OpenApi.IOpenApiExtension>? Extensions { get; set; }
public System.Collections.Generic.IDictionary<string, Microsoft.OpenApi.IOpenApiHeader>? Headers { get; set; }
Expand Down