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

Schema#ref does not apply custom configuration options #198

Open
ahx opened this issue Nov 9, 2024 · 4 comments
Open

Schema#ref does not apply custom configuration options #198

ahx opened this issue Nov 9, 2024 · 4 comments

Comments

@ahx
Copy link
Contributor

ahx commented Nov 9, 2024

Hi. What I would like to do is to use insert_property_defaults: true for subschemas that are adressed via Schema#ref without globally changing JSONSchemer.configuration.
But currently that does not seem to be possible, because Schema#ref does not apply the custom local options, but only uses the global configuration and passing insert_property_defaults: true to Schema#validate is not supported.

Here is an example:

doc = JSONSchemer.openapi(contents, insert_property_defaults: true)
content_schema = doc.ref('/components/schemas/MySchema')
content_schema.validate(data) # Fails, because insert_property_defaults is not applied (false)
# Now this (hack) makes it apply the option, but also changes global configuration, which I don't want:
doc = JSONSchemer.openapi(contents,)
content_schema = doc.ref('/components/schemas/MySchema')
content_schema.configuration.insert_property_defaults = true
content_schema.validate(data) # Succeeds

puts JSONSchemer.configuration.insert_property_defaults # => true

If you have a suggestion for an API to solve this, I'd be happy to try to create a PR for that.

@ahx ahx changed the title Schema#ref does not use custom options configuration Schema#ref does not apply custom configuration options Nov 9, 2024
@ahx
Copy link
Contributor Author

ahx commented Nov 9, 2024

Ha! I've found a solution.

  configuration = JSONSchemer::Configuration.new(insert_property_defaults: true)
  doc = JSONSchemer.openapi(resolved, configuration:)
  content_schema = doc.ref('/components/schemas/MySchema')
  content_schema.validate(data) # Succeeds! 

Thanks! 🦆
Feel free to close this issue.

@ahx ahx closed this as completed Dec 6, 2024
@davishmcclurg
Copy link
Owner

Hi @ahx, sorry for the delay here. I'm going to reopen this because I think it's an issue that subschemas don't inherit some options. It seems like we need to merge (some of?) the initialize options back into configuration:

@configuration = configuration

so that they get passed to the ref schema:

:configuration => configuration,

Let me know if you want to work on a fix!

@davishmcclurg davishmcclurg reopened this Dec 15, 2024
@ahx
Copy link
Contributor Author

ahx commented Dec 16, 2024

Hey David, I agree and I would like to work on a fix.
What I would also like is support for being able to overwrite configuration per subschema, Like doc.ref('/components/schemas/MySchema', after_property_validation: myproc)

I'll try to work on this. If I don't get back here until the new year, feel free to poke me.

@tonobo
Copy link

tonobo commented Jan 3, 2025

Yep, also hit this case. The library defaults get inherited.

if root.before_property_validation.any?

it took me a moment to figure out that “root” is not “root”, but in the “parsed” loop below it saw the properties, then I came up with the issue.

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