Skip to content

Commit db2385d

Browse files
authored
Bugfix/define schema for shale mapper objects (#52)
* Allow `Shale::Mapper` to define a `schema` * Update CHANGELOG
1 parent c185947 commit db2385d

File tree

4 files changed

+15
-3
lines changed

4 files changed

+15
-3
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## [unreleased]
2+
3+
### Fixed
4+
- [ollietulloch] Allow `Shale::Mapper` attributes to define a `schema`
5+
16
## [1.2.1] - 2025-01-09
27

38
### Fixed

lib/shale/schema/json_generator.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ def as_schema(klass, id: nil, title: nil, description: nil)
8787
next unless attribute
8888

8989
if mapper_type?(attribute.type)
90-
json_type = Ref.new(mapping.name, attribute.type.model.name)
90+
json_type = Ref.new(mapping.name, attribute.type.model.name, schema: mapping.schema)
9191
else
9292
json_klass = self.class.get_json_type(attribute.type)
9393

lib/shale/schema/json_generator/ref.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ class JSONGenerator
99
#
1010
# @api private
1111
class Ref < Base
12-
def initialize(name, type)
13-
super(name)
12+
def initialize(name, type, schema: nil)
13+
super(name, schema: schema)
1414
@type = type.gsub('::', '_')
1515
end
1616

spec/shale/schema/json_generator_spec.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,12 @@ class PersonMapper < Shale::Mapper
8383
attribute :first_name, :string
8484
attribute :last_name, :string
8585
attribute :address, AddressMapper
86+
87+
json do
88+
map 'first_name', to: :first_name
89+
map 'last_name', to: :last_name
90+
map 'address', to: :address, schema: { required: true }
91+
end
8692
end
8793
end
8894

@@ -319,6 +325,7 @@ class PersonMapper < Shale::Mapper
319325
'last_name' => { 'type' => %w[string null] },
320326
'address' => { '$ref' => '#/$defs/ShaleSchemaJSONGeneratorTesting_Address' },
321327
},
328+
'required' => ['address'],
322329
},
323330
},
324331
}

0 commit comments

Comments
 (0)