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
17 changes: 14 additions & 3 deletions src/Microsoft.OpenApi/Models/OpenApiEncoding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ namespace Microsoft.OpenApi
/// </summary>
public class OpenApiEncoding : IOpenApiSerializable, IOpenApiExtensible
{
/// <summary>
/// Explode backing variable
/// </summary>
private bool? _explode;
/// <summary>
/// The Content-Type for encoding a specific property.
/// The value can be a specific media type (e.g. application/json),
Expand All @@ -35,7 +39,11 @@ public class OpenApiEncoding : IOpenApiSerializable, IOpenApiExtensible
/// For all other styles, the default value is false.
/// This property SHALL be ignored if the request body media type is not application/x-www-form-urlencoded.
/// </summary>
public bool? Explode { get; set; }
public bool? Explode
{
get => _explode ?? Style == ParameterStyle.Form;
set => _explode = value;
}

/// <summary>
/// Determines whether the parameter value SHOULD allow reserved characters,
Expand Down Expand Up @@ -63,7 +71,7 @@ public OpenApiEncoding(OpenApiEncoding encoding)
ContentType = encoding?.ContentType ?? ContentType;
Headers = encoding?.Headers != null ? new Dictionary<string, IOpenApiHeader>(encoding.Headers) : null;
Style = encoding?.Style ?? Style;
Explode = encoding?.Explode ?? Explode;
Explode = encoding?._explode;
AllowReserved = encoding?.AllowReserved ?? AllowReserved;
Extensions = encoding?.Extensions != null ? new Dictionary<string, IOpenApiExtension>(encoding.Extensions) : null;
}
Expand Down Expand Up @@ -106,7 +114,10 @@ private void SerializeInternal(IOpenApiWriter writer, OpenApiSpecVersion version
writer.WriteProperty(OpenApiConstants.Style, Style?.GetDisplayName());

// explode
writer.WriteProperty(OpenApiConstants.Explode, Explode, false);
if (_explode.HasValue)
{
writer.WriteProperty(OpenApiConstants.Explode, Explode);
}

// allowReserved
writer.WriteProperty(OpenApiConstants.AllowReserved, AllowReserved, false);
Expand Down
50 changes: 50 additions & 0 deletions test/Microsoft.OpenApi.Tests/Models/OpenApiEncodingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,55 @@ public async Task SerializeAdvanceEncodingAsV3YamlWorks()
expected = expected.MakeLineBreaksEnvironmentNeutral();
Assert.Equal(expected, actual);
}

[Theory]
[InlineData(ParameterStyle.Form, true)]
[InlineData(ParameterStyle.SpaceDelimited, false)]
[InlineData(null, false)]
public void WhenStyleIsFormTheDefaultValueOfExplodeShouldBeTrueOtherwiseFalse(ParameterStyle? style, bool expectedExplode)
{
// Arrange
var parameter = new OpenApiEncoding
{
Style = style
};

// Act & Assert
Assert.Equal(parameter.Explode, expectedExplode);
}

[Theory]
[InlineData(true, true)]
[InlineData(false, true)]
[InlineData(null, false)]
public async Task WhenExplodeIsSetOutputShouldHaveExplode(bool? expectedExplode, bool hasExplode)
{
// Arrange
OpenApiEncoding parameter = new()
{
ContentType = "multipart/form-data",
Style = ParameterStyle.Form,
Explode = expectedExplode,
};

var expected =
$"""
contentType: multipart/form-data
style: form
""";

if (hasExplode)
{
expected = expected + $"\nexplode: {expectedExplode.ToString().ToLower()}";
}

// Act
var actual = await parameter.SerializeAsYamlAsync(OpenApiSpecVersion.OpenApi3_0);

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