Fix JSON schema generation for Dry::Struct wrapped in constructors #497
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fix JSON schema generation for Dry::Struct wrapped in constructors
Fixes #495
Problem
When generating JSON schema for a schema containing a
Dry::Struct
wrapped in a constructor (e.g.,Address.constructor(&:itself)
), all struct properties were omitted from the generated schema, returning only{type: "object"}
instead of the full schema with properties.Root Cause
The struct? method in the struct extension only recognized direct struct classes (Class instances that inherit
from
Dry::Struct
), but not struct constructors (Dry::Types::Constructor
instances wrapping structs).Solution
• Extended
struct?
method to also recognizeDry::Types::Constructor
instances that wrap struct classes• Added
extract_struct_class
method to extract the underlying struct class from both direct structs andconstructors
• Modified the macro processing to use the extracted struct class for schema compilation
Changes
•
lib/dry/schema/extensions/struct.rb
: Enhanced struct detection and extraction logic•
spec/integration/extensions/json_schema/struct_constructor_spec.rb
: Comprehensive test coverage•
CHANGELOG.md
: Document the fixBefore/After
Testing
• Tests direct struct usage (still works)
• Tests struct constructor usage (now works)
• Verifies both generate schemas with proper struct properties