dry-swagger 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 0ea8396c1d7976844254a7e7564a74a0cdc0391c3bf42a4aa49fc479fd99e11e
4
+ data.tar.gz: 611a49427c0c0ece4bd4ec1e134afd8bcebc01f43b0803eecabdd9cf16df4d14
5
+ SHA512:
6
+ metadata.gz: 7d1e8404d230897c6f4e88fffbd1870ee1fbf305b725d8d9067f5529c3f2bcdda71990b364372478d3ff9f780e6a997144c557f57f574768d705caea10255923
7
+ data.tar.gz: 0d7b5f769a536d9fd18b594cf097c5c9922b92f74bdf009ff2dd90ea099e90c5bc9d36aa684efcdd49a1176f299d6db2574a786a8a98213a5301a15bfdb223bf
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+ .idea/
data/.rakeTasks ADDED
@@ -0,0 +1,7 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <Settings><!--This file was automatically generated by Ruby plugin.
3
+ You are allowed to:
4
+ 1. Remove rake task
5
+ 2. Add existing rake tasks
6
+ To add existing rake tasks automatically delete this file and reload the project.
7
+ --><RakeGroup description="" fullCmd="" taksId="rake"><RakeTask description="Build dry-swagger-0.1.0.gem into the pkg directory" fullCmd="build" taksId="build" /><RakeTask description="Remove any temporary products" fullCmd="clean" taksId="clean" /><RakeTask description="Remove any generated files" fullCmd="clobber" taksId="clobber" /><RakeTask description="Build and install dry-swagger-0.1.0.gem into system gems" fullCmd="install" taksId="install" /><RakeGroup description="" fullCmd="" taksId="install"><RakeTask description="Build and install dry-swagger-0.1.0.gem into system gems without network access" fullCmd="install:local" taksId="local" /></RakeGroup><RakeTask description="Create tag v0.1.0 and build and push dry-swagger-0.1.0.gem to TODO: Set to 'http://mygemserver.com'" fullCmd="release[remote]" taksId="release[remote]" /><RakeTask description="Run RSpec code examples" fullCmd="spec" taksId="spec" /><RakeTask description="" fullCmd="default" taksId="default" /><RakeTask description="" fullCmd="release" taksId="release" /><RakeGroup description="" fullCmd="" taksId="release"><RakeTask description="" fullCmd="release:guard_clean" taksId="guard_clean" /><RakeTask description="" fullCmd="release:rubygem_push" taksId="rubygem_push" /><RakeTask description="" fullCmd="release:source_control_push" taksId="source_control_push" /></RakeGroup></RakeGroup></Settings>
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.7.1
6
+ before_install: gem install bundler -v 2.1.4
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at jane.terziev@webfactory.mk. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [https://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: https://contributor-covenant.org
74
+ [version]: https://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in dry-swagger.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
7
+ gem "rspec", "~> 3.0"
8
+ gem "dry-validation"
9
+ gem "dry-struct"
data/Gemfile.lock ADDED
@@ -0,0 +1,76 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ dry-swagger (0.3.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ concurrent-ruby (1.1.9)
10
+ diff-lcs (1.4.4)
11
+ dry-configurable (0.12.1)
12
+ concurrent-ruby (~> 1.0)
13
+ dry-core (~> 0.5, >= 0.5.0)
14
+ dry-container (0.8.0)
15
+ concurrent-ruby (~> 1.0)
16
+ dry-configurable (~> 0.1, >= 0.1.3)
17
+ dry-core (0.7.1)
18
+ concurrent-ruby (~> 1.0)
19
+ dry-equalizer (0.3.0)
20
+ dry-inflector (0.2.1)
21
+ dry-initializer (3.0.4)
22
+ dry-logic (1.2.0)
23
+ concurrent-ruby (~> 1.0)
24
+ dry-core (~> 0.5, >= 0.5)
25
+ dry-schema (1.7.0)
26
+ concurrent-ruby (~> 1.0)
27
+ dry-configurable (~> 0.8, >= 0.8.3)
28
+ dry-core (~> 0.5, >= 0.5)
29
+ dry-initializer (~> 3.0)
30
+ dry-logic (~> 1.0)
31
+ dry-types (~> 1.5)
32
+ dry-struct (1.4.0)
33
+ dry-core (~> 0.5, >= 0.5)
34
+ dry-types (~> 1.5)
35
+ ice_nine (~> 0.11)
36
+ dry-types (1.5.1)
37
+ concurrent-ruby (~> 1.0)
38
+ dry-container (~> 0.3)
39
+ dry-core (~> 0.5, >= 0.5)
40
+ dry-inflector (~> 0.1, >= 0.1.2)
41
+ dry-logic (~> 1.0, >= 1.0.2)
42
+ dry-validation (1.6.0)
43
+ concurrent-ruby (~> 1.0)
44
+ dry-container (~> 0.7, >= 0.7.1)
45
+ dry-core (~> 0.4)
46
+ dry-equalizer (~> 0.2)
47
+ dry-initializer (~> 3.0)
48
+ dry-schema (~> 1.5, >= 1.5.2)
49
+ ice_nine (0.11.2)
50
+ rake (12.3.3)
51
+ rspec (3.10.0)
52
+ rspec-core (~> 3.10.0)
53
+ rspec-expectations (~> 3.10.0)
54
+ rspec-mocks (~> 3.10.0)
55
+ rspec-core (3.10.1)
56
+ rspec-support (~> 3.10.0)
57
+ rspec-expectations (3.10.1)
58
+ diff-lcs (>= 1.2.0, < 2.0)
59
+ rspec-support (~> 3.10.0)
60
+ rspec-mocks (3.10.2)
61
+ diff-lcs (>= 1.2.0, < 2.0)
62
+ rspec-support (~> 3.10.0)
63
+ rspec-support (3.10.2)
64
+
65
+ PLATFORMS
66
+ ruby
67
+
68
+ DEPENDENCIES
69
+ dry-struct
70
+ dry-swagger!
71
+ dry-validation
72
+ rake (~> 12.0)
73
+ rspec (~> 3.0)
74
+
75
+ BUNDLED WITH
76
+ 2.1.4
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 wf-janeterziev
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,294 @@
1
+ # Dry::Swagger
2
+
3
+ Generate a valid and up to date swagger documentation out of your dry-structs and dry-validations
4
+
5
+ The gem is still work in progress and is not yet fully tested.
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'dry-swagger'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ bundle install
17
+
18
+ Or install it yourself as:
19
+
20
+ gem install dry-swagger
21
+
22
+ ## Usage
23
+ ## Dry::Validation::Contract
24
+ class Contract < Dry::Validation::Contract
25
+ json do
26
+ required(:required_field).value(:str?)
27
+ required(:required_nullable_field).maybe(:str?)
28
+ required(:required_filled_field).filled(:str?)
29
+
30
+ optional(:optional_field).value(:str?)
31
+ optional(:optional_nullable_field).maybe(:str?)
32
+ optional(:optional_filled_field).filled(:str?)
33
+ end
34
+ end
35
+
36
+ Dry::Swagger::ContractParser.new.call(Contract)
37
+ => {
38
+ "type": "object",
39
+ "properties": {
40
+ "required_field": {
41
+ "type": "string",
42
+ "nullable": false
43
+ },
44
+ "required_nullable_field": {
45
+ "type": "string",
46
+ "nullable": true
47
+ },
48
+ "required_filled_field": {
49
+ "type": "string",
50
+ "nullable": false
51
+ },
52
+ "optional_field": {
53
+ "type": "string",
54
+ "nullable": false
55
+ },
56
+ "optional_nullable_field": {
57
+ "type": "string",
58
+ "nullable": true
59
+ },
60
+ "optional_filled_field": {
61
+ "type": "string",
62
+ "nullable": false
63
+ }
64
+ },
65
+ "required": [
66
+ "required_field",
67
+ "required_nullable_field",
68
+ "required_filled_field"
69
+ ]
70
+ }
71
+ #### With nested fields - hash or array
72
+ class Contract < Dry::Validation::Contract
73
+ json do
74
+ required(:array_field).array(:int?)
75
+ required(:array_of_hash).array(:hash) do
76
+ required(:field).value(:str?)
77
+ end
78
+ required(:hash_field).hash do
79
+ required(:field).value(:str?)
80
+ end
81
+ end
82
+ end
83
+
84
+ Dry::Swagger::ContractParser.new.call(Contract)
85
+ => {
86
+ "type": "object",
87
+ "properties": {
88
+ "array_field": {
89
+ "type": "array",
90
+ "items": {
91
+ "type": "integer"
92
+ }
93
+ },
94
+ "array_of_hash": {
95
+ "type": "array",
96
+ "items": {
97
+ "type": "object",
98
+ "properties": {
99
+ "field": {
100
+ "type": "string",
101
+ "x-nullable": false
102
+ }
103
+ },
104
+ "required": [
105
+ "field"
106
+ ]
107
+ }
108
+ },
109
+ "hash_field": {
110
+ "type": "object",
111
+ "properties": {
112
+ "field": {
113
+ "type": "string",
114
+ "x-nullable": false
115
+ }
116
+ },
117
+ "required": [
118
+ "field"
119
+ ]
120
+ }
121
+ },
122
+ "required": [
123
+ "array_field",
124
+ "array_of_hash",
125
+ "hash_field"
126
+ ]
127
+ }
128
+
129
+ ### With Dry::Struct
130
+ class DTO < Dry::Struct
131
+ attribute :required_string, Types::String
132
+ attribute :required_nullable_string, Types::String.optional
133
+ attribute :required_string_with_enum, Types::String.enum('enum1')
134
+ attribute? :optional_string, Types::String
135
+ attribute? :optional_nullable_string, Types::String.optional
136
+ end
137
+
138
+ Dry::Swagger::StructParser.new.call(DTO)
139
+ => {
140
+ "type": "object",
141
+ "properties": {
142
+ "required_string": {
143
+ "type": "string",
144
+ "x-nullable": false
145
+ },
146
+ "required_nullable_string": {
147
+ "type": "string",
148
+ "x-nullable": true
149
+ },
150
+ "required_string_with_enum": {
151
+ "type": "string",
152
+ "enum": [
153
+ "enum1"
154
+ ],
155
+ "x-nullable": false
156
+ },
157
+ "optional_string": {
158
+ "type": "string",
159
+ "x-nullable": false
160
+ },
161
+ "optional_nullable_string": {
162
+ "type": "string",
163
+ "x-nullable": true
164
+ }
165
+ },
166
+ "required": [
167
+ "required_string",
168
+ "required_nullable_string",
169
+ "required_string_with_enum"
170
+ ]
171
+ }
172
+ #### With nested fields
173
+ {
174
+ "type": "object",
175
+ "properties": {
176
+ "array_of_integer": {
177
+ "type": "array",
178
+ "items": {
179
+ "type": "integer"
180
+ },
181
+ "x-nullable": false
182
+ },
183
+ "array_of_objects": {
184
+ "type": "array",
185
+ "items": {
186
+ "type": "object",
187
+ "properties": {
188
+ "required_string": {
189
+ "type": "string",
190
+ "x-nullable": false
191
+ },
192
+ "required_nullable_string": {
193
+ "type": "string",
194
+ "x-nullable": true
195
+ },
196
+ "required_string_with_enum": {
197
+ "type": "string",
198
+ "enum": [
199
+ "enum1"
200
+ ],
201
+ "x-nullable": false
202
+ },
203
+ "optional_string": {
204
+ "type": "string",
205
+ "x-nullable": false
206
+ },
207
+ "optional_nullable_string": {
208
+ "type": "string",
209
+ "x-nullable": true
210
+ }
211
+ },
212
+ "required": [
213
+ "required_string",
214
+ "required_nullable_string",
215
+ "required_string_with_enum"
216
+ ]
217
+ },
218
+ "x-nullable": false
219
+ },
220
+ "dto": {
221
+ "type": "object",
222
+ "properties": {
223
+ "required_string": {
224
+ "type": "string",
225
+ "x-nullable": false
226
+ },
227
+ "required_nullable_string": {
228
+ "type": "string",
229
+ "x-nullable": true
230
+ },
231
+ "required_string_with_enum": {
232
+ "type": "string",
233
+ "enum": [
234
+ "enum1"
235
+ ],
236
+ "x-nullable": false
237
+ },
238
+ "optional_string": {
239
+ "type": "string",
240
+ "x-nullable": false
241
+ },
242
+ "optional_nullable_string": {
243
+ "type": "string",
244
+ "x-nullable": true
245
+ }
246
+ },
247
+ "required": [
248
+ "required_string",
249
+ "required_nullable_string",
250
+ "required_string_with_enum"
251
+ ],
252
+ "x-nullable": false
253
+ }
254
+ },
255
+ "required": [
256
+ "array_of_integer",
257
+ "array_of_objects",
258
+ "dto"
259
+ ]
260
+ }
261
+ ## Custom Configuration For Your Project
262
+ You can override default configurations by creating a file in config/initializers/dry-swagger.rb and changing the following values.
263
+
264
+ Dry::Swagger.configuration do |config|
265
+ config.struct_enable_required_validation = true / false
266
+ config.struct_enable_nullable_validation = true / false
267
+ config.struct_enable_enums = true / false
268
+ config.struct_enable_descriptions = true / false
269
+
270
+ config.contract_enable_required_validation = true / false
271
+ config.contract_enable_nullable_validation = true / false
272
+ config.contract_enable_enums = true / false
273
+ config.contract_enable_descriptions = true / false
274
+ config.nullable_type = :"x-nullable" / :nullable
275
+ end
276
+ By default, all these settings are true, and nullable_type is :"x-nullable".
277
+ ## Development
278
+
279
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
280
+
281
+ 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 tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
282
+
283
+ ## Contributing
284
+
285
+ Bug reports and pull requests are welcome on GitHub at https://github.com/Jane-Terziev/dry-swagger. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/dry-swagger/blob/master/CODE_OF_CONDUCT.md).
286
+
287
+
288
+ ## License
289
+
290
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
291
+
292
+ ## Code of Conduct
293
+
294
+ Everyone interacting in the Dry::Swagger project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/dry-swagger/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "dry/swagger"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,29 @@
1
+ require_relative 'lib/dry/swagger/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "dry-swagger"
5
+ spec.version = Dry::Swagger::VERSION
6
+ spec.authors = ["Jane-Terziev"]
7
+ spec.email = ["janeterziev@gmail.com"]
8
+
9
+ spec.summary = %q{Generate dry-validation and dry-struct into valid swagger documentation}
10
+ spec.description = %q{A parser which converts dry-validation or dry-struct into valid swagger documentation}
11
+ spec.homepage = "https://github.com/Jane-Terziev/dry-swagger"
12
+ spec.license = "MIT"
13
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
14
+
15
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
16
+
17
+ spec.metadata["homepage_uri"] = spec.homepage
18
+ spec.metadata["source_code_uri"] = "https://github.com/Jane-Terziev/dry-swagger"
19
+ spec.metadata["changelog_uri"] = "https://github.com/Jane-Terziev/dry-swagger"
20
+
21
+ # Specify which files should be added to the gem when it is released.
22
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
24
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
25
+ end
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+ end
@@ -0,0 +1,24 @@
1
+ require "dry/swagger/version"
2
+ require "dry/swagger/contract_parser"
3
+ require "dry/swagger/struct_parser"
4
+ require 'helpers/configuration'
5
+
6
+ module Dry
7
+ module Swagger
8
+ class Error < StandardError; end
9
+
10
+ extend Configuration
11
+
12
+ define_setting :struct_enable_required_validation, true
13
+ define_setting :struct_enable_nullable_validation, true
14
+ define_setting :struct_enable_enums, true
15
+ define_setting :struct_enable_descriptions, true
16
+
17
+ define_setting :contract_enable_required_validation, true
18
+ define_setting :contract_enable_nullable_validation, true
19
+ define_setting :contract_enable_enums, true
20
+ define_setting :contract_enable_descriptions, true
21
+
22
+ define_setting :nullable_type, :"x-nullable"
23
+ end
24
+ end
@@ -0,0 +1,173 @@
1
+ module Dry
2
+ module Swagger
3
+ class ContractParser
4
+ PREDICATE_TO_TYPE = {
5
+ array?: 'array',
6
+ bool?: 'boolean',
7
+ date?: 'date',
8
+ date_time?: 'datetime',
9
+ decimal?: 'float',
10
+ float?: 'float',
11
+ hash?: 'hash',
12
+ int?: 'integer',
13
+ nil?: 'nil',
14
+ str?: 'string',
15
+ time?: 'time'
16
+ }.freeze
17
+
18
+ SWAGGER_FIELD_TYPE_DEFINITIONS = {
19
+ "string" => { type: :string },
20
+ "integer" => { type: :integer },
21
+ "boolean" => { type: :boolean },
22
+ "float" => { type: :float },
23
+ "datetime" => { type: :string, format: :datetime },
24
+ "date" => { type: :string, format: :date },
25
+ "time" => { type: :string, format: :time },
26
+ }.freeze
27
+
28
+ # @api private
29
+ attr_reader :keys
30
+
31
+ # @api private
32
+ def initialize
33
+ @keys = {}
34
+ end
35
+
36
+ # @api private
37
+ def to_h
38
+ { keys: keys }
39
+ end
40
+
41
+ # @api private
42
+ def call(contract, &block)
43
+ @keys = {}
44
+ visit(contract.schema.to_ast)
45
+ instance_eval(&block) if block_given?
46
+ to_swagger
47
+ end
48
+
49
+ # @api private
50
+ def visit(node, opts = {})
51
+ meth, rest = node
52
+ public_send(:"visit_#{meth}", rest, opts)
53
+ end
54
+
55
+ # @api private
56
+ def visit_set(node, opts = {})
57
+ target = (key = opts[:key]) ? self.class.new : self
58
+
59
+ node.map { |child| target.visit(child, opts) }
60
+
61
+ return unless key
62
+
63
+ target_info = opts[:member] ? target.to_h : target.to_h
64
+ type = keys[key][:array] ? 'array' : 'hash'
65
+
66
+ keys.update(key => { **keys[key], type: type, **target_info })
67
+ end
68
+
69
+ # @api private
70
+ def visit_and(node, opts = {})
71
+ left, right = node
72
+
73
+ visit(left, opts)
74
+ visit(right, opts)
75
+ end
76
+
77
+ def visit_not(_node, opts = {})
78
+ key = opts[:key]
79
+ keys[key][::Dry::Swagger.nullable_type] = true if ::Dry::Swagger.contract_enable_nullable_validation
80
+ end
81
+
82
+ # @api private
83
+ def visit_implication(node, opts = {})
84
+ node.each do |el|
85
+ opts = opts.merge(required: false) if ::Dry::Swagger.contract_enable_required_validation
86
+ visit(el, opts)
87
+ end
88
+ end
89
+
90
+ # @api private
91
+ def visit_each(node, opts = {})
92
+ visit(node, opts.merge(member: true))
93
+ end
94
+
95
+ # @api private
96
+ def visit_key(node, opts = {})
97
+ name, rest = node
98
+ opts = opts.merge(key: name)
99
+ opts = opts.merge(required: true) if ::Dry::Swagger.contract_enable_required_validation
100
+ visit(rest, opts)
101
+ end
102
+
103
+ # @api private
104
+ def visit_predicate(node, opts = {})
105
+ name, rest = node
106
+
107
+ key = opts[:key]
108
+
109
+ if name.equal?(:key?)
110
+ keys[rest[0][1]] = { required: opts.fetch(:required, true) } if ::Dry::Swagger.contract_enable_required_validation
111
+ elsif name.equal?(:array?)
112
+ keys[key][:array] = true
113
+ elsif name.equal?(:included_in?)
114
+ keys[key][:enum] = rest[0][1]
115
+ elsif PREDICATE_TO_TYPE[name]
116
+ keys[key][:type] = PREDICATE_TO_TYPE[name]
117
+ else
118
+ description = predicate_description(name.to_s, rest[0][1].to_s)
119
+ if keys[key][:description].to_s.empty?
120
+ keys[key][:description] = description unless description.to_s.empty?
121
+ else
122
+ keys[key][:description] += ", #{description}" unless description.to_s.empty?
123
+ end
124
+ end
125
+ end
126
+
127
+ def predicate_description(name, validation)
128
+ case name
129
+ when 'eql?' then "Must be equal to #{validation}"
130
+ when 'max_size?' then "Maximum size: #{validation}"
131
+ when 'min_size?' then "Minimum size: #{validation}"
132
+ when 'gteq?' then "Greater or equal #{validation}"
133
+ when 'gt?' then "Greater than #{validation}"
134
+ when 'lt?' then "Lower than #{validation}"
135
+ when 'lteq?' then "Lower than or equal to #{validation}"
136
+ else
137
+ ''
138
+ end
139
+ end
140
+
141
+ def to_swagger
142
+ generate_documentation(keys)
143
+ end
144
+
145
+ private
146
+
147
+ def generate_documentation(fields)
148
+ documentation = { properties: {}, required: [] }
149
+ fields.each do |field_name, attributes_hash|
150
+ documentation[:properties][field_name] = generate_field_properties(attributes_hash)
151
+ documentation[:required] << field_name if ::Dry::Swagger.contract_enable_required_validation && attributes_hash[:required]
152
+ end
153
+ { :type => :object, :properties => documentation[:properties], :required => documentation[:required] }
154
+ end
155
+
156
+ def generate_field_properties(attributes_hash)
157
+ if attributes_hash[:type] == 'array'
158
+ { type: :array, items: generate_documentation(attributes_hash[:keys]) }
159
+ elsif attributes_hash[:array] && attributes_hash[:type] != 'array'
160
+ { type: :array, items: SWAGGER_FIELD_TYPE_DEFINITIONS.fetch(attributes_hash[:type]) }
161
+ elsif attributes_hash[:type] == 'hash'
162
+ generate_documentation(attributes_hash[:keys])
163
+ else
164
+ field = SWAGGER_FIELD_TYPE_DEFINITIONS.fetch(attributes_hash[:type])
165
+ field = field.merge(::Dry::Swagger.nullable_type => attributes_hash[::Dry::Swagger.nullable_type] | false) if ::Dry::Swagger.contract_enable_nullable_validation
166
+ field = field.merge(enum: attributes_hash[:enum]) if attributes_hash[:enum] if ::Dry::Swagger.contract_enable_enums
167
+ field = field.merge(description: attributes_hash[:description]) if attributes_hash[:description] if ::Dry::Swagger.contract_enable_descriptions
168
+ field
169
+ end
170
+ end
171
+ end
172
+ end
173
+ end
@@ -0,0 +1,82 @@
1
+ module Dry
2
+ module Swagger
3
+ class StructParser
4
+ SWAGGER_FIELD_TYPE_DEFINITIONS = {
5
+ "String" => { type: :string },
6
+ "Integer" => { type: :integer },
7
+ "TrueClass | FalseClass" => { type: :boolean },
8
+ "BigDecimal" => { type: :decimal },
9
+ "Float" => { type: :float },
10
+ "DateTime" => { type: :string, format: :datetime },
11
+ "Date" => { type: :string, format: :date },
12
+ "Time" => { type: :string, format: :time }
13
+ }
14
+
15
+ def call(dto)
16
+ documentation = generate_fields_documentation(dto.schema)
17
+ { :type => :object, :properties => documentation[:properties], :required => documentation[:required]}
18
+ end
19
+
20
+ def generate_fields_documentation(dto_schema)
21
+ documentation = { properties: {}, required: [] }
22
+ dto_schema.name_key_map.each do |name, schema_key_object|
23
+ documentation[:properties][name] = schema_key_object.type.optional? ?
24
+ generate_field_properties(schema_key_object.type.right, true) :
25
+ generate_field_properties(schema_key_object.type, false)
26
+
27
+ documentation[:required] << name if ::Dry::Swagger.struct_enable_required_validation && schema_key_object.required?
28
+ end
29
+ documentation
30
+ end
31
+
32
+ def generate_field_properties(type, nullable)
33
+ field_type = type.name
34
+ if SWAGGER_FIELD_TYPE_DEFINITIONS[field_type] # IS PRIMITIVE FIELD?
35
+ definition = SWAGGER_FIELD_TYPE_DEFINITIONS[field_type]
36
+ definition = definition
37
+ if is_enum?(type.class.name) && ::Dry::Swagger.struct_enable_enums
38
+ enums = type.values
39
+ enums += [nil] if nullable
40
+ definition = definition.merge(enum: enums)
41
+ end
42
+ elsif is_array?(field_type)
43
+ definition = { type: :array }
44
+ if is_primitive?(type.type.member.name)
45
+ definition = definition
46
+ definition = definition.merge(items: SWAGGER_FIELD_TYPE_DEFINITIONS[type.type.member.name])
47
+ else
48
+ schema = is_array_with_dynamic_schema?(type.type.member) ? type.type.member.left : type.type.member
49
+ definition = definition.merge(items: call(schema))
50
+ end
51
+ else
52
+ schema = is_dynamic_schema?(type) ? type.left : type
53
+ definition = call(schema)
54
+ end
55
+
56
+ ::Dry::Swagger.struct_enable_nullable_validation ? definition.merge(::Dry::Swagger.nullable_type => nullable) : definition
57
+ end
58
+
59
+ private
60
+
61
+ def is_enum?(class_name)
62
+ class_name == 'Dry::Types::Enum'
63
+ end
64
+
65
+ def is_array?(type_name)
66
+ type_name == 'Array'
67
+ end
68
+
69
+ def is_primitive?(type_name)
70
+ !SWAGGER_FIELD_TYPE_DEFINITIONS[type_name].nil?
71
+ end
72
+
73
+ def is_array_with_dynamic_schema?(member)
74
+ member.respond_to?(:right)
75
+ end
76
+
77
+ def is_dynamic_schema?(type)
78
+ !type.respond_to?(:schema)
79
+ end
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,7 @@
1
+ module Dry
2
+ module Swagger
3
+ module Types
4
+ include Dry.Types()
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,5 @@
1
+ module Dry
2
+ module Swagger
3
+ VERSION = "0.3.0"
4
+ end
5
+ end
@@ -0,0 +1,27 @@
1
+ module Configuration
2
+
3
+ def configuration
4
+ yield self
5
+ end
6
+
7
+ def define_setting(name, default = nil)
8
+ class_variable_set("@@#{name}", default)
9
+
10
+ define_class_method "#{name}=" do |value|
11
+ class_variable_set("@@#{name}", value)
12
+ end
13
+
14
+ define_class_method name do
15
+ class_variable_get("@@#{name}")
16
+ end
17
+ end
18
+
19
+ private
20
+
21
+ def define_class_method(name, &block)
22
+ (class << self; self; end).instance_eval do
23
+ define_method name, &block
24
+ end
25
+ end
26
+
27
+ end
metadata ADDED
@@ -0,0 +1,67 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dry-swagger
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.0
5
+ platform: ruby
6
+ authors:
7
+ - Jane-Terziev
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-07-31 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A parser which converts dry-validation or dry-struct into valid swagger
14
+ documentation
15
+ email:
16
+ - janeterziev@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - ".gitignore"
22
+ - ".rakeTasks"
23
+ - ".rspec"
24
+ - ".travis.yml"
25
+ - CODE_OF_CONDUCT.md
26
+ - Gemfile
27
+ - Gemfile.lock
28
+ - LICENSE.txt
29
+ - README.md
30
+ - Rakefile
31
+ - bin/console
32
+ - bin/setup
33
+ - dry-swagger.gemspec
34
+ - lib/dry/swagger.rb
35
+ - lib/dry/swagger/contract_parser.rb
36
+ - lib/dry/swagger/struct_parser.rb
37
+ - lib/dry/swagger/types.rb
38
+ - lib/dry/swagger/version.rb
39
+ - lib/helpers/configuration.rb
40
+ homepage: https://github.com/Jane-Terziev/dry-swagger
41
+ licenses:
42
+ - MIT
43
+ metadata:
44
+ allowed_push_host: https://rubygems.org
45
+ homepage_uri: https://github.com/Jane-Terziev/dry-swagger
46
+ source_code_uri: https://github.com/Jane-Terziev/dry-swagger
47
+ changelog_uri: https://github.com/Jane-Terziev/dry-swagger
48
+ post_install_message:
49
+ rdoc_options: []
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: 2.3.0
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ requirements: []
63
+ rubygems_version: 3.1.2
64
+ signing_key:
65
+ specification_version: 4
66
+ summary: Generate dry-validation and dry-struct into valid swagger documentation
67
+ test_files: []