Skip to content

Commit 3c2042f

Browse files
committed
fix the null reference if the collection in Operation being set as null
1 parent 74a90eb commit 3c2042f

File tree

2 files changed

+40
-7
lines changed

2 files changed

+40
-7
lines changed

src/Microsoft.OpenApi/Models/OpenApiOperation.cs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,15 @@ public void SerializeAsV2(IOpenApiWriter writer)
199199
// operationId
200200
writer.WriteProperty(OpenApiConstants.OperationId, OperationId);
201201

202-
var parameters = new List<OpenApiParameter>(Parameters);
202+
IList<OpenApiParameter> parameters;
203+
if (Parameters == null)
204+
{
205+
parameters = new List<OpenApiParameter>();
206+
}
207+
else
208+
{
209+
parameters = new List<OpenApiParameter>(Parameters);
210+
}
203211

204212
if (RequestBody != null)
205213
{
@@ -258,7 +266,9 @@ public void SerializeAsV2(IOpenApiWriter writer)
258266
// All schemes in the Servers are extracted, regardless of whether the host matches
259267
// the host defined in the outermost Swagger object. This is due to the
260268
// inaccessibility of information for that host in the context of an inner object like this Operation.
261-
var schemes = Servers.Select(
269+
if (Servers != null)
270+
{
271+
var schemes = Servers.Select(
262272
s =>
263273
{
264274
Uri.TryCreate(s.Url, UriKind.RelativeOrAbsolute, out var url);
@@ -268,7 +278,8 @@ public void SerializeAsV2(IOpenApiWriter writer)
268278
.Distinct()
269279
.ToList();
270280

271-
writer.WriteOptionalCollection(OpenApiConstants.Schemes, schemes, (w, s) => w.WriteValue(s));
281+
writer.WriteOptionalCollection(OpenApiConstants.Schemes, schemes, (w, s) => w.WriteValue(s));
282+
}
272283

273284
// deprecated
274285
writer.WriteProperty(OpenApiConstants.Deprecated, Deprecated, false);

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

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public OpenApiOperationTests(ITestOutputHelper output)
2626
{
2727
};
2828

29-
public static OpenApiOperation AdvancedOperation= new OpenApiOperation
29+
public static OpenApiOperation AdvancedOperation = new OpenApiOperation
3030
{
3131
Summary = "summary1",
3232
Description = "operationDescription",
@@ -61,7 +61,7 @@ public OpenApiOperationTests(ITestOutputHelper output)
6161
{
6262
Type = "number",
6363
Minimum = 5,
64-
Maximum = 10
64+
Maximum = 10
6565
}
6666
}
6767
}
@@ -229,7 +229,7 @@ public void SerializeBasicOperationAsV3JsonWorks()
229229
}";
230230

231231
// Act
232-
var actual = BasicOperation.SerializeAsJson(OpenApiSpecVersion.OpenApi3_0_0);
232+
var actual = BasicOperation.SerializeAsJson(OpenApiSpecVersion.OpenApi3_0_0);
233233

234234
_output.WriteLine(actual);
235235

@@ -308,7 +308,7 @@ public void SerializeAdvancedOperationAsV3JsonWorks()
308308
expected = expected.MakeLineBreaksEnvironmentNeutral();
309309
actual.Should().Be(expected);
310310
}
311-
311+
312312
[Fact]
313313
public void SerializeAdvancedOperationWithTagAndSecurityAsV3JsonWorks()
314314
{
@@ -555,5 +555,27 @@ public void SerializeAdvancedOperationWithTagAndSecurityAsV2JsonWorks()
555555
expected = expected.MakeLineBreaksEnvironmentNeutral();
556556
actual.Should().Be(expected);
557557
}
558+
559+
[Fact]
560+
public void SerializeOperationWithNullCollectionAsV2JsonWorks()
561+
{
562+
// Arrange
563+
var expected = @"{
564+
""responses"": { }
565+
}";
566+
OpenApiOperation operation = new OpenApiOperation
567+
{
568+
Parameters = null,
569+
Servers = null,
570+
};
571+
572+
// Act
573+
var actual = operation.SerializeAsJson(OpenApiSpecVersion.OpenApi2_0);
574+
575+
// Assert
576+
actual = actual.MakeLineBreaksEnvironmentNeutral();
577+
expected = expected.MakeLineBreaksEnvironmentNeutral();
578+
actual.Should().Be(expected);
579+
}
558580
}
559581
}

0 commit comments

Comments
 (0)