rapitapir 0.1.2 → 2.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 +4 -4
- data/.rubocop.yml +7 -7
- data/.rubocop_todo.yml +88 -0
- data/CHANGELOG.md +29 -1
- data/README.md +1339 -236
- data/RUBY_WEEKLY_LAUNCH_POST.md +200 -0
- data/docs/RAILS_INTEGRATION_IMPLEMENTATION.md +209 -0
- data/docs/SINATRA_EXTENSION.md +399 -348
- data/docs/STRICT_VALIDATION.md +229 -0
- data/docs/VALIDATION_IMPROVEMENTS.md +218 -0
- data/docs/adr/0001-base-class-strategy.md +33 -0
- data/docs/adr/0002-type-shortcut-and-type-system.md +30 -0
- data/docs/adr/0003-openapi-source-of-truth.md +32 -0
- data/docs/adr/0004-client-generation-typescript-first.md +34 -0
- data/docs/adr/0005-auth-architecture.md +32 -0
- data/docs/adr/0006-observability-defaults.md +30 -0
- data/docs/adr/0007-ai-scope-and-boundaries.md +30 -0
- data/docs/adr/README.md +46 -0
- data/docs/adr/template.md +24 -0
- data/docs/ai-integration-plan.md +112 -0
- data/docs/auto-derivation.md +505 -92
- data/docs/blueprint.md +92 -4
- data/docs/endpoint-definition.md +536 -129
- data/docs/implementation-status.md +1 -1
- data/docs/n8n-integration.md +212 -0
- data/docs/observability.md +810 -500
- data/docs/using-mcp.md +93 -0
- data/examples/ai/knowledge_base_rag.rb +83 -0
- data/examples/ai/user_management_mcp.rb +92 -0
- data/examples/ai/user_validation_llm.rb +187 -0
- data/examples/auto_derivation_ruby_friendly.rb +44 -113
- data/examples/cli/user_api_endpoints.rb +63 -36
- data/examples/demo_api.rb +57 -18
- data/examples/enterprise_extension_demo.rb +13 -13
- data/examples/enterprise_rapitapir_api.rb +81 -93
- data/examples/nocodb/nocodb_scaffold/Gemfile +22 -0
- data/examples/nocodb/nocodb_scaffold/README.md +10 -0
- data/examples/nocodb/nocodb_scaffold/Rakefile +7 -0
- data/examples/nocodb/nocodb_scaffold/app/api/app.rb +1139 -0
- data/examples/nocodb/nocodb_scaffold/app/models/email_template.rb +6 -0
- data/examples/nocodb/nocodb_scaffold/app/models/faq.rb +6 -0
- data/examples/nocodb/nocodb_scaffold/app/models/feature.rb +6 -0
- data/examples/nocodb/nocodb_scaffold/app/models/idea.rb +6 -0
- data/examples/nocodb/nocodb_scaffold/app/models/itinerary_day.rb +6 -0
- data/examples/nocodb/nocodb_scaffold/app/models/route.rb +6 -0
- data/examples/nocodb/nocodb_scaffold/app/models/subscriber.rb +6 -0
- data/examples/nocodb/nocodb_scaffold/app/models/tour.rb +6 -0
- data/examples/nocodb/nocodb_scaffold/app/models/webhook.rb +6 -0
- data/examples/nocodb/nocodb_scaffold/app/models_loader.rb +3 -0
- data/examples/nocodb/nocodb_scaffold/config/database.yml +11 -0
- data/examples/nocodb/nocodb_scaffold/config.ru +9 -0
- data/examples/nocodb/nocodb_scaffold/db/migrate/20250808152012_create_features.rb +10 -0
- data/examples/nocodb/nocodb_scaffold/db/migrate/20250808152013_create_tours.rb +10 -0
- data/examples/nocodb/nocodb_scaffold/db/migrate/20250808152014_create_routes.rb +10 -0
- data/examples/nocodb/nocodb_scaffold/db/migrate/20250808152015_create_itinerary_days.rb +10 -0
- data/examples/nocodb/nocodb_scaffold/db/migrate/20250808152016_create_subscribers.rb +10 -0
- data/examples/nocodb/nocodb_scaffold/db/migrate/20250808152017_create_email_templates.rb +10 -0
- data/examples/nocodb/nocodb_scaffold/db/migrate/20250808152018_create_faqs.rb +10 -0
- data/examples/nocodb/nocodb_scaffold/db/migrate/20250808152019_create_webhooks.rb +10 -0
- data/examples/nocodb/nocodb_scaffold/db/migrate/20250808152020_create_ideas.rb +10 -0
- data/examples/nocodb/swagger.json +4378 -0
- data/examples/production_ready_example.rb +78 -58
- data/examples/rails/RAILS_8_GUIDE.md +165 -0
- data/examples/rails/RAILS_LOADING_FIX.rb +35 -0
- data/examples/rails/README.md +497 -0
- data/examples/rails/comprehensive_test.rb +91 -0
- data/examples/rails/config/routes.rb +48 -0
- data/examples/rails/debug_controller.rb +63 -0
- data/examples/rails/detailed_test.rb +46 -0
- data/examples/rails/enhanced_users_controller.rb +278 -0
- data/examples/rails/final_server_test.rb +50 -0
- data/examples/rails/hello_world_app.rb +116 -0
- data/examples/rails/hello_world_controller.rb +186 -0
- data/examples/rails/hello_world_routes.rb +28 -0
- data/examples/rails/rails8_minimal_demo.rb +132 -0
- data/examples/rails/rails8_simple_demo.rb +140 -0
- data/examples/rails/rails8_working_demo.rb +255 -0
- data/examples/rails/real_world_blog_api.rb +510 -0
- data/examples/rails/server_test.rb +46 -0
- data/examples/rails/test_direct_processing.rb +41 -0
- data/examples/rails/test_hello_world.rb +80 -0
- data/examples/rails/test_rails_integration.rb +54 -0
- data/examples/rails/traditional_app/Gemfile +37 -0
- data/examples/rails/traditional_app/README.md +265 -0
- data/examples/rails/traditional_app/app/controllers/api/v1/posts_controller.rb +254 -0
- data/examples/rails/traditional_app/app/controllers/api/v1/users_controller.rb +220 -0
- data/examples/rails/traditional_app/app/controllers/application_controller.rb +86 -0
- data/examples/rails/traditional_app/app/controllers/application_controller_simplified.rb +87 -0
- data/examples/rails/traditional_app/app/controllers/documentation_controller.rb +149 -0
- data/examples/rails/traditional_app/app/controllers/health_controller.rb +42 -0
- data/examples/rails/traditional_app/config/routes.rb +25 -0
- data/examples/rails/traditional_app/config/routes_best_practice.rb +25 -0
- data/examples/rails/traditional_app/config/routes_simplified.rb +36 -0
- data/examples/rails/traditional_app_runnable.rb +406 -0
- data/examples/rails/users_controller.rb +4 -1
- data/examples/serverless/Gemfile +43 -0
- data/examples/serverless/QUICKSTART.md +331 -0
- data/examples/serverless/README.md +520 -0
- data/examples/serverless/aws_lambda_example.rb +307 -0
- data/examples/serverless/aws_sam_template.yaml +215 -0
- data/examples/serverless/azure_functions_example.rb +407 -0
- data/examples/serverless/deploy.rb +204 -0
- data/examples/serverless/gcp_cloud_functions_example.rb +367 -0
- data/examples/serverless/gcp_function.yaml +23 -0
- data/examples/serverless/host.json +24 -0
- data/examples/serverless/package.json +32 -0
- data/examples/serverless/spec/aws_lambda_spec.rb +196 -0
- data/examples/serverless/spec/spec_helper.rb +89 -0
- data/examples/serverless/vercel.json +31 -0
- data/examples/serverless/vercel_example.rb +404 -0
- data/examples/simple_demo_api.rb +40 -7
- data/examples/strict_validation_examples.rb +104 -0
- data/examples/user_api.rb +130 -132
- data/examples/validation_error_examples.rb +173 -0
- data/lib/rapitapir/ai/llm_instruction.rb +456 -0
- data/lib/rapitapir/ai/mcp.rb +134 -0
- data/lib/rapitapir/ai/rag.rb +287 -0
- data/lib/rapitapir/ai/rag_middleware.rb +147 -0
- data/lib/rapitapir/auth/oauth2.rb +44 -58
- data/lib/rapitapir/cli/command.rb +392 -6
- data/lib/rapitapir/cli/mcp_export.rb +18 -0
- data/lib/rapitapir/cli/scaffold_generator.rb +639 -0
- data/lib/rapitapir/cli/validator.rb +3 -7
- data/lib/rapitapir/core/endpoint.rb +59 -6
- data/lib/rapitapir/core/enhanced_endpoint.rb +3 -7
- data/lib/rapitapir/dsl/enhanced_structures.rb +1 -1
- data/lib/rapitapir/dsl/fluent_endpoint_builder.rb +53 -0
- data/lib/rapitapir/endpoint_registry.rb +47 -0
- data/lib/rapitapir/observability/health_check.rb +4 -4
- data/lib/rapitapir/observability/logging.rb +10 -10
- data/lib/rapitapir/openapi/schema_generator.rb +46 -2
- data/lib/rapitapir/schema.rb +2 -2
- data/lib/rapitapir/server/enhanced_rack_adapter.rb +11 -8
- data/lib/rapitapir/server/middleware.rb +2 -2
- data/lib/rapitapir/server/rack_adapter.rb +1 -3
- data/lib/rapitapir/server/rails/configuration.rb +77 -0
- data/lib/rapitapir/server/rails/controller_base.rb +185 -0
- data/lib/rapitapir/server/rails/documentation_helpers.rb +76 -0
- data/lib/rapitapir/server/rails/resource_builder.rb +181 -0
- data/lib/rapitapir/server/rails/routes.rb +114 -0
- data/lib/rapitapir/server/rails_adapter.rb +10 -3
- data/lib/rapitapir/server/rails_adapter_class.rb +1 -3
- data/lib/rapitapir/server/rails_controller.rb +1 -3
- data/lib/rapitapir/server/rails_integration.rb +67 -0
- data/lib/rapitapir/server/rails_response_handler.rb +16 -3
- data/lib/rapitapir/server/sinatra_adapter.rb +29 -5
- data/lib/rapitapir/server/sinatra_integration.rb +4 -4
- data/lib/rapitapir/sinatra/extension.rb +2 -2
- data/lib/rapitapir/sinatra/oauth2_helpers.rb +34 -40
- data/lib/rapitapir/types/array.rb +4 -0
- data/lib/rapitapir/types/auto_derivation.rb +4 -20
- data/lib/rapitapir/types/datetime.rb +1 -3
- data/lib/rapitapir/types/float.rb +2 -6
- data/lib/rapitapir/types/hash.rb +40 -2
- data/lib/rapitapir/types/integer.rb +4 -12
- data/lib/rapitapir/types/object.rb +6 -2
- data/lib/rapitapir/types.rb +6 -2
- data/lib/rapitapir/version.rb +1 -1
- data/lib/rapitapir.rb +5 -3
- data/rapitapir.gemspec +3 -2
- metadata +142 -8
|
@@ -0,0 +1,639 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'json'
|
|
4
|
+
require 'fileutils'
|
|
5
|
+
|
|
6
|
+
module RapiTapir
|
|
7
|
+
module CLI
|
|
8
|
+
# ScaffoldGenerator builds a SinatraRapiTapir + ActiveRecord (SQLite) app
|
|
9
|
+
# from an OpenAPI/Swagger JSON file.
|
|
10
|
+
class ScaffoldGenerator
|
|
11
|
+
attr_reader :openapi_path, :output_dir, :config
|
|
12
|
+
|
|
13
|
+
def initialize(openapi_path:, output_dir:, config: {})
|
|
14
|
+
@openapi_path = openapi_path
|
|
15
|
+
@output_dir = output_dir
|
|
16
|
+
@config = config
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def generate!
|
|
20
|
+
spec = JSON.parse(File.read(openapi_path))
|
|
21
|
+
FileUtils.mkdir_p(output_dir)
|
|
22
|
+
create_gemfile
|
|
23
|
+
create_rakefile
|
|
24
|
+
create_config_ru
|
|
25
|
+
create_db_config
|
|
26
|
+
create_app_structure
|
|
27
|
+
create_models(spec)
|
|
28
|
+
create_migrations(spec)
|
|
29
|
+
create_api_app(spec)
|
|
30
|
+
create_readme(spec)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
private
|
|
34
|
+
|
|
35
|
+
def create_gemfile
|
|
36
|
+
content = <<~RUBY
|
|
37
|
+
source 'https://rubygems.org'
|
|
38
|
+
|
|
39
|
+
ruby '>= 3.2.0'
|
|
40
|
+
|
|
41
|
+
gem 'sinatra'
|
|
42
|
+
gem 'activerecord', require: 'active_record'
|
|
43
|
+
gem 'sinatra-activerecord'
|
|
44
|
+
gem 'sqlite3'
|
|
45
|
+
gem 'rackup'
|
|
46
|
+
gem 'rake'
|
|
47
|
+
|
|
48
|
+
# API layer
|
|
49
|
+
# If you're using this scaffold from inside the rapitapir repo, this local path works.
|
|
50
|
+
# Otherwise, replace with: gem 'rapitapir', '~> #{RapiTapir::VERSION}'
|
|
51
|
+
gem 'rapitapir', path: File.expand_path('../../..', __dir__)
|
|
52
|
+
|
|
53
|
+
group :development, :test do
|
|
54
|
+
gem 'rspec'
|
|
55
|
+
gem 'rack-test'
|
|
56
|
+
gem 'dotenv'
|
|
57
|
+
gem 'pry'
|
|
58
|
+
end
|
|
59
|
+
RUBY
|
|
60
|
+
write_file('Gemfile', content)
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def create_rakefile
|
|
64
|
+
content = <<~RUBY
|
|
65
|
+
# frozen_string_literal: true
|
|
66
|
+
|
|
67
|
+
ENV['RACK_ENV'] ||= 'development'
|
|
68
|
+
|
|
69
|
+
require 'dotenv/load'
|
|
70
|
+
require_relative 'app/api/app'
|
|
71
|
+
require 'sinatra/activerecord/rake'
|
|
72
|
+
RUBY
|
|
73
|
+
write_file('Rakefile', content)
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def create_config_ru
|
|
77
|
+
content = <<~RUBY
|
|
78
|
+
# frozen_string_literal: true
|
|
79
|
+
|
|
80
|
+
$LOAD_PATH.unshift File.expand_path('app', __dir__)
|
|
81
|
+
|
|
82
|
+
require 'bundler/setup'
|
|
83
|
+
require 'rapitapir'
|
|
84
|
+
require 'api/app'
|
|
85
|
+
|
|
86
|
+
run API::App
|
|
87
|
+
RUBY
|
|
88
|
+
write_file('config.ru', content)
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
def create_db_config
|
|
92
|
+
FileUtils.mkdir_p(File.join(output_dir, 'config'))
|
|
93
|
+
content = <<~YAML
|
|
94
|
+
development:
|
|
95
|
+
adapter: sqlite3
|
|
96
|
+
database: db/development.sqlite3
|
|
97
|
+
|
|
98
|
+
test:
|
|
99
|
+
adapter: sqlite3
|
|
100
|
+
database: db/test.sqlite3
|
|
101
|
+
|
|
102
|
+
production:
|
|
103
|
+
adapter: sqlite3
|
|
104
|
+
database: db/production.sqlite3
|
|
105
|
+
YAML
|
|
106
|
+
write_file('config/database.yml', content)
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
def create_app_structure
|
|
110
|
+
%w[app app/models app/api db db/migrate spec].each do |dir|
|
|
111
|
+
FileUtils.mkdir_p(File.join(output_dir, dir))
|
|
112
|
+
end
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
def create_models(spec)
|
|
116
|
+
# Minimal placeholder: one model per top-level tag (if present)
|
|
117
|
+
tags = collect_tags(spec)
|
|
118
|
+
tags.each do |tag|
|
|
119
|
+
class_name = classify(singularize(tag))
|
|
120
|
+
content = <<~RUBY
|
|
121
|
+
# frozen_string_literal: true
|
|
122
|
+
|
|
123
|
+
module Models
|
|
124
|
+
class #{class_name} < ActiveRecord::Base
|
|
125
|
+
end
|
|
126
|
+
end
|
|
127
|
+
RUBY
|
|
128
|
+
write_file("app/models/#{underscore(class_name)}.rb", content)
|
|
129
|
+
end
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
def create_migrations(spec)
|
|
133
|
+
timestamp = Time.now.strftime('%Y%m%d%H%M%S')
|
|
134
|
+
tags = collect_tags(spec)
|
|
135
|
+
tags.each_with_index do |tag, idx|
|
|
136
|
+
t = (timestamp.to_i + idx).to_s
|
|
137
|
+
table = pluralize(underscore(classify(tag)))
|
|
138
|
+
content = <<~RUBY
|
|
139
|
+
# frozen_string_literal: true
|
|
140
|
+
|
|
141
|
+
class Create#{classify(table)} < ActiveRecord::Migration[7.1]
|
|
142
|
+
def change
|
|
143
|
+
create_table :#{table} do |t|
|
|
144
|
+
t.string :name
|
|
145
|
+
t.timestamps
|
|
146
|
+
end
|
|
147
|
+
end
|
|
148
|
+
end
|
|
149
|
+
RUBY
|
|
150
|
+
write_file("db/migrate/#{t}_create_#{table}.rb", content)
|
|
151
|
+
end
|
|
152
|
+
end
|
|
153
|
+
|
|
154
|
+
def create_api_app(spec)
|
|
155
|
+
content = <<~RUBY
|
|
156
|
+
# frozen_string_literal: true
|
|
157
|
+
|
|
158
|
+
require 'sinatra/base'
|
|
159
|
+
require 'sinatra/activerecord'
|
|
160
|
+
require 'json'
|
|
161
|
+
require 'rapitapir'
|
|
162
|
+
require 'rapitapir/sinatra_rapitapir'
|
|
163
|
+
require_relative '../models_loader'
|
|
164
|
+
|
|
165
|
+
module API
|
|
166
|
+
class App < RapiTapir::SinatraRapiTapir
|
|
167
|
+
register Sinatra::ActiveRecordExtension
|
|
168
|
+
set :database_file, File.expand_path('../../config/database.yml', __dir__)
|
|
169
|
+
|
|
170
|
+
rapitapir do
|
|
171
|
+
info(title: #{spec.dig('info', 'title').inspect}, version: #{spec.dig('info', 'version').inspect})
|
|
172
|
+
development_defaults!
|
|
173
|
+
end
|
|
174
|
+
|
|
175
|
+
helpers do
|
|
176
|
+
def json(obj)
|
|
177
|
+
content_type :json
|
|
178
|
+
JSON.dump(obj)
|
|
179
|
+
end
|
|
180
|
+
end
|
|
181
|
+
|
|
182
|
+
# Basic health
|
|
183
|
+
get '/health' do
|
|
184
|
+
json(ok: true)
|
|
185
|
+
end
|
|
186
|
+
|
|
187
|
+
# Generated endpoints (scaffold level)
|
|
188
|
+
#{generate_endpoints_block(spec)}
|
|
189
|
+
end
|
|
190
|
+
end
|
|
191
|
+
RUBY
|
|
192
|
+
# Also add a models_loader to require all models
|
|
193
|
+
models_loader = <<~RUBY
|
|
194
|
+
# frozen_string_literal: true
|
|
195
|
+
|
|
196
|
+
Dir[File.expand_path('models/**/*.rb', __dir__)].sort.each { |f| require f }
|
|
197
|
+
RUBY
|
|
198
|
+
write_file('app/api/app.rb', content)
|
|
199
|
+
write_file('app/models_loader.rb', models_loader)
|
|
200
|
+
end
|
|
201
|
+
|
|
202
|
+
def create_readme(spec)
|
|
203
|
+
content = <<~MD
|
|
204
|
+
# #{spec.dig('info', 'title') || 'RapiTapir Sinatra App'}
|
|
205
|
+
|
|
206
|
+
Generated by RapiTapir scaffold from OpenAPI spec.
|
|
207
|
+
|
|
208
|
+
## Run
|
|
209
|
+
```bash
|
|
210
|
+
bundle install
|
|
211
|
+
bundle exec rake db:create db:migrate
|
|
212
|
+
bundle exec rackup
|
|
213
|
+
```
|
|
214
|
+
MD
|
|
215
|
+
write_file('README.md', content)
|
|
216
|
+
end
|
|
217
|
+
|
|
218
|
+
# --- Helpers ---
|
|
219
|
+
|
|
220
|
+
def generate_endpoints_block(spec)
|
|
221
|
+
paths = spec['paths'] || {}
|
|
222
|
+
blocks = []
|
|
223
|
+
|
|
224
|
+
paths.each do |raw_path, path_item|
|
|
225
|
+
next unless path_item.is_a?(Hash)
|
|
226
|
+
|
|
227
|
+
path = to_sinatra_path(raw_path)
|
|
228
|
+
operation_methods(path_item).each do |http|
|
|
229
|
+
operation = path_item[http]
|
|
230
|
+
blocks << endpoint_block(http, path, operation, path_item, spec)
|
|
231
|
+
end
|
|
232
|
+
end
|
|
233
|
+
|
|
234
|
+
blocks.compact.join("\n\n ")
|
|
235
|
+
end
|
|
236
|
+
|
|
237
|
+
# rubocop:disable-next Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
|
|
238
|
+
def endpoint_block(http, path, operation, path_item, spec)
|
|
239
|
+
method = http.to_s.downcase
|
|
240
|
+
return nil unless %w[get post put patch delete].include?(method)
|
|
241
|
+
|
|
242
|
+
tag = (operation.is_a?(Hash) ? operation['tags']&.first : nil) || path.split('/')[1] || 'resource'
|
|
243
|
+
model = classify(tag)
|
|
244
|
+
table = pluralize(underscore(model))
|
|
245
|
+
model_class = "Models::#{classify(singularize(table))}"
|
|
246
|
+
|
|
247
|
+
summary = operation.is_a?(Hash) ? (operation['summary'] || "#{method.upcase} #{path}") : "#{method.upcase} #{path}"
|
|
248
|
+
description = operation.is_a?(Hash) ? operation['description'] : nil
|
|
249
|
+
op_tags = operation.is_a?(Hash) ? Array(operation['tags']).compact : []
|
|
250
|
+
|
|
251
|
+
dsl = []
|
|
252
|
+
dsl << " RapiTapir.#{method}('#{path}')"
|
|
253
|
+
|
|
254
|
+
# Parameters (path-level + operation-level)
|
|
255
|
+
params = merge_parameters(path_item, operation)
|
|
256
|
+
path_param_names = extract_path_params(path)
|
|
257
|
+
path_params = []
|
|
258
|
+
params.each do |param|
|
|
259
|
+
next unless param.is_a?(Hash)
|
|
260
|
+
|
|
261
|
+
location = param['in']
|
|
262
|
+
name = param['name']
|
|
263
|
+
schema = param['schema'] || {}
|
|
264
|
+
type_code = schema_to_t(schema, spec)
|
|
265
|
+
# Fallback for non-body params with missing schema
|
|
266
|
+
type_code = 'T.string' if (schema.nil? || !schema.is_a?(Hash) || !schema['type']) && location != 'body'
|
|
267
|
+
type_code = "T.optional(#{type_code})" unless param['required']
|
|
268
|
+
desc_opt = param['description'] ? ", description: #{param['description'].inspect}" : ''
|
|
269
|
+
|
|
270
|
+
case location
|
|
271
|
+
when 'path'
|
|
272
|
+
if path_param_names.include?(name.to_s)
|
|
273
|
+
path_params << name.to_s
|
|
274
|
+
dsl << " .path_param(:#{name}, #{type_code}#{desc_opt})"
|
|
275
|
+
end
|
|
276
|
+
when 'query'
|
|
277
|
+
dsl << " .query(:#{safe_symbol(name)}, #{type_code}#{desc_opt})"
|
|
278
|
+
when 'header'
|
|
279
|
+
# Headers keep original case/dashes as string name
|
|
280
|
+
header_name = name.to_s
|
|
281
|
+
dsl << " .header(#{header_name.inspect}, #{type_code}#{desc_opt})"
|
|
282
|
+
end
|
|
283
|
+
end
|
|
284
|
+
|
|
285
|
+
# Ensure any path params present in the URL but missing from parameters are covered
|
|
286
|
+
(path_param_names - path_params).each do |p|
|
|
287
|
+
dsl << " .path_param(:#{p}, T.string)"
|
|
288
|
+
end
|
|
289
|
+
|
|
290
|
+
# Security headers from components/security + operation/global security
|
|
291
|
+
applied_security(operation, spec).each do |scheme_name|
|
|
292
|
+
scheme = spec.dig('components', 'securitySchemes', scheme_name)
|
|
293
|
+
next unless scheme.is_a?(Hash)
|
|
294
|
+
|
|
295
|
+
next unless scheme['type'] == 'apiKey' && scheme['in'] == 'header'
|
|
296
|
+
|
|
297
|
+
header_name = scheme['name'] || scheme_name
|
|
298
|
+
desc = scheme['description'] ? ", description: #{scheme['description'].inspect}" : ''
|
|
299
|
+
dsl << " .header(#{header_name.inspect}, T.string#{desc})"
|
|
300
|
+
end
|
|
301
|
+
|
|
302
|
+
# Request body
|
|
303
|
+
if (schema = request_body_schema(operation, spec))
|
|
304
|
+
dsl << " .json_body(#{schema_to_t(schema, spec)})"
|
|
305
|
+
end
|
|
306
|
+
|
|
307
|
+
# Output (map first 2xx response schema)
|
|
308
|
+
success_code = operation.is_a?(Hash) ? (first_success_code(operation) || 200) : 200
|
|
309
|
+
if success_code == 204
|
|
310
|
+
dsl << ' .no_content'
|
|
311
|
+
else
|
|
312
|
+
resp_schema = response_schema_for_code(operation, success_code, spec)
|
|
313
|
+
type_expr = resp_schema ? schema_to_t(resp_schema, spec) : 'T.hash({})'
|
|
314
|
+
dsl << if success_code == 201
|
|
315
|
+
" .created(#{type_expr})"
|
|
316
|
+
else
|
|
317
|
+
" .ok(#{type_expr})"
|
|
318
|
+
end
|
|
319
|
+
end
|
|
320
|
+
|
|
321
|
+
dsl << " .summary(#{summary.inspect})"
|
|
322
|
+
dsl << " .description(#{description.inspect})" if description && !description.strip.empty?
|
|
323
|
+
dsl << " .tags(#{op_tags.map(&:inspect).join(', ')})" if op_tags.any?
|
|
324
|
+
dsl << '.build'
|
|
325
|
+
|
|
326
|
+
handler = default_handler(method, table, model_class, path_param_names, success_code)
|
|
327
|
+
|
|
328
|
+
<<~RUBY
|
|
329
|
+
endpoint(
|
|
330
|
+
#{dsl.join("\n ")}
|
|
331
|
+
) do
|
|
332
|
+
#{handler}
|
|
333
|
+
end
|
|
334
|
+
RUBY
|
|
335
|
+
end
|
|
336
|
+
|
|
337
|
+
# Combine path-level and operation-level parameters. Later duplicates override earlier.
|
|
338
|
+
def merge_parameters(path_item, operation)
|
|
339
|
+
seen = {}
|
|
340
|
+
[path_item['parameters'], operation.is_a?(Hash) ? operation['parameters'] : nil].compact.each do |arr|
|
|
341
|
+
next unless arr.is_a?(Array)
|
|
342
|
+
|
|
343
|
+
arr.each do |param|
|
|
344
|
+
next unless param.is_a?(Hash)
|
|
345
|
+
|
|
346
|
+
key = [param['in'], param['name']].join(':')
|
|
347
|
+
seen[key] = param
|
|
348
|
+
end
|
|
349
|
+
end
|
|
350
|
+
# Maintain stable order grouped by location
|
|
351
|
+
seen.values
|
|
352
|
+
end
|
|
353
|
+
|
|
354
|
+
def request_body_schema(operation, spec)
|
|
355
|
+
return nil unless operation.is_a?(Hash)
|
|
356
|
+
|
|
357
|
+
content = operation.dig('requestBody', 'content')
|
|
358
|
+
return nil unless content.is_a?(Hash)
|
|
359
|
+
|
|
360
|
+
# Prefer JSON
|
|
361
|
+
json = content['application/json'] || content.values.first
|
|
362
|
+
return nil unless json.is_a?(Hash)
|
|
363
|
+
|
|
364
|
+
schema = json['schema']
|
|
365
|
+
resolved = resolve_schema(schema, spec)
|
|
366
|
+
# Attach original component name to metadata for OpenAPI generator pass-through
|
|
367
|
+
if schema.is_a?(Hash) && schema['$ref']
|
|
368
|
+
name = schema['$ref'].split('/').last
|
|
369
|
+
return resolved.merge('__component_name__' => name) if resolved.is_a?(Hash)
|
|
370
|
+
end
|
|
371
|
+
resolved
|
|
372
|
+
end
|
|
373
|
+
|
|
374
|
+
def response_schema_for_code(operation, code, spec)
|
|
375
|
+
return nil unless operation.is_a?(Hash)
|
|
376
|
+
|
|
377
|
+
resp = (operation['responses'] || {})[code.to_s]
|
|
378
|
+
return nil unless resp.is_a?(Hash)
|
|
379
|
+
|
|
380
|
+
content = resp['content']
|
|
381
|
+
return nil unless content.is_a?(Hash)
|
|
382
|
+
|
|
383
|
+
json = content['application/json'] || content.values.first
|
|
384
|
+
return nil unless json.is_a?(Hash)
|
|
385
|
+
|
|
386
|
+
schema = json['schema']
|
|
387
|
+
resolved = resolve_schema(schema, spec)
|
|
388
|
+
if schema.is_a?(Hash) && schema['$ref']
|
|
389
|
+
name = schema['$ref'].split('/').last
|
|
390
|
+
return resolved.merge('__component_name__' => name) if resolved.is_a?(Hash)
|
|
391
|
+
end
|
|
392
|
+
resolved
|
|
393
|
+
end
|
|
394
|
+
|
|
395
|
+
# --- OpenAPI schema -> RapiTapir::Types DSL (as string) ---
|
|
396
|
+
# rubocop:disable-next Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
|
|
397
|
+
def schema_to_t(schema, spec)
|
|
398
|
+
schema = resolve_schema(schema, spec) || {}
|
|
399
|
+
return 'T.hash({})' unless schema.is_a?(Hash)
|
|
400
|
+
|
|
401
|
+
component_name = schema['__component_name__']
|
|
402
|
+
|
|
403
|
+
# Nullable -> make optional
|
|
404
|
+
nullable = schema['nullable']
|
|
405
|
+
|
|
406
|
+
type_expr = case schema['type']
|
|
407
|
+
when 'string'
|
|
408
|
+
fmt = (schema['format'] || '').downcase
|
|
409
|
+
base = case fmt
|
|
410
|
+
when 'date-time' then 'T.datetime'
|
|
411
|
+
when 'date' then 'T.date'
|
|
412
|
+
when 'email' then 'T.email'
|
|
413
|
+
when 'uuid', 'guid' then 'T.uuid'
|
|
414
|
+
else 'T.string'
|
|
415
|
+
end
|
|
416
|
+
constraints = []
|
|
417
|
+
constraints << "min_length: #{schema['minLength']}" if schema.key?('minLength')
|
|
418
|
+
constraints << "max_length: #{schema['maxLength']}" if schema.key?('maxLength')
|
|
419
|
+
constraints.empty? ? base : "#{base}(#{constraints.join(', ')})"
|
|
420
|
+
when 'integer'
|
|
421
|
+
constraints = []
|
|
422
|
+
constraints << "minimum: #{schema['minimum']}" if schema.key?('minimum')
|
|
423
|
+
constraints << "maximum: #{schema['maximum']}" if schema.key?('maximum')
|
|
424
|
+
constraints.empty? ? 'T.integer' : "T.integer(#{constraints.join(', ')})"
|
|
425
|
+
when 'number'
|
|
426
|
+
constraints = []
|
|
427
|
+
constraints << "minimum: #{schema['minimum']}" if schema.key?('minimum')
|
|
428
|
+
constraints << "maximum: #{schema['maximum']}" if schema.key?('maximum')
|
|
429
|
+
constraints.empty? ? 'T.float' : "T.float(#{constraints.join(', ')})"
|
|
430
|
+
when 'boolean'
|
|
431
|
+
'T.boolean'
|
|
432
|
+
when 'array'
|
|
433
|
+
items = schema_to_t(schema['items'] || {}, spec)
|
|
434
|
+
"T.array(#{items})"
|
|
435
|
+
when 'object', nil
|
|
436
|
+
# If properties exist -> T.hash, otherwise generic object
|
|
437
|
+
props = schema['properties'] || {}
|
|
438
|
+
required = Array(schema['required'])
|
|
439
|
+
if props.any?
|
|
440
|
+
inner = props.map do |k, v|
|
|
441
|
+
t = schema_to_t(v, spec)
|
|
442
|
+
t = "T.optional(#{t})" unless required.include?(k.to_s)
|
|
443
|
+
"\"#{k}\" => #{t}"
|
|
444
|
+
end.join(', ')
|
|
445
|
+
"T.hash({ #{inner} })"
|
|
446
|
+
else
|
|
447
|
+
'T.hash({})'
|
|
448
|
+
end
|
|
449
|
+
else
|
|
450
|
+
'T.hash({})'
|
|
451
|
+
end
|
|
452
|
+
|
|
453
|
+
# Attach original component name if present
|
|
454
|
+
type_expr = "#{type_expr}.with_metadata(component_name: #{component_name.inspect})" if component_name
|
|
455
|
+
|
|
456
|
+
nullable ? "T.optional(#{type_expr})" : type_expr
|
|
457
|
+
end
|
|
458
|
+
|
|
459
|
+
def resolve_schema(schema, spec)
|
|
460
|
+
return nil unless schema
|
|
461
|
+
|
|
462
|
+
if schema.is_a?(Hash) && schema['$ref']
|
|
463
|
+
obj = resolve_ref(schema['$ref'], spec)
|
|
464
|
+
if obj
|
|
465
|
+
name = schema['$ref'].split('/').last
|
|
466
|
+
dup = obj.dup
|
|
467
|
+
dup['__component_name__'] = name
|
|
468
|
+
dup
|
|
469
|
+
else
|
|
470
|
+
schema
|
|
471
|
+
end
|
|
472
|
+
else
|
|
473
|
+
schema
|
|
474
|
+
end
|
|
475
|
+
end
|
|
476
|
+
|
|
477
|
+
def resolve_ref(ref, spec)
|
|
478
|
+
return nil unless ref.is_a?(String)
|
|
479
|
+
|
|
480
|
+
return unless ref.start_with?('#/components/schemas/')
|
|
481
|
+
|
|
482
|
+
name = ref.split('/').last
|
|
483
|
+
(spec.dig('components', 'schemas', name) || {}).dup
|
|
484
|
+
end
|
|
485
|
+
|
|
486
|
+
# Effective security requirements: operation-level overrides global when present
|
|
487
|
+
def applied_security(operation, spec)
|
|
488
|
+
names = []
|
|
489
|
+
security = operation.is_a?(Hash) && operation['security'] ? operation['security'] : spec['security']
|
|
490
|
+
Array(security).each do |sec_req|
|
|
491
|
+
next unless sec_req.is_a?(Hash)
|
|
492
|
+
|
|
493
|
+
names.concat(sec_req.keys)
|
|
494
|
+
end
|
|
495
|
+
names.uniq
|
|
496
|
+
end
|
|
497
|
+
|
|
498
|
+
def default_handler(method, _table, model_class, path_params, success_code)
|
|
499
|
+
case method
|
|
500
|
+
when 'get'
|
|
501
|
+
if path_params.any?
|
|
502
|
+
pk = path_params.first
|
|
503
|
+
<<~RUBY.strip
|
|
504
|
+
rec = #{model_class}.find_by(id: params[:#{pk}])
|
|
505
|
+
halt 404 unless rec
|
|
506
|
+
rec.attributes
|
|
507
|
+
RUBY
|
|
508
|
+
else
|
|
509
|
+
"#{model_class}.limit(25).all.map(&:attributes)"
|
|
510
|
+
end
|
|
511
|
+
when 'post'
|
|
512
|
+
<<~RUBY.strip
|
|
513
|
+
payload = JSON.parse(request.body.read) rescue {}
|
|
514
|
+
model = #{model_class}.create(payload)
|
|
515
|
+
model.attributes
|
|
516
|
+
RUBY
|
|
517
|
+
when 'put', 'patch'
|
|
518
|
+
if path_params.any?
|
|
519
|
+
pk = path_params.first
|
|
520
|
+
<<~RUBY.strip
|
|
521
|
+
payload = JSON.parse(request.body.read) rescue {}
|
|
522
|
+
model = #{model_class}.find_by(id: params[:#{pk}])
|
|
523
|
+
halt 404 unless model
|
|
524
|
+
model.update(payload)
|
|
525
|
+
model.attributes
|
|
526
|
+
RUBY
|
|
527
|
+
else
|
|
528
|
+
<<~RUBY.strip
|
|
529
|
+
payload = JSON.parse(request.body.read) rescue {}
|
|
530
|
+
model = #{model_class}.first
|
|
531
|
+
halt 404 unless model
|
|
532
|
+
model.update(payload)
|
|
533
|
+
model.attributes
|
|
534
|
+
RUBY
|
|
535
|
+
end
|
|
536
|
+
when 'delete'
|
|
537
|
+
if path_params.any?
|
|
538
|
+
pk = path_params.first
|
|
539
|
+
<<~RUBY.strip
|
|
540
|
+
model = #{model_class}.find_by(id: params[:#{pk}])
|
|
541
|
+
halt 404 unless model
|
|
542
|
+
model.destroy
|
|
543
|
+
#{success_code == 204 ? "''" : '{}'}
|
|
544
|
+
RUBY
|
|
545
|
+
else
|
|
546
|
+
<<~RUBY.strip
|
|
547
|
+
model = #{model_class}.first
|
|
548
|
+
halt 404 unless model
|
|
549
|
+
model.destroy
|
|
550
|
+
#{success_code == 204 ? "''" : '{}'}
|
|
551
|
+
RUBY
|
|
552
|
+
end
|
|
553
|
+
else
|
|
554
|
+
'{}'
|
|
555
|
+
end
|
|
556
|
+
end
|
|
557
|
+
|
|
558
|
+
def first_success_code(operation)
|
|
559
|
+
(operation['responses'] || {}).keys.map(&:to_i).grep(200..299).first
|
|
560
|
+
end
|
|
561
|
+
|
|
562
|
+
def to_sinatra_path(openapi_path)
|
|
563
|
+
openapi_path.gsub('{', ':').gsub('}', '')
|
|
564
|
+
end
|
|
565
|
+
|
|
566
|
+
def from_openapi_path(sinatra_path)
|
|
567
|
+
sinatra_path.gsub(/:([a-zA-Z_]\w*)/, '{\1}')
|
|
568
|
+
end
|
|
569
|
+
|
|
570
|
+
def extract_path_params(path)
|
|
571
|
+
path.scan(/:([a-zA-Z_]\w*)/).flatten
|
|
572
|
+
end
|
|
573
|
+
|
|
574
|
+
def collect_tags(spec)
|
|
575
|
+
tags = []
|
|
576
|
+
(spec['paths'] || {}).each_value do |path_item|
|
|
577
|
+
next unless path_item.is_a?(Hash)
|
|
578
|
+
|
|
579
|
+
operation_methods(path_item).each do |m|
|
|
580
|
+
op = path_item[m]
|
|
581
|
+
tags.concat(Array(op['tags'])) if op.is_a?(Hash) && op['tags']
|
|
582
|
+
end
|
|
583
|
+
end
|
|
584
|
+
tags = tags.uniq
|
|
585
|
+
return tags unless tags.empty?
|
|
586
|
+
|
|
587
|
+
# Fallback: derive from first path segment
|
|
588
|
+
(spec['paths'] || {}).keys.map { |p| p.to_s.split('/')[1] }.compact.uniq
|
|
589
|
+
end
|
|
590
|
+
|
|
591
|
+
def operation_methods(path_item)
|
|
592
|
+
%w[get post put patch delete].select { |m| path_item.key?(m) }
|
|
593
|
+
end
|
|
594
|
+
|
|
595
|
+
# String helpers (lightweight, no ActiveSupport)
|
|
596
|
+
def classify(name)
|
|
597
|
+
name.to_s.split(/[_\-\s]/).map { |p| p[0].upcase + p[1..].to_s }.join
|
|
598
|
+
end
|
|
599
|
+
|
|
600
|
+
def underscore(name)
|
|
601
|
+
name.to_s
|
|
602
|
+
.gsub('::', '/')
|
|
603
|
+
.gsub(/([A-Z]+)([A-Z][a-z])/, '\\1_\\2')
|
|
604
|
+
.gsub(/([a-z\d])([A-Z])/, '\\1_\\2')
|
|
605
|
+
.tr('-', '_')
|
|
606
|
+
.downcase
|
|
607
|
+
end
|
|
608
|
+
|
|
609
|
+
def pluralize(word)
|
|
610
|
+
return word if word.end_with?('s')
|
|
611
|
+
return "#{word[0..-2]}ies" if word.end_with?('y')
|
|
612
|
+
|
|
613
|
+
"#{word}s"
|
|
614
|
+
end
|
|
615
|
+
|
|
616
|
+
def singularize(word)
|
|
617
|
+
return word[0..-2] if word.end_with?('s')
|
|
618
|
+
return "#{word[0..-4]}y" if word.end_with?('ies')
|
|
619
|
+
|
|
620
|
+
word
|
|
621
|
+
end
|
|
622
|
+
|
|
623
|
+
def write_file(rel_path, content)
|
|
624
|
+
abs = File.join(output_dir, rel_path)
|
|
625
|
+
FileUtils.mkdir_p(File.dirname(abs))
|
|
626
|
+
File.write(abs, content)
|
|
627
|
+
end
|
|
628
|
+
|
|
629
|
+
def safe_symbol(name)
|
|
630
|
+
n = name.to_s.strip
|
|
631
|
+
n = n.gsub('-', '_')
|
|
632
|
+
n = n.gsub(/[^a-zA-Z0-9_]/, '_')
|
|
633
|
+
# Allow CamelCase/camelCase symbols as-is, just ensure starts with letter
|
|
634
|
+
n = "param_#{n}" unless n =~ /[A-Za-z_]/
|
|
635
|
+
n
|
|
636
|
+
end
|
|
637
|
+
end
|
|
638
|
+
end
|
|
639
|
+
end
|
|
@@ -112,9 +112,7 @@ module RapiTapir
|
|
|
112
112
|
# Check path parameter format
|
|
113
113
|
path.scan(/:(\w+)/).each do |param_match|
|
|
114
114
|
param_name = param_match[0]
|
|
115
|
-
unless param_name.match?(/^[a-zA-Z][a-zA-Z0-9_]*$/)
|
|
116
|
-
@errors << "#{context}: Invalid path parameter name '#{param_name}'"
|
|
117
|
-
end
|
|
115
|
+
@errors << "#{context}: Invalid path parameter name '#{param_name}'" unless param_name.match?(/^[a-zA-Z][a-zA-Z0-9_]*$/)
|
|
118
116
|
end
|
|
119
117
|
end
|
|
120
118
|
|
|
@@ -219,9 +217,7 @@ module RapiTapir
|
|
|
219
217
|
|
|
220
218
|
def validate_hash_schema(schema, context)
|
|
221
219
|
schema.each do |key, value|
|
|
222
|
-
unless key.is_a?(Symbol) || key.is_a?(String)
|
|
223
|
-
@errors << "#{context}: Hash key '#{key}' must be a symbol or string"
|
|
224
|
-
end
|
|
220
|
+
@errors << "#{context}: Hash key '#{key}' must be a symbol or string" unless key.is_a?(Symbol) || key.is_a?(String)
|
|
225
221
|
|
|
226
222
|
validate_type(value, "#{context}.#{key}")
|
|
227
223
|
end
|
|
@@ -303,7 +299,7 @@ module RapiTapir
|
|
|
303
299
|
|
|
304
300
|
def validate_metadata_tags(metadata, context)
|
|
305
301
|
return unless metadata[:tags]
|
|
306
|
-
return if metadata[:tags].is_a?(Array) && metadata[:tags].all?
|
|
302
|
+
return if metadata[:tags].is_a?(Array) && metadata[:tags].all?(String)
|
|
307
303
|
|
|
308
304
|
@errors << "#{context}: Tags must be an array of strings"
|
|
309
305
|
end
|