Skip to content
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

Can't insert property defaults when using definitions #173

Closed
rquant opened this issue Feb 16, 2024 · 4 comments
Closed

Can't insert property defaults when using definitions #173

rquant opened this issue Feb 16, 2024 · 4 comments

Comments

@rquant
Copy link

rquant commented Feb 16, 2024

Hi I am trying to create a re-usable definition in my schema, that contains some property defaults. Here is an example of my schema:

{
    "type": "object",
    "properties": {
      "email": {
        "$ref": "#/definitions/notification_setting"
      }
    },
    "definitions": {
      "notification_setting": {
        "type": "object",
        "properties": {
          "enabled": { "type": "boolean" },
          "deprecated": { "type": "boolean" }
        },
        "default": {
            "enabled": true,
            "deprecated": false
        }
      }
    }
  }

The issue is it doesn't appear the references are being resolved, and I'm not seeing default values being inserted into my empty hash:

irb(main):080:0> schema
=> {"type"=>"object", "properties"=>{"address"=>{"$ref"=>"#/definitions/address"}}, "definitions"=>{"address"=>{"type"=>"object", "properties"=>{"street"=>{"type"=>"string"}, "city"=>{"type"=>"string"}}, "default"=>{"street"=>"123 abc st", "city"=>"funky town"}}}}
irb(main):081:0> data = {}
=> {}
irb(main):083:0> JSONSchemer.schema(schema, insert_property_defaults: true).valid?(data)
=> true
irb(main):085:0> puts data
{}

Any idea on why this doesn't work? Do I need to provide a custom ref_resolver?

Thanks!

@ekzobrain
Copy link

It does not depend on ref_resolver, this schema uses only local json pointers. Seems like a bug with object + default or with $ref + default.

@davishmcclurg
Copy link
Owner

Thanks for opening this, @rquant! Looks like refs aren't followed when checking for default values. I'm working on a fix now.

davishmcclurg added a commit that referenced this issue Feb 25, 2024
This resolves any `ref_schema` keywords (`$ref`, `$dynamicRef`,
`$recursiveRef`) when looking for `default` keywords for
`insert_property_defaults`. It follows the keyword order defined in the
vocabulary (`$ref` first, then `$dynamicRef`/`$recursiveRef` depending
on the meta schema) and searches depth-first (ie, follows a `$ref` chain
until a leaf schema before moving on to a sibling `$dynamicRef`). The
first `default` keyword found is used, meaning a `$ref` default can be
overwritten by the including schema, eg:

```json
{
  "properties": {
    "example": {
      "$ref": "#/$defs/ref",
      "default": "override!"
    }
  },
  "$defs": {
    "ref": {
      "default": "overridden"
    }
  }
}
```

Closes: #173
@davishmcclurg
Copy link
Owner

@rquant I just opened a PR to address this: #175
Do you mind trying it out to make sure it works for you?

davishmcclurg added a commit that referenced this issue Mar 2, 2024
Features:

- Global configuration
- Symbol key property defaults
- Better schema validation support

See individual commits for more details.

Closes:

- #157
- #162
- #167
- #173
davishmcclurg added a commit that referenced this issue Mar 2, 2024
Features:

- Global configuration
- Symbol key property defaults
- Better schema validation support

See CHANGELOG.md and individual commits for more details.

Closes:

- #157
- #162
- #167
- #173
@davishmcclurg davishmcclurg mentioned this issue Mar 2, 2024
@davishmcclurg
Copy link
Owner

Fix has been merge and will be released shortly in 2.2.0. Original example works now:

?> schema = {
?>   "type": "object",
?>   "properties": {
?>     "email": {
?>       "$ref": "#/definitions/notification_setting"
?>     }
?>   },
?>   "definitions": {
?>     "notification_setting": {
?>       "type": "object",
?>       "properties": {
?>         "enabled": { "type": "boolean" },
?>         "deprecated": { "type": "boolean" }
?>       },
?>       "default": {
?>         "enabled": true,
?>         "deprecated": false
?>       }
?>     }
?>   }
>> }
=>
{:type=>"object",
...
>> data = {}
=> {}
>> JSONSchemer.schema(schema, insert_property_defaults: true).valid?(data)
=> true
>> puts data
{"email"=>{"enabled"=>true, "deprecated"=>false}}
=> nil

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants