openapi_kit-codegen 0.1.0.pre.2 → 0.1.0.pre.3
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/README.md +15 -12
- data/lib/openapi_kit/codegen/config.rb +4 -2
- data/lib/openapi_kit/codegen/emit/controllers.rb +3 -3
- data/lib/openapi_kit/codegen/emit/handlers.rb +1 -1
- data/lib/openapi_kit/codegen/emit/operations.rb +1 -1
- data/lib/openapi_kit/codegen/emit/registry.rb +17 -17
- data/lib/openapi_kit/codegen/emit/routes.rb +1 -1
- data/lib/openapi_kit/codegen/emit/security.rb +1 -1
- data/lib/openapi_kit/codegen/emit/types.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 8293a1b8d230aaa789c5e396b3880647d38105f51275d7de098feefd895102c2
|
|
4
|
+
data.tar.gz: 361d781382d6026ab64b99f8e8c8562576df570e876f82a283b7ec67c450780e
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 97cd58e24d8d2595c562eccda0d4920367534feea814ced250364a93b002b5bd63233ea8db9b2169f88dd6d03ae418c8113251ea1bee98519cdab7cf20811c3f
|
|
7
|
+
data.tar.gz: 974edf608f17bfda87eecb94cad8b2457ae7b2ecd97485f4fd805d911651a89598eda058ea3808bb40c5645fc0d877971536c05b8639876e0daa093d37ef4b81
|
data/README.md
CHANGED
|
@@ -28,7 +28,7 @@ end
|
|
|
28
28
|
```yaml
|
|
29
29
|
# openapi_kit.yml
|
|
30
30
|
spec: openapi/petstore.yaml
|
|
31
|
-
output: app/api
|
|
31
|
+
output: app/api/petstore/v1
|
|
32
32
|
modules: [Petstore, V1]
|
|
33
33
|
controller_base: Api::BaseController
|
|
34
34
|
principal: "::Petstore::Principal"
|
|
@@ -37,8 +37,8 @@ principal: "::Petstore::Principal"
|
|
|
37
37
|
| Option | Meaning |
|
|
38
38
|
| --- | --- |
|
|
39
39
|
| `spec` | root OpenAPI document, resolved relative to this file |
|
|
40
|
-
| `output` | directory the tree is written
|
|
41
|
-
| `modules` | namespace
|
|
40
|
+
| `output` | directory the tree is written into, which openapi_kit owns |
|
|
41
|
+
| `modules` | namespace every generated constant is declared in, so `Petstore::V1::Types::Pet` |
|
|
42
42
|
| `controller_base` | class the generated controllers inherit from |
|
|
43
43
|
| `principal` | the class a successful authentication produces |
|
|
44
44
|
| `type_mappings` | your Ruby type for a `type:format` pair |
|
|
@@ -57,13 +57,16 @@ app/api/petstore/v1/registry.rb
|
|
|
57
57
|
app/api/petstore/v1/routes.rb
|
|
58
58
|
```
|
|
59
59
|
|
|
60
|
-
|
|
61
|
-
|
|
60
|
+
Nothing is written outside `output`, and `modules` decides only what the files declare. One
|
|
61
|
+
constant per file at the path that constant implies, counting from `output`, so laying the
|
|
62
|
+
directory out to match the namespace is what lets Rails autoload it. Each run wipes
|
|
63
|
+
`output`, but only after checking openapi_kit generated every `.rb` in it.
|
|
62
64
|
|
|
63
65
|
## Integrating with Rails
|
|
64
66
|
|
|
65
|
-
**Autoload the
|
|
66
|
-
|
|
67
|
+
**Autoload the tree**, if it is not already under `app/`. The root is the directory
|
|
68
|
+
`modules` counts from, not `output` itself. An acronym in `modules` is a directory like any
|
|
69
|
+
other, so it needs an inflection.
|
|
67
70
|
|
|
68
71
|
```ruby
|
|
69
72
|
# config/application.rb
|
|
@@ -125,15 +128,15 @@ module Api
|
|
|
125
128
|
end
|
|
126
129
|
```
|
|
127
130
|
|
|
128
|
-
**Assign the registry.** openapi_kit generates a `Registry` struct with one slot per handler
|
|
129
|
-
authenticator, and controllers read
|
|
130
|
-
|
|
131
|
-
|
|
131
|
+
**Assign the registry.** openapi_kit generates a `Registry` struct with one slot per handler
|
|
132
|
+
and authenticator, and controllers read the one you assigned. Rails instantiates controllers
|
|
133
|
+
itself, so they cannot be handed one. Omit a slot, or pass something that does not
|
|
134
|
+
implement its interface, and it does not compile.
|
|
132
135
|
|
|
133
136
|
```ruby
|
|
134
137
|
# config/initializers/openapi_kit.rb
|
|
135
138
|
Rails.application.config.to_prepare do
|
|
136
|
-
Petstore::V1.
|
|
139
|
+
Petstore::V1::Registry.instance = Petstore::V1::Registry.new(
|
|
137
140
|
pets: PetsHandler.new(repo: PetRepo.new),
|
|
138
141
|
system: SystemHandler.new,
|
|
139
142
|
bearer_auth: BearerAuthenticator.new(decoder: TokenDecoder.new)
|
|
@@ -104,11 +104,13 @@ module OpenAPIKit
|
|
|
104
104
|
(value || {}).to_h { |k, v| [k.to_s, v.to_s] }
|
|
105
105
|
end
|
|
106
106
|
|
|
107
|
+
# The namespace as a path, which is what Rails routing wants in `to:`. File paths
|
|
108
|
+
# come from `output` instead, so this no longer decides where anything is written.
|
|
107
109
|
sig { returns(String) }
|
|
108
|
-
def
|
|
110
|
+
def module_path = modules.map { |name| Naming.snake(name) }.join("/")
|
|
109
111
|
|
|
110
112
|
sig { returns(String) }
|
|
111
|
-
def
|
|
113
|
+
def namespace = modules.join("::")
|
|
112
114
|
end
|
|
113
115
|
end
|
|
114
116
|
end
|
|
@@ -28,7 +28,7 @@ module OpenAPIKit
|
|
|
28
28
|
sig { override.returns(T::Array[SourceFile]) }
|
|
29
29
|
def render
|
|
30
30
|
@document.operations.group_by(&:tag).map do |tag, operations|
|
|
31
|
-
Source.file(path: "
|
|
31
|
+
Source.file(path: "controllers/#{Naming.snake(tag)}_controller.rb",
|
|
32
32
|
modules: @config.modules + ["Controllers"]) do |buffer|
|
|
33
33
|
emit_controller(buffer, tag, operations)
|
|
34
34
|
end
|
|
@@ -71,7 +71,7 @@ module OpenAPIKit
|
|
|
71
71
|
interface = "#{@config.namespace}::Handlers::#{Handlers.module_name(tag)}"
|
|
72
72
|
|
|
73
73
|
buffer.line("sig { returns(#{interface}) }")
|
|
74
|
-
buffer.line("def handler = #{@config.namespace}.
|
|
74
|
+
buffer.line("def handler = #{@config.namespace}::Registry.instance.#{Naming.snake(tag)}")
|
|
75
75
|
end
|
|
76
76
|
|
|
77
77
|
sig { params(buffer: Buffer, operation: Model::Operation).void }
|
|
@@ -126,7 +126,7 @@ module OpenAPIKit
|
|
|
126
126
|
def attempt(requirement)
|
|
127
127
|
name = T.must(requirement.schemes.keys.first)
|
|
128
128
|
scopes = T.must(requirement.schemes[name])
|
|
129
|
-
reader = "#{@config.namespace}.
|
|
129
|
+
reader = "#{@config.namespace}::Registry.instance.#{Naming.snake(name)}"
|
|
130
130
|
|
|
131
131
|
"-> { #{reader}.authenticate(request: request, scopes: #{scopes.inspect}) }"
|
|
132
132
|
end
|
|
@@ -20,7 +20,7 @@ module OpenAPIKit
|
|
|
20
20
|
sig { override.returns(T::Array[SourceFile]) }
|
|
21
21
|
def render
|
|
22
22
|
by_tag.map do |tag, operations|
|
|
23
|
-
Source.file(path: "
|
|
23
|
+
Source.file(path: "handlers/#{Naming.snake(tag)}.rb",
|
|
24
24
|
modules: @config.modules + ["Handlers"]) do |buffer|
|
|
25
25
|
emit_interface(buffer, tag, operations)
|
|
26
26
|
end
|
|
@@ -18,7 +18,7 @@ module OpenAPIKit
|
|
|
18
18
|
sig { override.returns(T::Array[SourceFile]) }
|
|
19
19
|
def render
|
|
20
20
|
@document.operations.map do |operation|
|
|
21
|
-
Source.file(path: "
|
|
21
|
+
Source.file(path: "operations/#{Naming.snake(operation.id)}.rb",
|
|
22
22
|
modules: @config.modules + ["Operations"]) do |buffer|
|
|
23
23
|
emit_operation(buffer, operation)
|
|
24
24
|
end
|
|
@@ -6,7 +6,7 @@ module OpenAPIKit
|
|
|
6
6
|
module Emit
|
|
7
7
|
# The typed boundary between openapi_kit's interfaces and an application's objects. Rails
|
|
8
8
|
# instantiates controllers itself, so a controller cannot be handed the registry: it
|
|
9
|
-
# reads
|
|
9
|
+
# reads the one an application assigned at boot.
|
|
10
10
|
class Registry
|
|
11
11
|
extend T::Sig
|
|
12
12
|
include Emitter
|
|
@@ -28,10 +28,8 @@ module OpenAPIKit
|
|
|
28
28
|
return [] if slots.empty?
|
|
29
29
|
|
|
30
30
|
[
|
|
31
|
-
Source.file(path: "
|
|
32
|
-
modules: @config.modules) { |buffer| emit_registry(buffer) }
|
|
33
|
-
Source.file(path: "#{@config.module_path}.rb",
|
|
34
|
-
modules: @config.modules) { |buffer| emit_accessor(buffer) }
|
|
31
|
+
Source.file(path: "registry.rb",
|
|
32
|
+
modules: @config.modules) { |buffer| emit_registry(buffer) }
|
|
35
33
|
]
|
|
36
34
|
end
|
|
37
35
|
|
|
@@ -62,37 +60,39 @@ module OpenAPIKit
|
|
|
62
60
|
sig { params(buffer: Buffer).void }
|
|
63
61
|
def emit_registry(buffer)
|
|
64
62
|
buffer.nest("class Registry < T::Struct") do
|
|
63
|
+
buffer.line("extend T::Sig")
|
|
64
|
+
buffer.blank
|
|
65
65
|
slots.each { |slot| buffer.line("const :#{slot.reader}, #{slot.interface}") }
|
|
66
|
+
buffer.blank
|
|
67
|
+
emit_accessor(buffer)
|
|
66
68
|
end
|
|
67
69
|
end
|
|
68
70
|
|
|
69
71
|
# Rails instantiates controllers itself, so a controller cannot be handed the
|
|
70
|
-
# registry. It reads
|
|
72
|
+
# registry. It reads the one here, which an application assigns at boot.
|
|
71
73
|
sig { params(buffer: Buffer).void }
|
|
72
74
|
def emit_accessor(buffer)
|
|
73
|
-
buffer.line("
|
|
74
|
-
buffer.blank
|
|
75
|
-
buffer.line("@registry = T.let(nil, T.nilable(Registry))")
|
|
75
|
+
buffer.line("@instance = T.let(nil, T.nilable(Registry))")
|
|
76
76
|
buffer.blank
|
|
77
77
|
buffer.line("sig { params(registry: Registry).void }")
|
|
78
|
-
buffer.nest("def self.
|
|
79
|
-
buffer.line("@
|
|
78
|
+
buffer.nest("def self.instance=(registry)") do
|
|
79
|
+
buffer.line("@instance = registry")
|
|
80
80
|
end
|
|
81
81
|
buffer.blank
|
|
82
82
|
buffer.line("sig { returns(Registry) }")
|
|
83
|
-
buffer.nest("def self.
|
|
84
|
-
buffer.line("@
|
|
85
|
-
buffer.indent {
|
|
83
|
+
buffer.nest("def self.instance") do
|
|
84
|
+
buffer.line("@instance || raise(")
|
|
85
|
+
buffer.indent { unassigned_message.each { |line| buffer.line(line) } }
|
|
86
86
|
buffer.line(")")
|
|
87
87
|
end
|
|
88
88
|
end
|
|
89
89
|
|
|
90
90
|
sig { returns(T::Array[String]) }
|
|
91
|
-
def
|
|
91
|
+
def unassigned_message
|
|
92
92
|
namespace = @config.namespace
|
|
93
93
|
[
|
|
94
|
-
%("#{namespace}.
|
|
95
|
-
%("e.g. #{namespace}.
|
|
94
|
+
%("#{namespace}::Registry.instance has not been assigned. Build one in an " \\),
|
|
95
|
+
%("initializer, e.g. #{namespace}::Registry.instance = #{namespace}::Registry.new(...).")
|
|
96
96
|
]
|
|
97
97
|
end
|
|
98
98
|
end
|
|
@@ -21,7 +21,7 @@ module OpenAPIKit
|
|
|
21
21
|
def render
|
|
22
22
|
declared_types.map do |type|
|
|
23
23
|
name = Model::TypeDef.name_of(type)
|
|
24
|
-
Source.file(path: "
|
|
24
|
+
Source.file(path: "types/#{Naming.snake(name)}.rb",
|
|
25
25
|
modules: @config.modules + ["Types"]) do |buffer|
|
|
26
26
|
emit_declaration(buffer, type)
|
|
27
27
|
end
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: openapi_kit-codegen
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.1.0.pre.
|
|
4
|
+
version: 0.1.0.pre.3
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Jack Robertson
|
|
@@ -29,14 +29,14 @@ dependencies:
|
|
|
29
29
|
requirements:
|
|
30
30
|
- - '='
|
|
31
31
|
- !ruby/object:Gem::Version
|
|
32
|
-
version: 0.1.0.pre.
|
|
32
|
+
version: 0.1.0.pre.3
|
|
33
33
|
type: :runtime
|
|
34
34
|
prerelease: false
|
|
35
35
|
version_requirements: !ruby/object:Gem::Requirement
|
|
36
36
|
requirements:
|
|
37
37
|
- - '='
|
|
38
38
|
- !ruby/object:Gem::Version
|
|
39
|
-
version: 0.1.0.pre.
|
|
39
|
+
version: 0.1.0.pre.3
|
|
40
40
|
- !ruby/object:Gem::Dependency
|
|
41
41
|
name: prism
|
|
42
42
|
requirement: !ruby/object:Gem::Requirement
|