Skip to content

resolve default through ref unless default exists at same level#69

Open
jwetzell wants to merge 2 commits intogoogle:mainfrom
jwetzell:resolve-defaults-inside-refs
Open

resolve default through ref unless default exists at same level#69
jwetzell wants to merge 2 commits intogoogle:mainfrom
jwetzell:resolve-defaults-inside-refs

Conversation

@jwetzell
Copy link
Copy Markdown

@jwetzell jwetzell commented Apr 2, 2026

This library does not follow a $ref when applying defaults. I've added test cases for the two scenarios I can think of where there is both and $ref (with it's own default) and a default at the same level and when there is just a $ref with it's own default inside. In this case the solution I have implemented only searches the $ref if there is no default on the subschema. Essentially meaning a subschema can override a default inside the $ref.

I did look through the JSONSchema reference about how default should be handled and found this blurb so I guess it is up to tooling.

the JSON Schema specification does not provide any guidance on how this keyword should be used

Either way is trivial to implement but it should at least be noted I guess.

@google-cla
Copy link
Copy Markdown

google-cla Bot commented Apr 2, 2026

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@jwetzell
Copy link
Copy Markdown
Author

CLA has been signed!

@jwetzell
Copy link
Copy Markdown
Author

not sure who to bother for a review but tagging the highest contributor! @jba

Comment thread jsonschema/validate.go Outdated
}

subschemaDefault := subschema.Default
subschemaInfo := st.rs.resolvedInfos[subschema]
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be nil? (I honestly don't remember.) Either way, it's safer to add a check on 730.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before you do, see if you can write a failing test.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to write a test that would get the resolved reference to be nil but it would always be caught by some earlier piece (json reference resolver or the checkStructure part in validate). Nil checks added though.

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

Successfully merging this pull request may close these issues.

2 participants