Skip to content

Commit 27c0e78

Browse files
Merge pull request #1814 from microsoft/mk/fix-default-missing-properties-to-empty-list
Fix: Do not default to an empty list if Security scheme is missing
2 parents da3dfd9 + 18d99e6 commit 27c0e78

File tree

3 files changed

+64
-4
lines changed

3 files changed

+64
-4
lines changed

src/Microsoft.OpenApi/Models/OpenApiDocument.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) Microsoft Corporation. All rights reserved.
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT license.
33

44
using System;
@@ -48,8 +48,7 @@ public class OpenApiDocument : IOpenApiSerializable, IOpenApiExtensible, IOpenAp
4848
/// <summary>
4949
/// A declaration of which security mechanisms can be used across the API.
5050
/// </summary>
51-
public IList<OpenApiSecurityRequirement> SecurityRequirements { get; set; } =
52-
new List<OpenApiSecurityRequirement>();
51+
public IList<OpenApiSecurityRequirement> SecurityRequirements { get; set; }
5352

5453
/// <summary>
5554
/// A list of tags used by the specification with additional metadata.

src/Microsoft.OpenApi/Models/OpenApiOperation.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public class OpenApiOperation : IOpenApiSerializable, IOpenApiExtensible, IOpenA
9191
/// This definition overrides any declared top-level security.
9292
/// To remove a top-level security declaration, an empty array can be used.
9393
/// </summary>
94-
public IList<OpenApiSecurityRequirement> Security { get; set; } = new List<OpenApiSecurityRequirement>();
94+
public IList<OpenApiSecurityRequirement> Security { get; set; }
9595

9696
/// <summary>
9797
/// An alternative server array to service this operation.

test/Microsoft.OpenApi.Readers.Tests/V3Tests/OpenApiDocumentTests.cs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using FluentAssertions;
1111
using Microsoft.OpenApi.Any;
1212
using Microsoft.OpenApi.Exceptions;
13+
using Microsoft.OpenApi.Extensions;
1314
using Microsoft.OpenApi.Interfaces;
1415
using Microsoft.OpenApi.Models;
1516
using Microsoft.OpenApi.Readers.Interface;
@@ -1432,5 +1433,65 @@ public void ParseBasicDocumentWithServerVariableAndNoDefaultShouldFail()
14321433

14331434
diagnostic.Errors.Should().NotBeEmpty();
14341435
}
1436+
1437+
[Fact]
1438+
public void ParseDocumentWithMissingSecuritySchemeDefaultsToNull()
1439+
{
1440+
// Arrange
1441+
var input = @"openapi: 3.0.0
1442+
info:
1443+
title: test
1444+
version: ""1.0""
1445+
paths:
1446+
/test:
1447+
get:
1448+
description: description for test path
1449+
responses:
1450+
'200':
1451+
description: test
1452+
components:
1453+
securitySchemes:
1454+
apiKey0:
1455+
type: apiKey,
1456+
name: x-api-key,
1457+
in: header";
1458+
1459+
// Act && Assert
1460+
var doc = new OpenApiStringReader().Read(input, out var diagnostic);
1461+
1462+
doc.Paths["/test"].Operations[OperationType.Get].Security.Should().BeNull();
1463+
doc.SecurityRequirements.Should().BeNull();
1464+
}
1465+
1466+
[Fact]
1467+
public void ParseDocumentWithEmptySecuritySchemeDefaultsToEmptyList()
1468+
{
1469+
// Arrange
1470+
var input = @"openapi: 3.0.0
1471+
info:
1472+
title: test
1473+
version: ""1.0""
1474+
paths:
1475+
/test:
1476+
get:
1477+
description: description for test path
1478+
responses:
1479+
'200':
1480+
description: test
1481+
security: []
1482+
security:
1483+
- apiKey0: []
1484+
components:
1485+
securitySchemes:
1486+
apiKey0:
1487+
type: apiKey,
1488+
name: x-api-key,
1489+
in: header";
1490+
1491+
// Act && Assert
1492+
var doc = new OpenApiStringReader().Read(input, out var diagnostic);
1493+
1494+
doc.Paths["/test"].Operations[OperationType.Get].Security.Should().BeEmpty();
1495+
}
14351496
}
14361497
}

0 commit comments

Comments
 (0)