Skip to content

bug: nested defaults will not be applied #632

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
lgersman opened this issue May 19, 2020 · 2 comments
Closed

bug: nested defaults will not be applied #632

lgersman opened this issue May 19, 2020 · 2 comments
Labels

Comments

@lgersman
Copy link

lgersman commented May 19, 2020

If a json schema provides nested defaults like this schema :

{
  "title": "Schema default properties",
  "type": "object",
  "properties": {
    "valuesInFormData": {
      "title": "Values in form data",
      "$ref": "#/definitions/defaultsExample"
    },
    "noValuesInFormData": {
      "title": "No values in form data",
      "$ref": "#/definitions/defaultsExample"
    }
  },
  "definitions": {
    "defaultsExample": {
      "type": "object",
      "properties": {
        "scalar": {
          "title": "Scalar",
          "type": "string",
          "default": "scalar default"
        },
        "array": {
          "title": "Array",
          "type": "array",
          "items": {
            "type": "object",
            "properties": {
              "nested": {
                "title": "Nested array",
                "type": "string",
                "default": "nested array default"
              }
            }
          }
        },
        "object": {
          "title": "Object",
          "type": "object",
          "properties": {
            "nested": {
              "title": "Nested object",
              "type": "string",
              "default": "nested object default"
            }
          }
        }
      }
    }
  }
}

json-schema does not apply these defaults using Constraint::CHECK_MODE_APPLY_DEFAULTS flag.

only top level defaults will be applied.

@COil
Copy link

COil commented Nov 20, 2020

Hello @lgersman, we are facing the same issue. In fact, it works for nested properties of objects but it doesn't work for array types.

@DannyvdSluijs
Copy link
Collaborator

@lgersman I see this report have been left unanswered very long, that is on us. I'm however trying to wrok through the lisst of reports and I'm trying to resolve this one.

In reading the JSON Schema specification about defaults and one thing that seems important in this is:

It is RECOMMENDED that a default value be valid against the associated schema.
This is also reflected in code and thus intentional once a default value has been applied no defaults of deeper properties is applied. Some debugging also confirmed nested defaults are not applied by design.

In your schema there is no default for #/definitions/defaultsExample. Setting a valid default (so including properties and values for all properties of the #/definitions/defaultsExample definition on that level would resolve your problem.

Closing as "Not planned" in cause you've any additional information feel free to let us know more and reopen this report.

@DannyvdSluijs DannyvdSluijs closed this as not planned Won't fix, can't repro, duplicate, stale Feb 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants