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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ec1f32df6c9400314583020aa177ef82226c5cd564f2409b5e86bda736488540
4
- data.tar.gz: 104cbcd05659b76c827be6bbf5ed95827f0e25440a69bc89d91084fa7235ba67
3
+ metadata.gz: 8293a1b8d230aaa789c5e396b3880647d38105f51275d7de098feefd895102c2
4
+ data.tar.gz: 361d781382d6026ab64b99f8e8c8562576df570e876f82a283b7ec67c450780e
5
5
  SHA512:
6
- metadata.gz: e43e6effbac010acfef9f6763b8dd9e1160e7ae3cf94cc10c3d6df15dfaee93bd22cf8545b131bd42be9d6ad8681fa7d549b2351cad32c97cd4100fd0cfb6129
7
- data.tar.gz: af5ea12308813f9e7d2510fa70b441f9257e8b8e8eca76843dd942d25166aac48e9cecfef399f6ddc8395ed457af22cecd7ab242961dcd15631c5cb5eae00501
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 to, which openapi_kit owns |
41
- | `modules` | namespace for every generated constant, so `Petstore::V1::Types::Pet` |
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
- One constant per file at the path that constant implies, so Rails autoloads it. Each run
61
- wipes `output`, but only after checking openapi_kit generated every `.rb` in it.
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 output**, if it is not already under `app/`. An acronym in `modules`
66
- becomes a directory, so it needs an inflection like any other acronym constant.
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 and
129
- authenticator, and controllers read it from `Petstore::V1.registry`. Rails instantiates
130
- controllers itself, so they cannot be handed one. Omit a slot, or pass something that does
131
- not implement its interface, and it does not compile.
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.registry = Petstore::V1::Registry.new(
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 namespace = modules.join("::")
110
+ def module_path = modules.map { |name| Naming.snake(name) }.join("/")
109
111
 
110
112
  sig { returns(String) }
111
- def module_path = modules.map { |name| Naming.snake(name) }.join("/")
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: "#{@config.module_path}/controllers/#{Naming.snake(tag)}_controller.rb",
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}.registry.#{Naming.snake(tag)}")
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}.registry.#{Naming.snake(name)}"
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: "#{@config.module_path}/handlers/#{Naming.snake(tag)}.rb",
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: "#{@config.module_path}/operations/#{Naming.snake(operation.id)}.rb",
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 it from the accessor, which an application assigns at boot.
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: "#{@config.module_path}/registry.rb",
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 it from here, and an application assigns it at boot.
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("extend T::Sig")
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.registry=(registry)") do
79
- buffer.line("@registry = registry")
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.registry") do
84
- buffer.line("@registry || raise(")
85
- buffer.indent { unset_message.each { |line| buffer.line(line) } }
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 unset_message
91
+ def unassigned_message
92
92
  namespace = @config.namespace
93
93
  [
94
- %("#{namespace}.registry has not been assigned. Build one in an initializer, " \\),
95
- %("e.g. #{namespace}.registry = #{namespace}::Registry.new(...).")
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
@@ -19,7 +19,7 @@ module OpenAPIKit
19
19
  return [] if @document.operations.empty?
20
20
 
21
21
  [
22
- Source.file(path: "#{@config.module_path}/routes.rb",
22
+ Source.file(path: "routes.rb",
23
23
  modules: @config.modules + ["Routes"]) { |buffer| emit_body(buffer) }
24
24
  ]
25
25
  end
@@ -35,7 +35,7 @@ module OpenAPIKit
35
35
  return [] if @document.security_schemes.empty?
36
36
 
37
37
  [
38
- Source.file(path: "#{@config.module_path}/security.rb",
38
+ Source.file(path: "security.rb",
39
39
  modules: @config.modules + ["Security"]) { |buffer| emit_body(buffer) }
40
40
  ]
41
41
  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: "#{@config.module_path}/types/#{Naming.snake(name)}.rb",
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.2
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.2
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.2
39
+ version: 0.1.0.pre.3
40
40
  - !ruby/object:Gem::Dependency
41
41
  name: prism
42
42
  requirement: !ruby/object:Gem::Requirement