Skip to content

Commit 99f2b86

Browse files
Merge pull request #1173 from microsoft/bugfix/style
Fixes parameter style property getter that changes model data
2 parents b1cf08d + 9548a21 commit 99f2b86

8 files changed

+53
-31
lines changed

src/Microsoft.OpenApi/Models/OpenApiParameter.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ namespace Microsoft.OpenApi.Models
1616
public class OpenApiParameter : IOpenApiSerializable, IOpenApiReferenceable, IEffective<OpenApiParameter>, IOpenApiExtensible
1717
{
1818
private bool? _explode;
19-
public ParameterStyle? _style;
19+
private ParameterStyle? _style;
2020

2121
/// <summary>
2222
/// Indicates if object is populated with data or is just a reference to the data
@@ -75,8 +75,8 @@ public class OpenApiParameter : IOpenApiSerializable, IOpenApiReferenceable, IEf
7575
/// for cookie - form.
7676
/// </summary>
7777
public ParameterStyle? Style
78-
{
79-
get => _style ?? SetDefaultStyleValue();
78+
{
79+
get => _style ?? GetDefaultStyleValue();
8080
set => _style = value;
8181
}
8282

@@ -240,7 +240,10 @@ public void SerializeAsV3WithoutReference(IOpenApiWriter writer)
240240
writer.WriteProperty(OpenApiConstants.AllowEmptyValue, AllowEmptyValue, false);
241241

242242
// style
243-
writer.WriteProperty(OpenApiConstants.Style, Style?.GetDisplayName());
243+
if (_style.HasValue)
244+
{
245+
writer.WriteProperty(OpenApiConstants.Style, Style.Value.GetDisplayName());
246+
}
244247

245248
// explode
246249
writer.WriteProperty(OpenApiConstants.Explode, Explode, Style.HasValue && Style.Value == ParameterStyle.Form);
@@ -401,7 +404,7 @@ public void SerializeAsV2WithoutReference(IOpenApiWriter writer)
401404
writer.WriteEndObject();
402405
}
403406

404-
private ParameterStyle? SetDefaultStyleValue()
407+
private ParameterStyle? GetDefaultStyleValue()
405408
{
406409
Style = In switch
407410
{
@@ -411,7 +414,7 @@ public void SerializeAsV2WithoutReference(IOpenApiWriter writer)
411414
ParameterLocation.Cookie => (ParameterStyle?)ParameterStyle.Form,
412415
_ => (ParameterStyle?)ParameterStyle.Simple,
413416
};
414-
417+
415418
return Style;
416419
}
417420

test/Microsoft.OpenApi.Tests/Models/OpenApiDocumentTests.SerializeAdvancedDocumentAsV3JsonWorks_produceTerseOutput=False.verified.txt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
"name": "tags",
3131
"in": "query",
3232
"description": "tags to filter by",
33-
"style": "form",
3433
"schema": {
3534
"type": "array",
3635
"items": {
@@ -42,7 +41,6 @@
4241
"name": "limit",
4342
"in": "query",
4443
"description": "maximum number of results to return",
45-
"style": "form",
4644
"schema": {
4745
"type": "integer",
4846
"format": "int32"
@@ -266,7 +264,6 @@
266264
"in": "path",
267265
"description": "ID of pet to fetch",
268266
"required": true,
269-
"style": "simple",
270267
"schema": {
271268
"type": "integer",
272269
"format": "int64"
@@ -378,7 +375,6 @@
378375
"in": "path",
379376
"description": "ID of pet to delete",
380377
"required": true,
381-
"style": "simple",
382378
"schema": {
383379
"type": "integer",
384380
"format": "int64"

test/Microsoft.OpenApi.Tests/Models/OpenApiDocumentTests.SerializeAdvancedDocumentWithReferenceAsV3JsonWorks_produceTerseOutput=False.verified.txt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
"name": "tags",
3131
"in": "query",
3232
"description": "tags to filter by",
33-
"style": "form",
3433
"schema": {
3534
"type": "array",
3635
"items": {
@@ -42,7 +41,6 @@
4241
"name": "limit",
4342
"in": "query",
4443
"description": "maximum number of results to return",
45-
"style": "form",
4644
"schema": {
4745
"type": "integer",
4846
"format": "int32"
@@ -151,7 +149,6 @@
151149
"in": "path",
152150
"description": "ID of pet to fetch",
153151
"required": true,
154-
"style": "simple",
155152
"schema": {
156153
"type": "integer",
157154
"format": "int64"
@@ -205,7 +202,6 @@
205202
"in": "path",
206203
"description": "ID of pet to delete",
207204
"required": true,
208-
"style": "simple",
209205
"schema": {
210206
"type": "integer",
211207
"format": "int64"

test/Microsoft.OpenApi.Tests/Models/OpenApiDocumentTests.SerializeDuplicateExtensionsAsV3JsonWorks_produceTerseOutput=False.verified.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
"in": "path",
2121
"description": "The first operand",
2222
"required": true,
23-
"style": "simple",
2423
"schema": {
2524
"type": "integer",
2625
"my-extension": 4
@@ -32,7 +31,6 @@
3231
"in": "path",
3332
"description": "The second operand",
3433
"required": true,
35-
"style": "simple",
3634
"schema": {
3735
"type": "integer",
3836
"my-extension": 4

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

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -334,13 +334,11 @@ public void SerializeOperationWithBodyAsV3JsonWorks()
334334
""parameters"": [
335335
{
336336
""name"": ""parameter1"",
337-
""in"": ""path"",
338-
""style"": ""simple""
337+
""in"": ""path""
339338
},
340339
{
341340
""name"": ""parameter2"",
342-
""in"": ""header"",
343-
""style"": ""simple""
341+
""in"": ""header""
344342
}
345343
],
346344
""requestBody"": {
@@ -409,13 +407,11 @@ public void SerializeAdvancedOperationWithTagAndSecurityAsV3JsonWorks()
409407
""parameters"": [
410408
{
411409
""name"": ""parameter1"",
412-
""in"": ""path"",
413-
""style"": ""simple""
410+
""in"": ""path""
414411
},
415412
{
416413
""name"": ""parameter2"",
417-
""in"": ""header"",
418-
""style"": ""simple""
414+
""in"": ""header""
419415
}
420416
],
421417
""requestBody"": {
@@ -505,7 +501,6 @@ public void SerializeOperationWithFormDataAsV3JsonWorks()
505501
""in"": ""path"",
506502
""description"": ""ID of pet that needs to be updated"",
507503
""required"": true,
508-
""style"": ""simple"",
509504
""schema"": {
510505
""type"": ""string""
511506
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
{
22
"name": "name1",
3-
"in": "path",
4-
"style": "simple"
3+
"in": "path"
54
}

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

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,20 @@ public class OpenApiParameterTests
110110
}
111111

112112
};
113+
114+
public static OpenApiParameter QueryParameterWithMissingStyle = new OpenApiParameter
115+
{
116+
Name = "id",
117+
In = ParameterLocation.Query,
118+
Schema = new OpenApiSchema
119+
{
120+
Type = "object",
121+
AdditionalProperties = new OpenApiSchema
122+
{
123+
Type = "integer"
124+
}
125+
}
126+
};
113127

114128
public static OpenApiParameter AdvancedHeaderParameterWithSchemaReference = new OpenApiParameter
115129
{
@@ -186,7 +200,7 @@ public void WhenStyleIsFormTheDefaultValueOfExplodeShouldBeTrueOtherwiseFalse(Pa
186200

187201
// Act & Assert
188202
parameter.Explode.Should().Be(expectedExplode);
189-
}
203+
}
190204

191205
[Theory]
192206
[InlineData(ParameterLocation.Path, ParameterStyle.Simple)]
@@ -197,24 +211,46 @@ public void WhenStyleIsFormTheDefaultValueOfExplodeShouldBeTrueOtherwiseFalse(Pa
197211
public void WhenStyleAndInIsNullTheDefaultValueOfStyleShouldBeSimple(ParameterLocation? inValue, ParameterStyle expectedStyle)
198212
{
199213
// Arrange
214+
var outputStringWriter = new StringWriter(CultureInfo.InvariantCulture);
215+
var writer = new OpenApiJsonWriter(outputStringWriter, new OpenApiJsonWriterSettings { Terse = false });
200216
var parameter = new OpenApiParameter
201217
{
202218
Name = "name1",
203219
In = inValue,
204220
};
205221

206222
// Act & Assert
223+
parameter.SerializeAsV3(writer);
224+
writer.Flush();
225+
207226
parameter.Style.Should().Be(expectedStyle);
208227
}
209228

229+
[Fact]
230+
public void SerializeQueryParameterWithMissingStyleSucceeds()
231+
{
232+
// Arrange
233+
var expected = @"name: id
234+
in: query
235+
schema:
236+
type: object
237+
additionalProperties:
238+
type: integer";
239+
240+
// Act
241+
var actual = QueryParameterWithMissingStyle.SerializeAsYaml(OpenApiSpecVersion.OpenApi3_0);
242+
243+
// Assert
244+
actual.MakeLineBreaksEnvironmentNeutral().Should().Be(expected.MakeLineBreaksEnvironmentNeutral());
245+
}
246+
210247
[Fact]
211248
public void SerializeBasicParameterAsV3JsonWorks()
212249
{
213250
// Arrange
214251
var expected = @"{
215252
""name"": ""name1"",
216-
""in"": ""path"",
217-
""style"": ""simple""
253+
""in"": ""path""
218254
}";
219255

220256
// Act

test/Microsoft.OpenApi.Tests/PublicApi/PublicApi.approved.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -729,7 +729,6 @@ namespace Microsoft.OpenApi.Models
729729
}
730730
public class OpenApiParameter : Microsoft.OpenApi.Interfaces.IEffective<Microsoft.OpenApi.Models.OpenApiParameter>, Microsoft.OpenApi.Interfaces.IOpenApiElement, Microsoft.OpenApi.Interfaces.IOpenApiExtensible, Microsoft.OpenApi.Interfaces.IOpenApiReferenceable, Microsoft.OpenApi.Interfaces.IOpenApiSerializable
731731
{
732-
public Microsoft.OpenApi.Models.ParameterStyle? _style;
733732
public OpenApiParameter() { }
734733
public OpenApiParameter(Microsoft.OpenApi.Models.OpenApiParameter parameter) { }
735734
public bool AllowEmptyValue { get; set; }

0 commit comments

Comments
 (0)