Skip to content

Commit ae03eab

Browse files
authored
Fix OpenApiEncoding explode property serialization (to main) (#2512)
fix: OpenApiEncoding explode property serialization defaults with form style
1 parent d93689c commit ae03eab

File tree

2 files changed

+64
-3
lines changed

2 files changed

+64
-3
lines changed

src/Microsoft.OpenApi/Models/OpenApiEncoding.cs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ namespace Microsoft.OpenApi
1111
/// </summary>
1212
public class OpenApiEncoding : IOpenApiSerializable, IOpenApiExtensible
1313
{
14+
/// <summary>
15+
/// Explode backing variable
16+
/// </summary>
17+
private bool? _explode;
1418
/// <summary>
1519
/// The Content-Type for encoding a specific property.
1620
/// The value can be a specific media type (e.g. application/json),
@@ -35,7 +39,11 @@ public class OpenApiEncoding : IOpenApiSerializable, IOpenApiExtensible
3539
/// For all other styles, the default value is false.
3640
/// This property SHALL be ignored if the request body media type is not application/x-www-form-urlencoded.
3741
/// </summary>
38-
public bool? Explode { get; set; }
42+
public bool? Explode
43+
{
44+
get => _explode ?? Style == ParameterStyle.Form;
45+
set => _explode = value;
46+
}
3947

4048
/// <summary>
4149
/// Determines whether the parameter value SHOULD allow reserved characters,
@@ -63,7 +71,7 @@ public OpenApiEncoding(OpenApiEncoding encoding)
6371
ContentType = encoding?.ContentType ?? ContentType;
6472
Headers = encoding?.Headers != null ? new Dictionary<string, IOpenApiHeader>(encoding.Headers) : null;
6573
Style = encoding?.Style ?? Style;
66-
Explode = encoding?.Explode ?? Explode;
74+
Explode = encoding?._explode;
6775
AllowReserved = encoding?.AllowReserved ?? AllowReserved;
6876
Extensions = encoding?.Extensions != null ? new Dictionary<string, IOpenApiExtension>(encoding.Extensions) : null;
6977
}
@@ -106,7 +114,10 @@ private void SerializeInternal(IOpenApiWriter writer, OpenApiSpecVersion version
106114
writer.WriteProperty(OpenApiConstants.Style, Style?.GetDisplayName());
107115

108116
// explode
109-
writer.WriteProperty(OpenApiConstants.Explode, Explode, false);
117+
if (_explode.HasValue)
118+
{
119+
writer.WriteProperty(OpenApiConstants.Explode, Explode);
120+
}
110121

111122
// allowReserved
112123
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
@@ -76,5 +76,55 @@ public async Task SerializeAdvanceEncodingAsV3YamlWorks()
7676
expected = expected.MakeLineBreaksEnvironmentNeutral();
7777
Assert.Equal(expected, actual);
7878
}
79+
80+
[Theory]
81+
[InlineData(ParameterStyle.Form, true)]
82+
[InlineData(ParameterStyle.SpaceDelimited, false)]
83+
[InlineData(null, false)]
84+
public void WhenStyleIsFormTheDefaultValueOfExplodeShouldBeTrueOtherwiseFalse(ParameterStyle? style, bool expectedExplode)
85+
{
86+
// Arrange
87+
var parameter = new OpenApiEncoding
88+
{
89+
Style = style
90+
};
91+
92+
// Act & Assert
93+
Assert.Equal(parameter.Explode, expectedExplode);
94+
}
95+
96+
[Theory]
97+
[InlineData(true, true)]
98+
[InlineData(false, true)]
99+
[InlineData(null, false)]
100+
public async Task WhenExplodeIsSetOutputShouldHaveExplode(bool? expectedExplode, bool hasExplode)
101+
{
102+
// Arrange
103+
OpenApiEncoding parameter = new()
104+
{
105+
ContentType = "multipart/form-data",
106+
Style = ParameterStyle.Form,
107+
Explode = expectedExplode,
108+
};
109+
110+
var expected =
111+
$"""
112+
contentType: multipart/form-data
113+
style: form
114+
""";
115+
116+
if (hasExplode)
117+
{
118+
expected = expected + $"\nexplode: {expectedExplode.ToString().ToLower()}";
119+
}
120+
121+
// Act
122+
var actual = await parameter.SerializeAsYamlAsync(OpenApiSpecVersion.OpenApi3_0);
123+
124+
// Assert
125+
actual = actual.MakeLineBreaksEnvironmentNeutral();
126+
expected = expected.MakeLineBreaksEnvironmentNeutral();
127+
Assert.Equal(actual, expected);
128+
}
79129
}
80130
}

0 commit comments

Comments
 (0)