Skip to content

Commit 84b55be

Browse files
authored
Removed the exclusions for defaulted values from required checking. (#706)
Closes #705
1 parent ee6ea40 commit 84b55be

File tree

3 files changed

+35
-10
lines changed

3 files changed

+35
-10
lines changed

Solutions/Corvus.Json.CodeGeneration.CSharp/Corvus.Json.CodeGeneration/CSharp/ValidationHandlers/ObjectChildHandlers/PropertiesValidationHandler.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,7 @@ static void AppendPropertyValidation(CodeGenerator generator, PropertyDeclaratio
9696
.AppendLineIndent("{")
9797
.PushIndent();
9898

99-
if (property.RequiredOrOptional == RequiredOrOptional.Required &&
100-
!property.ReducedPropertyType.HasDefaultValue())
99+
if (property.RequiredOrOptional == RequiredOrOptional.Required)
101100
{
102101
string hasSeenField = RequiredValidationHandler.GetHasSeenVariableName(generator, property);
103102
generator

Solutions/Corvus.Json.CodeGeneration.CSharp/Corvus.Json.CodeGeneration/CSharp/ValidationHandlers/ObjectChildHandlers/RequiredValidationHandler.cs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ public CodeGenerator AppendValidationCode(CodeGenerator generator, TypeDeclarati
3737
return generator;
3838
}
3939

40-
// Don't bother with properties that have default values.
41-
if (property.ReducedPropertyType.HasDefaultValue() || property.RequiredKeyword is not IObjectRequiredPropertyValidationKeyword keyword)
40+
if (property.RequiredKeyword is not IObjectRequiredPropertyValidationKeyword keyword)
4241
{
4342
continue;
4443
}
@@ -140,12 +139,6 @@ public CodeGenerator AppendValidateMethodSetup(CodeGenerator generator, TypeDecl
140139
return generator;
141140
}
142141

143-
// Don't bother with properties that have default values.
144-
if (property.ReducedPropertyType.HasDefaultValue())
145-
{
146-
continue;
147-
}
148-
149142
string requiredName = GetHasSeenVariableName(generator, property);
150143
generator
151144
.AppendLineIndent("bool ", requiredName, " = false;");
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
@draft202012
2+
3+
Feature: Repro 705 draft202012
4+
5+
Scenario Outline: Defaulted required property
6+
Given a schema file
7+
"""
8+
{
9+
"type": "object",
10+
"properties": {
11+
"regularProperty": { "type": "string" },
12+
"propertyWithDefault": { "$ref": "#/$defs/defaultedValue" },
13+
"unsetProperty": { "type": "string" }
14+
},
15+
"required": ["regularProperty", "propertyWithDefault"],
16+
"$defs": {
17+
"defaultedValue": {
18+
"type": "string",
19+
"default": "Hello"
20+
}
21+
}
22+
}
23+
"""
24+
And the input data value <inputData>
25+
And I generate a type for the schema
26+
And I construct an instance of the schema type from the data
27+
When I validate the instance with level Detailed
28+
Then the result will be <valid>
29+
30+
Examples:
31+
| inputData | valid |
32+
| {"regularProperty": "foo"} | false |
33+
| {"regularProperty": "foo", "propertyWithDefault": "bar"} | true |

0 commit comments

Comments
 (0)