rswag-schema_components 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 2df85fd39afcf4bda138ec5e178f8564aed86de64e87539e7a126dd03b411e98
4
+ data.tar.gz: 655a801d17c39ab96f109b105ab6522042d11967a13d3c48b03bc52c166aac90
5
+ SHA512:
6
+ metadata.gz: 8eb05da6c3c02a99d550a4c00971071583bf6787e57e525afc1b8440e776ba8744d95301e3da229310837afa9e4975fb436b2a6964cca10d9bebff170686c3fc
7
+ data.tar.gz: 3e71dd10e8ef6732ee28645f2eb9c57967ca7182b687e46160b7ac835bdd66c7c5d0a2e0a87db53cb12587e3c0fb042324643734f3b62d17b14ca57778db2c2b
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright Marten Klitzke
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,142 @@
1
+ # Rswag::SchemaComponents
2
+
3
+ As an addition to the rswag gem, this gem allows you to define reusable components for your swagger schema. This allows you to define schema components as ruby classes once and reuse and inherit it in multiple places in your schema.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem "rswag-schema_components"
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ ```bash
16
+ $ bundle
17
+ ```
18
+
19
+ Or install it yourself as:
20
+
21
+ ```bash
22
+ $ gem install rswag-schema_components
23
+ ```
24
+
25
+ ### Configuration
26
+
27
+ To use a different components folder just add an initializer called "rswag_schema_components.rb" and paste in the following to add your custom global classes:
28
+
29
+ ```ruby
30
+ # encoding: utf-8
31
+
32
+ Rswag::SchemaComponents.setup do |config|
33
+ config.components_base_path = "api/components"
34
+ end
35
+ ```
36
+
37
+ ## Usage
38
+
39
+ ### Define a component
40
+
41
+ Create a new class in the `components_base_path` (defaults to `app/api_components`) and include the `Rswag::SchemaComponents::Component` module.
42
+
43
+ A basic example of a component:
44
+
45
+ ```ruby
46
+ class ExampleComponent
47
+ include Rswag::SchemaComponents::Component
48
+
49
+ schema({
50
+ type: :object,
51
+ properties: {
52
+ id: {type: :string, format: :uuid},
53
+ name: {type: :string},
54
+ description: {type: :string},
55
+ createdAt: {type: :string, format: "date-time"},
56
+ updatedAt: {type: :string, format: "date-time"}
57
+ },
58
+ additionalProperties: false,
59
+ required: %w[
60
+ id name createdAt updatedAt
61
+ ]
62
+ })
63
+ end
64
+ ```
65
+
66
+ ### Use all Components in your rswag schema
67
+
68
+ Inside your `swagger_helper.rb` file:
69
+
70
+ ```ruby
71
+ ...
72
+ RSpec.configure do |config|
73
+ components_loader = Rswag::SchemaComponents::Loader.new
74
+
75
+ servers = [
76
+ {
77
+ url: "http://localhost:3000",
78
+ description: "Dev Server"
79
+ },
80
+ ]
81
+
82
+ config.openapi_specs = {
83
+ "schema.yaml" => {
84
+ openapi: "3.0.1",
85
+ info: {
86
+ title: "Example API",
87
+ version: "v1",
88
+ },
89
+ servers:,
90
+ components: {
91
+ schemas: components_loader.schemas,
92
+ ...
93
+ }.compact
94
+ ...
95
+ },
96
+ }
97
+ end
98
+ ```
99
+
100
+ Since the `Rswag::SchemaComponents::Loader` returns a simple Ruby hash you can just merge it into another schema. With this you are able to create a shared schema which includes all shared components.
101
+
102
+ ```ruby
103
+ ...
104
+ RSpec.configure do |config|
105
+ public_components = Rswag::SchemaComponents::Loader.new("public")
106
+ admin_components = Rswag::SchemaComponents::Loader.new("admin")
107
+ shared_components = Rswag::SchemaComponents::Loader.new("shared")
108
+
109
+ config.openapi_specs = {
110
+ "public-schema.yaml" => {
111
+ openapi: "3.0.1",
112
+ info: {
113
+ title: "Example Public API",
114
+ version: "v1",
115
+ },
116
+ servers:,
117
+ components: {
118
+ schemas: shared_components.schemas.merge(public_components.schemas)
119
+ ...
120
+ }.compact
121
+ ...
122
+ },
123
+ "admin-schema.yaml" => {
124
+ openapi: "3.0.1",
125
+ info: {
126
+ title: "Example Admin API",
127
+ version: "v1",
128
+ },
129
+ servers:,
130
+ components: {
131
+ schemas: shared_components.schemas.merge(admin_components.schemas)
132
+ ...
133
+ }.compact
134
+ ...
135
+ }
136
+ }
137
+ end
138
+ ```
139
+
140
+ ## License
141
+
142
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,14 @@
1
+ require "bundler/setup"
2
+
3
+ require "bundler/gem_tasks"
4
+
5
+ require "rake/testtask"
6
+
7
+ Rake::TestTask.new(:test) do |t|
8
+ t.libs << "lib"
9
+ t.libs << "test"
10
+ t.pattern = "test/**/*_test.rb"
11
+ t.verbose = false
12
+ end
13
+
14
+ task default: :test
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rswag
4
+ module SchemaComponents
5
+ module Component
6
+ extend ActiveSupport::Concern
7
+
8
+ included do
9
+ class << self
10
+ attr_accessor :schema_value
11
+
12
+ def schema(schema_definition)
13
+ self.schema_value = if superclass.respond_to?(:schema_value)
14
+ parent_schema_definition = superclass.schema_value.deep_dup
15
+ parent_schema_definition.deeper_merge(schema_definition)
16
+ else
17
+ schema_definition
18
+ end
19
+ end
20
+ end
21
+
22
+ def to_schema
23
+ self.class.schema_value
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,76 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rswag
4
+ module SchemaComponents
5
+ class Loader
6
+ attr_accessor :identifier, :type
7
+
8
+ VALID_TYPES = %w[
9
+ schemas parameters securitySchemes requestBodies responses headers examples links callbacks
10
+ ].freeze
11
+
12
+ def initialize(identifier = nil)
13
+ @identifier = identifier
14
+ end
15
+
16
+ VALID_TYPES.each do |type|
17
+ define_method(type) do
18
+ @type = type
19
+
20
+ load_components
21
+ end
22
+ end
23
+
24
+ private def load_components
25
+ components = {}
26
+
27
+ load_classes.each do |klass|
28
+ component_name = extract_component_name(klass)
29
+
30
+ definition = klass.new.to_schema
31
+
32
+ if components[component_name].present?
33
+ raise "Duplicate component name \"#{component_name}\" found in \"#{definitions_path}\""
34
+ end
35
+
36
+ components[component_name] = definition.merge(title: component_name)
37
+ end
38
+
39
+ components
40
+ end
41
+
42
+ private def load_classes
43
+ Dir.glob(base_path.join("#{definitions_path}/**/*")).filter_map do |file_path|
44
+ next if File.directory?(file_path)
45
+
46
+ file_path.gsub(base_path.to_s, "") # remove base_path
47
+ .gsub(".rb", "") # remove extension
48
+ .camelize.constantize
49
+ end
50
+ end
51
+
52
+ private def extract_component_name(klass)
53
+ klass_name = extract_class_name(klass)
54
+
55
+ klass_name_parts = klass_name.split("::").reject(&:blank?)
56
+
57
+ klass_name_parts.join
58
+ end
59
+
60
+ private def extract_class_name(klass)
61
+ klass.name.split("::").last
62
+ end
63
+
64
+ private def definitions_path
65
+ [
66
+ identifier,
67
+ type
68
+ ].compact.join("/")
69
+ end
70
+
71
+ private def base_path
72
+ @base_path ||= Rails.root.join(Rswag::SchemaComponents.components_base_path)
73
+ end
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,6 @@
1
+ module Rswag
2
+ module SchemaComponents
3
+ class Railtie < ::Rails::Railtie
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,5 @@
1
+ module Rswag
2
+ module SchemaComponents
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,15 @@
1
+ require "rswag/schema_components/version"
2
+ require "rswag/schema_components/railtie"
3
+ require "rswag/schema_components/loader"
4
+ require "rswag/schema_components/component"
5
+
6
+ module Rswag
7
+ module SchemaComponents
8
+ mattr_accessor :components_base_path
9
+ @@components_base_path = "app/api_components"
10
+
11
+ def self.setup
12
+ yield self
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :rswag_schema_components do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rswag-schema_components
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Marten Klitzke
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2024-06-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 3.0.0
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: 8.0.0
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: 3.0.0
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: 8.0.0
33
+ description: Schema Components for the rswag gem to define reusable schema components.
34
+ email:
35
+ - marten@fobizz.com
36
+ executables: []
37
+ extensions: []
38
+ extra_rdoc_files: []
39
+ files:
40
+ - MIT-LICENSE
41
+ - README.md
42
+ - Rakefile
43
+ - lib/rswag/schema_components.rb
44
+ - lib/rswag/schema_components/component.rb
45
+ - lib/rswag/schema_components/loader.rb
46
+ - lib/rswag/schema_components/railtie.rb
47
+ - lib/rswag/schema_components/version.rb
48
+ - lib/tasks/rswag/schema_components_tasks.rake
49
+ homepage: https://github.com/101skills-gmbh/rswag-schema-components
50
+ licenses:
51
+ - MIT
52
+ metadata:
53
+ homepage_uri: https://github.com/101skills-gmbh/rswag-schema-components
54
+ changelog_uri: https://github.com/101skills-gmbh/rswag-schema-components/blob/main/CHANGELOG.md
55
+ post_install_message:
56
+ rdoc_options: []
57
+ require_paths:
58
+ - lib
59
+ required_ruby_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: 2.7.0
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ requirements: []
70
+ rubygems_version: 3.5.13
71
+ signing_key:
72
+ specification_version: 4
73
+ summary: Schema Components for Rswag.
74
+ test_files: []