blueprinter_schema 0.5.0 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.rubocop.yml +4 -0
- data/README.md +108 -41
- data/Rakefile +0 -9
- data/lib/blueprinter_schema/generator.rb +56 -15
- data/lib/blueprinter_schema/version.rb +1 -1
- data/lib/blueprinter_schema.rb +8 -2
- metadata +30 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 624ef3521849ad9f4b406b1236f7466ca0938ae3b1070b831ab29ef87d0e759e
|
4
|
+
data.tar.gz: e71133b659e4fe5b40ff09319be9fe196894d698f8138811609eddfa4211a5f1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: '080d444267259e5323f40c2a43a95bac153e3a7aadf4f82e099500b92a43251ac4eb75bad2df1a3a6a50e45f70710bac5b3120d8b20a83db65679ed403189ba0'
|
7
|
+
data.tar.gz: 812e6fdffdbaffb41fa62c3d37ff146f115069e4710b3447d73046561274b19d86dd168edb9bcc34e30975f16aea489d49f65c19c1662581bb0e5bde6a9dfd6c
|
data/.rubocop.yml
CHANGED
data/README.md
CHANGED
@@ -1,8 +1,6 @@
|
|
1
1
|
# BlueprinterSchema
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
Serializers define which fields are used. Models know the database field types. Put these together and you get a JSON Schema.
|
3
|
+
#### Create JSON Schemas from [Blueprinter](https://github.com/procore-oss/blueprinter) Serializers.
|
6
4
|
|
7
5
|
## Installation
|
8
6
|
|
@@ -14,33 +12,54 @@ gem "blueprinter_schema"
|
|
14
12
|
|
15
13
|
## Usage
|
16
14
|
|
17
|
-
With the folloing Models and Serializers:
|
18
15
|
```rb
|
19
|
-
class
|
20
|
-
|
16
|
+
class UserSerializer < Blueprinter::Base
|
17
|
+
field :first_name, type: [:string, :null]
|
18
|
+
field :last_name, type: [:string, :null]
|
19
|
+
field :full_name, type: [:string, :null], description: "The concatendated first and last name."
|
20
|
+
field :email, type: :string, format: :email
|
21
21
|
end
|
22
22
|
|
23
|
-
|
24
|
-
|
25
|
-
end
|
23
|
+
BlueprinterSchema.generate(serializer: UserSerializer)
|
24
|
+
```
|
26
25
|
|
27
|
-
|
28
|
-
|
26
|
+
```rb
|
27
|
+
{
|
28
|
+
"type" => "object",
|
29
|
+
"properties" => {
|
30
|
+
"first_name" => {
|
31
|
+
"type" => ["string", "null"]
|
32
|
+
},
|
33
|
+
"last_name" => {
|
34
|
+
"type" => ["string", "null"]
|
35
|
+
},
|
36
|
+
"full_name" => {
|
37
|
+
"type" => ["string", "null"],
|
38
|
+
"description" => "The concatendated first and last name."
|
39
|
+
},
|
40
|
+
"email" => {
|
41
|
+
"type" => "string",
|
42
|
+
"format" => "email"
|
43
|
+
}
|
44
|
+
},
|
45
|
+
"required" => ["first_name", "last_name", "full_name", "email"],
|
46
|
+
"additionalProperties" => false
|
47
|
+
}
|
48
|
+
```
|
29
49
|
|
30
|
-
|
31
|
-
end
|
50
|
+
You can pass an ActiveRecord Model to automatically infer types from DB fields:
|
32
51
|
|
52
|
+
```rb
|
33
53
|
class UserSerializer < Blueprinter::Base
|
34
|
-
|
54
|
+
field :first_name
|
55
|
+
field :last_name
|
56
|
+
field :email
|
57
|
+
end
|
35
58
|
|
36
|
-
|
37
|
-
association :addresses, blueprint: AddressSerializer
|
59
|
+
class User < ApplicationRecord
|
38
60
|
end
|
39
|
-
```
|
40
61
|
|
41
|
-
|
42
|
-
```rb
|
43
|
-
BlueprinterSchema.generate(UserSerializer, User)
|
62
|
+
BlueprinterSchema.generate(serializer: UserSerializer, model: User)
|
44
63
|
```
|
45
64
|
|
46
65
|
```rb
|
@@ -48,37 +67,74 @@ BlueprinterSchema.generate(UserSerializer, User)
|
|
48
67
|
"type" => "object",
|
49
68
|
"title" => "User",
|
50
69
|
"properties" => {
|
51
|
-
"
|
52
|
-
"type" => "
|
70
|
+
"first_name" => {
|
71
|
+
"type" => ["string", "null"]
|
53
72
|
},
|
54
|
-
"
|
55
|
-
"type" => "string", "
|
73
|
+
"last_name" => {
|
74
|
+
"type" => ["string", "null"]
|
56
75
|
},
|
57
76
|
"email" => {
|
58
77
|
"type" => "string"
|
59
|
-
}
|
60
|
-
|
61
|
-
|
78
|
+
}
|
79
|
+
},
|
80
|
+
"required" => ["first_name", "last_name", "email"],
|
81
|
+
"additionalProperties" => false
|
82
|
+
}
|
83
|
+
```
|
84
|
+
|
85
|
+
You can use associations:
|
86
|
+
|
87
|
+
```rb
|
88
|
+
class UserSerializer < Blueprinter::Base
|
89
|
+
field :email, type: :string
|
90
|
+
|
91
|
+
association :addresses, blueprint: AddressSerializer, collection: true
|
92
|
+
association :profile, blueprint: ProfileSerializer
|
93
|
+
end
|
94
|
+
|
95
|
+
class AddressSerializer < Blueprinter::Base
|
96
|
+
field :address, type: :string
|
97
|
+
end
|
98
|
+
|
99
|
+
class ProfileSerializer < Blueprinter::Base
|
100
|
+
field :public, type: :boolean
|
101
|
+
end
|
102
|
+
|
103
|
+
BlueprinterSchema.generate(serializer: UserSerializer)
|
104
|
+
```
|
105
|
+
|
106
|
+
```rb
|
107
|
+
{
|
108
|
+
"type" => "object",
|
109
|
+
"properties" => {
|
110
|
+
"email" => {
|
111
|
+
"type" => "string"
|
62
112
|
},
|
63
113
|
"addresses" => {
|
64
114
|
"type" => "array",
|
65
115
|
"items" => {
|
66
116
|
"type" => "object",
|
67
|
-
"title" => "Address",
|
68
117
|
"properties" => {
|
69
|
-
"id" => {
|
70
|
-
"type" => "integer"
|
71
|
-
},
|
72
118
|
"address" => {
|
73
119
|
"type" => "string"
|
74
120
|
}
|
75
121
|
},
|
76
|
-
"required" => ["
|
122
|
+
"required" => ["address"],
|
77
123
|
"additionalProperties" => false
|
78
124
|
}
|
125
|
+
},
|
126
|
+
"profile" => {
|
127
|
+
"type" => "object",
|
128
|
+
"properties" => {
|
129
|
+
"public" => {
|
130
|
+
"type" => "boolean"
|
131
|
+
}
|
132
|
+
},
|
133
|
+
"required" => ["public"],
|
134
|
+
"additionalProperties" => false
|
79
135
|
}
|
80
136
|
},
|
81
|
-
"required" => ["
|
137
|
+
"required" => ["email", "addresses", "profile"],
|
82
138
|
"additionalProperties" => false
|
83
139
|
}
|
84
140
|
```
|
@@ -87,19 +143,30 @@ BlueprinterSchema.generate(UserSerializer, User)
|
|
87
143
|
|
88
144
|
```rb
|
89
145
|
BlueprinterSchema.generate(
|
90
|
-
serializer
|
91
|
-
model,
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
view: :default # The Blueprint view to use
|
96
|
-
}
|
146
|
+
serializer:,
|
147
|
+
model: nil,
|
148
|
+
include_conditional_fields: true, # Whether or not to include conditional fields from the serializer
|
149
|
+
fallback_type: {}, # Type when no DB column or type definition is found. E.g. { 'type' => 'object' }
|
150
|
+
view: :default # The blueprint view to use
|
97
151
|
)
|
98
152
|
```
|
99
153
|
|
100
154
|
## Development
|
101
155
|
|
102
|
-
|
156
|
+
Devcontainer / Codespaces / Native
|
157
|
+
|
158
|
+
```sh
|
159
|
+
bin/setup
|
160
|
+
```
|
161
|
+
|
162
|
+
Docker
|
163
|
+
|
164
|
+
```sh
|
165
|
+
docker compose up -d
|
166
|
+
docker compose exec ruby bin/setup
|
167
|
+
```
|
168
|
+
|
169
|
+
Then, run `bundle exec rspec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
103
170
|
|
104
171
|
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
105
172
|
|
data/Rakefile
CHANGED
@@ -1,31 +1,39 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module BlueprinterSchema
|
4
|
+
class InvalidJsonSchemaType < StandardError; end
|
5
|
+
|
6
|
+
# rubocop:disable Metrics/ClassLength
|
4
7
|
class Generator
|
5
|
-
def initialize(serializer
|
8
|
+
def initialize(serializer:, model:, skip_conditional_fields:, fallback_definition:, view:)
|
6
9
|
@serializer = serializer
|
7
10
|
@model = model
|
8
|
-
@
|
11
|
+
@skip_conditional_fields = skip_conditional_fields
|
12
|
+
@fallback_definition = fallback_definition
|
13
|
+
@view = view
|
9
14
|
end
|
10
15
|
|
11
16
|
def generate
|
12
|
-
{
|
17
|
+
schema = {
|
13
18
|
'type' => 'object',
|
14
|
-
'title' => @model.name,
|
15
19
|
'properties' => build_properties,
|
16
20
|
'required' => build_required_fields,
|
17
21
|
'additionalProperties' => false
|
18
22
|
}
|
23
|
+
|
24
|
+
schema['title'] = @model.name if @model
|
25
|
+
|
26
|
+
schema
|
19
27
|
end
|
20
28
|
|
21
29
|
private
|
22
30
|
|
23
31
|
def fields
|
24
|
-
@fields ||= @serializer.reflections[@
|
32
|
+
@fields ||= @serializer.reflections[@view].fields
|
25
33
|
end
|
26
34
|
|
27
35
|
def associations
|
28
|
-
@associations ||= @serializer.reflections[@
|
36
|
+
@associations ||= @serializer.reflections[@view].associations
|
29
37
|
end
|
30
38
|
|
31
39
|
def build_properties
|
@@ -45,18 +53,41 @@ module BlueprinterSchema
|
|
45
53
|
end
|
46
54
|
|
47
55
|
def skip_field?(field)
|
48
|
-
|
49
|
-
(field.options[:if] || field.options[:unless] || field.options[:exclude_if_nil])
|
56
|
+
@skip_conditional_fields && (field.options[:if] || field.options[:unless])
|
50
57
|
end
|
51
58
|
|
52
59
|
def build_required_fields
|
53
|
-
fields
|
60
|
+
fields
|
61
|
+
.reject { |_, field| field.options[:exclude_if_nil] }
|
62
|
+
.reject { |_, field| skip_field?(field) }
|
63
|
+
.keys.map(&:to_s)
|
54
64
|
end
|
55
65
|
|
66
|
+
# rubocop:disable Metrics/AbcSize
|
56
67
|
def field_to_json_schema(field)
|
57
|
-
|
68
|
+
type_definition = @fallback_definition.dup
|
69
|
+
|
70
|
+
if field.options[:type]
|
71
|
+
type_definition['type'] = ensure_valid_json_schema_types!(field)
|
72
|
+
elsif @model
|
73
|
+
column = @model.columns_hash[field.name.to_s]
|
74
|
+
type_definition = ar_column_to_json_schema(column)
|
75
|
+
end
|
76
|
+
|
77
|
+
type_definition['format'] = field.options[:format] if field.options[:format]
|
78
|
+
type_definition['description'] = field.options[:description] if field.options[:description]
|
79
|
+
type_definition
|
80
|
+
end
|
81
|
+
# rubocop:enable Metrics/AbcSize
|
82
|
+
|
83
|
+
def ensure_valid_json_schema_types!(field)
|
84
|
+
types = [field.options[:type]].flatten.map(&:to_s)
|
85
|
+
|
86
|
+
return field.options[:type] if types.all? do |type|
|
87
|
+
%w[string integer number boolean object array null].include?(type)
|
88
|
+
end
|
58
89
|
|
59
|
-
|
90
|
+
raise BlueprinterSchema::InvalidJsonSchemaType, "Invalid type: #{field.options[:type]}"
|
60
91
|
end
|
61
92
|
|
62
93
|
# rubocop:disable Metrics/MethodLength
|
@@ -93,13 +124,23 @@ module BlueprinterSchema
|
|
93
124
|
|
94
125
|
return { 'type' => 'object' } unless blueprint_class
|
95
126
|
|
96
|
-
ar_association = @model
|
97
|
-
is_collection = ar_association.collection?
|
98
|
-
association_model = ar_association.klass
|
127
|
+
ar_association = @model&.reflect_on_association(association.name)
|
128
|
+
is_collection = ar_association ? ar_association.collection? : association.options[:collection]
|
99
129
|
|
100
|
-
associated_schema =
|
130
|
+
associated_schema = recursive_generate(blueprint_class, ar_association&.klass)
|
101
131
|
|
102
132
|
is_collection ? { 'type' => 'array', 'items' => associated_schema } : associated_schema
|
103
133
|
end
|
134
|
+
|
135
|
+
def recursive_generate(serializer, model)
|
136
|
+
BlueprinterSchema.generate(
|
137
|
+
serializer:,
|
138
|
+
model:,
|
139
|
+
skip_conditional_fields: @skip_conditional_fields,
|
140
|
+
fallback_definition: @fallback_definition,
|
141
|
+
view: @view
|
142
|
+
)
|
143
|
+
end
|
104
144
|
end
|
145
|
+
# rubocop:enable Metrics/ClassLength
|
105
146
|
end
|
data/lib/blueprinter_schema.rb
CHANGED
@@ -4,7 +4,13 @@ require_relative 'blueprinter_schema/version'
|
|
4
4
|
require_relative 'blueprinter_schema/generator'
|
5
5
|
|
6
6
|
module BlueprinterSchema
|
7
|
-
def self.generate(
|
8
|
-
|
7
|
+
def self.generate(
|
8
|
+
serializer:,
|
9
|
+
model: nil,
|
10
|
+
skip_conditional_fields: false,
|
11
|
+
fallback_definition: {},
|
12
|
+
view: :default
|
13
|
+
)
|
14
|
+
Generator.new(serializer:, model:, skip_conditional_fields:, fallback_definition:, view:).generate
|
9
15
|
end
|
10
16
|
end
|
metadata
CHANGED
@@ -1,14 +1,42 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: blueprinter_schema
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- thisismydesign
|
8
8
|
bindir: exe
|
9
9
|
cert_chain: []
|
10
10
|
date: 1980-01-02 00:00:00.000000000 Z
|
11
|
-
dependencies:
|
11
|
+
dependencies:
|
12
|
+
- !ruby/object:Gem::Dependency
|
13
|
+
name: activerecord
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
15
|
+
requirements:
|
16
|
+
- - ">="
|
17
|
+
- !ruby/object:Gem::Version
|
18
|
+
version: '0'
|
19
|
+
type: :runtime
|
20
|
+
prerelease: false
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
22
|
+
requirements:
|
23
|
+
- - ">="
|
24
|
+
- !ruby/object:Gem::Version
|
25
|
+
version: '0'
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: blueprinter
|
28
|
+
requirement: !ruby/object:Gem::Requirement
|
29
|
+
requirements:
|
30
|
+
- - ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :runtime
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - ">="
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '0'
|
12
40
|
email:
|
13
41
|
- git.thisismydesign@gmail.com
|
14
42
|
executables: []
|