Skip to content

Commit

Permalink
Fixed default values of nested schemas (#104)
Browse files Browse the repository at this point in the history
Closes #97
  • Loading branch information
PascalSenn authored Jul 13, 2023
1 parent b1cea7a commit 4e9dfec
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,24 @@ protected override JsonNode Rewrite(JsonObject obj, DefaultValueVisitorContext c
continue;
}

var possiblePropertySchemas = propertySchema
.GetPossibleSchemas(context.ReferenceResolver)
.ToArray();

context.Schemas.Push(propertySchema);

// when the field is null and we have a default value, set it
if (!obj.ContainsKey(field))
{
if (propertySchema.GetDefault() is { } defaultValue)
if (possiblePropertySchemas.GetDefaultValue() is { } defaultValue)
{
obj[field] = defaultValue;
}
else if (requiredProperties != null && requiredProperties.Contains(field))
{
obj[field] = propertySchema.IsArray()
? new JsonArray()
: propertySchema
.GetPossibleSchemas(context.ReferenceResolver)
: possiblePropertySchemas
.Where(x => x.GetProperties() is { Count: > 0 })
.SingleOrNone() is not null
? new JsonObject()
Expand All @@ -67,8 +70,7 @@ protected override JsonNode Rewrite(JsonObject obj, DefaultValueVisitorContext c
// when the field is null and we have a required field, initialize it
if (obj.ContainsKey(field) && obj[field] is null)
{
var hasSchemaOrDefaults = propertySchema
.GetPossibleSchemas(context.ReferenceResolver)
var hasSchemaOrDefaults = possiblePropertySchemas
.Where(x => x.GetRequired() is { Count: > 0 })
.SingleOrNone() is not null;

Expand Down Expand Up @@ -163,6 +165,12 @@ public static IEnumerable<JsonSchema> GetPossibleSchemas(
}
}

public static JsonNode? GetDefaultValue(this IEnumerable<JsonSchema> schemas)
=> schemas
.Select(x => x.GetDefault())
.OfType<JsonNode>()
.SingleOrNone();

public static JsonObject AddRequiredFields(this JsonObject value, JsonSchema schema)
{
var properties = schema.GetProperties() ?? ImmutableDictionary<string, JsonSchema>.Empty;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,7 @@ type Nested {
var result = DefaultValueVisitor.ApplyDefaults(schema, value!);

// assert
result.MatchInline("""
result.MatchInline("""
{
"nested": "should-still-exists"
}
Expand Down Expand Up @@ -868,6 +868,62 @@ type Deeper {
Match(result);
}

[Fact]
public void Should_InitializeComplexObjectArrays_When_InComponent()
{
// arrange

var jsonSchema = Create(
new Definition("HealthChecks",
"""
type Configuration {
Endpoints: [Endpoint!]! @defaultValue(value: [
{
Path: "/_health/live",
TagsPredicate: [
"liveness"
]
},
{
Path: "/_health/ready",
TagsPredicate: [
"readyness"
]
}
])
}
type Endpoint {
Path: String!
TagsPredicate: [String]!
}
"""));
// act
var result = DefaultValueVisitor.ApplyDefaults(jsonSchema, JsonNode.Parse("{}")!);

// assert
result.MatchInline("""
{
"HealthChecks": {
"Endpoints": [
{
"Path": "/_health/live",
"TagsPredicate": [
"liveness"
]
},
{
"Path": "/_health/ready",
"TagsPredicate": [
"readyness"
]
}
]
}
}
""");
}

private static void Match(JsonNode result)
{
result.ToJsonString(new JsonSerializerOptions { WriteIndented = true }).MatchSnapshot();
Expand Down

0 comments on commit 4e9dfec

Please sign in to comment.