Skip to content

Commit 188fb9c

Browse files
authored
Merge pull request #376 from senthilkumarmohan/fix#375-Add-schema-rules-for-discriminator-and-required-fields
Fix#375 added schema rules for discriminator and required fields
2 parents e6f8c79 + dfbf2de commit 188fb9c

File tree

6 files changed

+78
-5
lines changed

6 files changed

+78
-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>

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
}

0 commit comments

Comments
 (0)