Skip to content

Commit e43c8c7

Browse files
Merge pull request #3 from Microsoft/master
sync
2 parents 24be792 + 188fb9c commit e43c8c7

File tree

8 files changed

+87
-5
lines changed

8 files changed

+87
-5
lines changed

src/Microsoft.OpenApi/Properties/SRResource.Designer.cs

Lines changed: 12 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Microsoft.OpenApi/Properties/SRResource.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,9 @@
216216
<data name="Validation_RuleAddTwice" xml:space="preserve">
217217
<value>The same rule cannot be in the same rule set twice.</value>
218218
</data>
219+
<data name="Validation_SchemaRequiredFieldListMustContainThePropertySpecifiedInTheDiscriminator" xml:space="preserve">
220+
<value>Schema {0} must contain property specified in the discriminator {1} in the required field list.</value>
221+
</data>
219222
<data name="Validation_StringMustBeEmailAddress" xml:space="preserve">
220223
<value>The string '{0}' MUST be in the format of an email address.</value>
221224
</data>

src/Microsoft.OpenApi/Services/OpenApiWalker.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -772,7 +772,7 @@ internal void Walk(OpenApiSchema schema)
772772

773773
if (schema.AnyOf != null)
774774
{
775-
Walk("anyOf", () => Walk(schema.AllOf));
775+
Walk("anyOf", () => Walk(schema.AnyOf));
776776
}
777777

778778
if (schema.Properties != null) {
@@ -883,7 +883,7 @@ internal void Walk(IList<OpenApiSchema> schemas)
883883
}
884884
}
885885
}
886-
886+
887887
/// <summary>
888888
/// Visits <see cref="OpenApiOAuthFlows"/> and child objects
889889
/// </summary>

src/Microsoft.OpenApi/Validations/Rules/OpenApiSchemaRules.cs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,29 @@ public static class OpenApiSchemaRules
5757
context.Exit();
5858
});
5959

60+
/// <summary>
61+
/// Validates Schema Discriminator
62+
/// </summary>
63+
public static ValidationRule<OpenApiSchema> ValidateSchemaDiscriminator =>
64+
new ValidationRule<OpenApiSchema>(
65+
(context, schema) =>
66+
{
67+
// discriminator
68+
context.Enter("discriminator");
69+
70+
if(schema.Reference != null && schema.Discriminator != null)
71+
{
72+
if (!schema.Required.Contains(schema.Discriminator?.PropertyName))
73+
{
74+
context.CreateError(nameof(ValidateSchemaDiscriminator),
75+
string.Format(SRResource.Validation_SchemaRequiredFieldListMustContainThePropertySpecifiedInTheDiscriminator,
76+
schema.Reference.Id, schema.Discriminator.PropertyName));
77+
}
78+
}
79+
80+
context.Exit();
81+
});
82+
6083
/// <summary>
6184
/// Validates OneOf Discriminator
6285
/// </summary>
@@ -97,7 +120,7 @@ public static class OpenApiSchemaRules
97120

98121
// add more rule.
99122

100-
123+
101124
/// <summary>
102125
/// Checks if the schemas in the list contain a property with the property name specified by the discriminator.
103126
/// </summary>

src/Microsoft.OpenApi/Writers/SpecialCharacterStringExtensions.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,13 @@ public static class SpecialCharacterStringExtensions
5454
","
5555
};
5656

57+
// Plain style strings cannot end with these characters.
58+
// http://www.yaml.org/spec/1.2/spec.html#style/flow/plain
59+
private static readonly string[] _yamlPlainStringForbiddenTerminals =
60+
{
61+
":"
62+
};
63+
5764
// Double-quoted strings are needed for these non-printable control characters.
5865
// http://www.yaml.org/spec/1.2/spec.html#style/flow/double-quoted
5966
private static readonly char[] _yamlControlCharacters =
@@ -170,6 +177,7 @@ internal static string GetYamlCompatibleString(this string input)
170177
// http://www.yaml.org/spec/1.2/spec.html#style/flow/plain
171178
if (_yamlPlainStringForbiddenCombinations.Any(fc => input.Contains(fc)) ||
172179
_yamlIndicators.Any(i => input.StartsWith(i.ToString())) ||
180+
_yamlPlainStringForbiddenTerminals.Any(i => input.EndsWith(i.ToString())) ||
173181
input.Trim() != input)
174182
{
175183
// Escape single quotes with two single quotes.

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,5 +352,41 @@ public void ValidateAnyOfCompositeSchemaMustContainPropertySpecifiedInTheDiscrim
352352

353353
});
354354
}
355+
356+
[Fact]
357+
public void ValidateSchemaRequiredFieldListMustContainThePropertySpecifiedInTheDiscriminator()
358+
{
359+
IEnumerable<OpenApiError> errors;
360+
var components = new OpenApiComponents
361+
{
362+
Schemas = {
363+
{
364+
"schema1",
365+
new OpenApiSchema
366+
{
367+
Type = "object",
368+
Discriminator = new OpenApiDiscriminator { PropertyName = "property1" },
369+
Reference = new OpenApiReference { Id = "schema1" }
370+
}
371+
}
372+
}
373+
};
374+
// Act
375+
var validator = new OpenApiValidator(ValidationRuleSet.GetDefaultRuleSet());
376+
var walker = new OpenApiWalker(validator);
377+
walker.Walk(components);
378+
379+
errors = validator.Errors;
380+
bool result = !errors.Any();
381+
382+
// Assert
383+
result.Should().BeFalse();
384+
errors.ShouldAllBeEquivalentTo(new List<OpenApiValidatorError>
385+
{
386+
new OpenApiValidatorError(nameof(OpenApiSchemaRules.ValidateSchemaDiscriminator),"#/schemas/schema1/discriminator",
387+
string.Format(SRResource.Validation_SchemaRequiredFieldListMustContainThePropertySpecifiedInTheDiscriminator,
388+
"schema1", "property1"))
389+
});
390+
}
355391
}
356392
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public void DefaultRuleSetPropertyReturnsTheCorrectRules()
4343
Assert.NotEmpty(rules);
4444

4545
// Update the number if you add new default rule(s).
46-
Assert.Equal(22, rules.Count);
46+
Assert.Equal(23, rules.Count);
4747
}
4848
}
4949
}

test/Microsoft.OpenApi.Tests/Writers/OpenApiWriterSpecialCharacterTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ public void WriteStringWithSpecialCharactersAsJsonWorks(string input, string exp
6060
[InlineData("true", " 'true'")]
6161
[InlineData("trailingspace ", " 'trailingspace '")]
6262
[InlineData(" trailingspace", " ' trailingspace'")]
63+
[InlineData("terminal:", " 'terminal:'")]
6364
public void WriteStringWithSpecialCharactersAsYamlWorks(string input, string expected)
6465
{
6566
// Arrange

0 commit comments

Comments
 (0)