Skip to content

"default" is not working inside $ref #506

@HemaSudha1498

Description

@HemaSudha1498

In the below example, The footerHeight is not resolved, and the default value is not set. I have approximately 50-70 properties of the same kind, all of which require default values, but duplicating the schema is not feasible.

My Schema:

{
    "$schema": "https://json-schema.org/draft-07/schema#",
    "type": "object",
    "properties": {
        "templateName": {
            "type": "string",
            "minLength": 2,
            "maxLength": 25
        },
        "headerHeight": {
            "type":"object",
            "additionalProperties": false,
            "default": {
                "units": "px",
                "value": 0
            },
            "properties": {
                "value": {
                    "type": "number"
                },
                "units": {
                    "type": "string",
                    "enum": ["px", "%"]
                }
            }
        },
        "footerHeight": {
            "$ref": "#/definitions/dimensionWithDefault"
        }
    },
    "definitions": {
        "dimensionWithDefault": {
            "type": "object",
            "additionalProperties": false,
            "default":{
                "value": 0,
                "units": "px"
            },
            "properties": {
                "value": {
                    "type": "number"
                },
                "units": {
                    "type": "string",
                    "enum": ["px", "%"]
                }
            }
        }
    },
    "required": ["templateName"]
}

My Code:

public class JsonExample {
    public static void main(String[] args) {
        JSONObject` input = new JSONObject("{\n" +
                "    \"templateName\": \"xyz\"\n" +
                "}");
        Schema schema = SchemaLoader.builder()
                .useDefaults(true)
                .schemaJson(rawSchema)
                .build()
                .load().build();

        schema.validate(input);
        System.out.println(input);
    }
}

The Output:

{
    "templateName": "xyz",
    "headerHeight": {
        "units": "px",
        "value": 0
    }
}

Kindly provide guidance on resolving this issue.

Thanks

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions