Skip to content

Commit e761861

Browse files
committed
Updates based on code review
1 parent b532b1a commit e761861

File tree

5 files changed

+67
-9
lines changed

5 files changed

+67
-9
lines changed

src/Microsoft.OpenApi.Readers/Services/OpenApiReferenceResolver.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,7 @@ private void ResolveTags(IList<OpenApiTag> tags)
177177

178178
if (IsUnresolvedReference(entity))
179179
{
180-
var x = ResolveReference<T>(entity.Reference);
181-
assign(x);
180+
assign(ResolveReference<T>(entity.Reference));
182181
}
183182
}
184183

src/Microsoft.OpenApi.Workbench/StatsVisitor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
namespace Microsoft.OpenApi.Workbench
1010
{
11-
public class StatsVisitor : OpenApiVisitorBase
11+
internal class StatsVisitor : OpenApiVisitorBase
1212
{
1313
public int ParameterCount { get; set; } = 0;
1414

src/Microsoft.OpenApi/Validations/OpenApiValidator.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,10 @@ private void Validate<T>(T item)
168168
/// </summary>
169169
private void Validate(object item, Type type)
170170
{
171-
if (item == null) return; // Required fields should be checked by higher level objects
171+
if (item == null)
172+
{
173+
return; // Required fields should be checked by higher level objects
174+
}
172175

173176
// Validate unresolved references as references
174177
var potentialReference = item as IOpenApiReferenceable;

src/Microsoft.OpenApi/Validations/ValidationRuleSet.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,16 @@ public static ValidationRuleSet GetDefaultRuleSet()
5656
return new ValidationRuleSet(_defaultRuleSet);
5757
}
5858

59+
/// <summary>
60+
/// Return Ruleset with no rules
61+
/// </summary>
5962
public static ValidationRuleSet GetEmptyRuleSet()
6063
{
6164
// We create a new instance of ValidationRuleSet per call as a safeguard
6265
// against unintentional modification of the private _defaultRuleSet.
6366
return new ValidationRuleSet();
6467
}
68+
6569
/// <summary>
6670
/// Initializes a new instance of the <see cref="ValidationRuleSet"/> class.
6771
/// </summary>

test/Microsoft.OpenApi.Tests/Validations/OpenApiReferenceValidationTests.cs

Lines changed: 57 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
using System;
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license.
3+
4+
using System;
25
using System.Collections.Generic;
36
using System.Linq;
47
using System.Text;
@@ -64,12 +67,13 @@ public void ReferencedSchemaShouldOnlyBeValidatedOnce()
6467
};
6568

6669
// Act
67-
var errors = document.Validate(new ValidationRuleSet() { new AllwaysFailRule<OpenApiSchema>()});
70+
var errors = document.Validate(new ValidationRuleSet() { new AlwaysFailRule<OpenApiSchema>()});
6871

6972

7073
// Assert
7174
Assert.True(errors.Count() == 1);
7275
}
76+
7377
[Fact]
7478
public void UnResolvedReferencedSchemaShouldNotBeValidated()
7579
{
@@ -94,17 +98,65 @@ public void UnResolvedReferencedSchemaShouldNotBeValidated()
9498
};
9599

96100
// Act
97-
var errors = document.Validate(new ValidationRuleSet() { new AllwaysFailRule<OpenApiSchema>() });
101+
var errors = document.Validate(new ValidationRuleSet() { new AlwaysFailRule<OpenApiSchema>() });
98102

99103
// Assert
100104
Assert.True(errors.Count() == 0);
101105
}
102106

107+
[Fact]
108+
public void UnResolvedSchemaReferencedShouldNotBeValidated()
109+
{
110+
// Arrange
111+
112+
var sharedSchema = new OpenApiSchema
113+
{
114+
Reference = new OpenApiReference()
115+
{
116+
Id = "test"
117+
},
118+
UnresolvedReference = true
119+
};
120+
121+
OpenApiDocument document = new OpenApiDocument();
122+
123+
document.Paths = new OpenApiPaths()
124+
{
125+
["/"] = new OpenApiPathItem()
126+
{
127+
Operations = new Dictionary<OperationType, OpenApiOperation>
128+
{
129+
[OperationType.Get] = new OpenApiOperation()
130+
{
131+
Responses = new OpenApiResponses()
132+
{
133+
["200"] = new OpenApiResponse()
134+
{
135+
Content = new Dictionary<string, OpenApiMediaType>()
136+
{
137+
["application/json"] = new OpenApiMediaType()
138+
{
139+
Schema = sharedSchema
140+
}
141+
}
142+
}
143+
}
144+
}
145+
}
146+
}
147+
};
148+
149+
// Act
150+
var errors = document.Validate(new ValidationRuleSet() { new AlwaysFailRule<OpenApiSchema>() });
151+
152+
// Assert
153+
Assert.True(errors.Count() == 0);
154+
}
103155
}
104156

105-
public class AllwaysFailRule<P> : ValidationRule<P> where P: IOpenApiElement
157+
public class AlwaysFailRule<T> : ValidationRule<T> where T: IOpenApiElement
106158
{
107-
public AllwaysFailRule() : base( (c,t) => c.CreateError("x","y"))
159+
public AlwaysFailRule() : base( (c,t) => c.CreateError("x","y"))
108160
{
109161

110162
}

0 commit comments

Comments
 (0)