Skip to content

Commit f6c558d

Browse files
committed
Fix OpenApiEncoding explode property serialization
1 parent 0ba1965 commit f6c558d

File tree

2 files changed

+63
-2
lines changed

2 files changed

+63
-2
lines changed

src/Microsoft.OpenApi/Models/OpenApiEncoding.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ namespace Microsoft.OpenApi.Models
1313
/// </summary>
1414
public class OpenApiEncoding : IOpenApiSerializable, IOpenApiExtensible
1515
{
16+
/// <summary>
17+
/// Explode backing variable
18+
/// </summary>
19+
private bool? _explode;
1620
/// <summary>
1721
/// The Content-Type for encoding a specific property.
1822
/// The value can be a specific media type (e.g. application/json),
@@ -37,7 +41,11 @@ public class OpenApiEncoding : IOpenApiSerializable, IOpenApiExtensible
3741
/// For all other styles, the default value is false.
3842
/// This property SHALL be ignored if the request body media type is not application/x-www-form-urlencoded.
3943
/// </summary>
40-
public bool? Explode { get; set; }
44+
public bool? Explode
45+
{
46+
get => _explode ?? Style == ParameterStyle.Form;
47+
set => _explode = value;
48+
}
4149

4250
/// <summary>
4351
/// Determines whether the parameter value SHOULD allow reserved characters,
@@ -89,7 +97,10 @@ public void SerializeAsV3(IOpenApiWriter writer)
8997
writer.WriteProperty(OpenApiConstants.Style, Style?.GetDisplayName());
9098

9199
// explode
92-
writer.WriteProperty(OpenApiConstants.Explode, Explode, false);
100+
if (_explode.HasValue)
101+
{
102+
writer.WriteProperty(OpenApiConstants.Explode, Explode);
103+
}
93104

94105
// allowReserved
95106
writer.WriteProperty(OpenApiConstants.AllowReserved, AllowReserved, false);

test/Microsoft.OpenApi.Tests/Models/OpenApiEncodingTests.cs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,5 +78,55 @@ public void SerializeAdvanceEncodingAsV3YamlWorks()
7878
expected = expected.MakeLineBreaksEnvironmentNeutral();
7979
actual.Should().Be(expected);
8080
}
81+
82+
[Theory]
83+
[InlineData(ParameterStyle.Form, true)]
84+
[InlineData(ParameterStyle.SpaceDelimited, false)]
85+
[InlineData(null, false)]
86+
public void WhenStyleIsFormTheDefaultValueOfExplodeShouldBeTrueOtherwiseFalse(ParameterStyle? style, bool expectedExplode)
87+
{
88+
// Arrange
89+
var parameter = new OpenApiEncoding
90+
{
91+
Style = style
92+
};
93+
94+
// Act & Assert
95+
parameter.Explode.Should().Be(expectedExplode);
96+
}
97+
98+
[Theory]
99+
[InlineData(true, true)]
100+
[InlineData(false, true)]
101+
[InlineData(null, false)]
102+
public void WhenExplodeIsSetOutputShouldHaveExplode(bool? expectedExplode, bool hasExplode)
103+
{
104+
// Arrange
105+
OpenApiEncoding parameter = new()
106+
{
107+
ContentType = "multipart/form-data",
108+
Style = ParameterStyle.Form,
109+
Explode = expectedExplode,
110+
};
111+
112+
var expected =
113+
$"""
114+
contentType: multipart/form-data
115+
style: form
116+
""";
117+
118+
if (hasExplode)
119+
{
120+
expected = expected + $"\nexplode: {expectedExplode.ToString().ToLower()}";
121+
}
122+
123+
// Act
124+
var actual = parameter.SerializeAsYaml(OpenApiSpecVersion.OpenApi3_0);
125+
126+
// Assert
127+
actual = actual.MakeLineBreaksEnvironmentNeutral();
128+
expected = expected.MakeLineBreaksEnvironmentNeutral();
129+
actual.Should().Be(expected);
130+
}
81131
}
82132
}

0 commit comments

Comments
 (0)