rapitapir 2.0.0 → 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.
Files changed (63) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop_todo.yml +5 -0
  3. data/CHANGELOG.md +29 -1
  4. data/README.md +23 -4
  5. data/RUBY_WEEKLY_LAUNCH_POST.md +43 -62
  6. data/docs/adr/0001-base-class-strategy.md +33 -0
  7. data/docs/adr/0002-type-shortcut-and-type-system.md +30 -0
  8. data/docs/adr/0003-openapi-source-of-truth.md +32 -0
  9. data/docs/adr/0004-client-generation-typescript-first.md +34 -0
  10. data/docs/adr/0005-auth-architecture.md +32 -0
  11. data/docs/adr/0006-observability-defaults.md +30 -0
  12. data/docs/adr/0007-ai-scope-and-boundaries.md +30 -0
  13. data/docs/adr/README.md +46 -0
  14. data/docs/adr/template.md +24 -0
  15. data/docs/blueprint.md +92 -4
  16. data/docs/implementation-status.md +1 -1
  17. data/examples/auto_derivation_ruby_friendly.rb +44 -113
  18. data/examples/cli/user_api_endpoints.rb +63 -36
  19. data/examples/demo_api.rb +57 -18
  20. data/examples/enterprise_extension_demo.rb +13 -13
  21. data/examples/enterprise_rapitapir_api.rb +81 -93
  22. data/examples/nocodb/nocodb_scaffold/Gemfile +22 -0
  23. data/examples/nocodb/nocodb_scaffold/README.md +10 -0
  24. data/examples/nocodb/nocodb_scaffold/Rakefile +7 -0
  25. data/examples/nocodb/nocodb_scaffold/app/api/app.rb +1139 -0
  26. data/examples/nocodb/nocodb_scaffold/app/models/email_template.rb +6 -0
  27. data/examples/nocodb/nocodb_scaffold/app/models/faq.rb +6 -0
  28. data/examples/nocodb/nocodb_scaffold/app/models/feature.rb +6 -0
  29. data/examples/nocodb/nocodb_scaffold/app/models/idea.rb +6 -0
  30. data/examples/nocodb/nocodb_scaffold/app/models/itinerary_day.rb +6 -0
  31. data/examples/nocodb/nocodb_scaffold/app/models/route.rb +6 -0
  32. data/examples/nocodb/nocodb_scaffold/app/models/subscriber.rb +6 -0
  33. data/examples/nocodb/nocodb_scaffold/app/models/tour.rb +6 -0
  34. data/examples/nocodb/nocodb_scaffold/app/models/webhook.rb +6 -0
  35. data/examples/nocodb/nocodb_scaffold/app/models_loader.rb +3 -0
  36. data/examples/nocodb/nocodb_scaffold/config/database.yml +11 -0
  37. data/examples/nocodb/nocodb_scaffold/config.ru +9 -0
  38. data/examples/nocodb/nocodb_scaffold/db/migrate/20250808152012_create_features.rb +10 -0
  39. data/examples/nocodb/nocodb_scaffold/db/migrate/20250808152013_create_tours.rb +10 -0
  40. data/examples/nocodb/nocodb_scaffold/db/migrate/20250808152014_create_routes.rb +10 -0
  41. data/examples/nocodb/nocodb_scaffold/db/migrate/20250808152015_create_itinerary_days.rb +10 -0
  42. data/examples/nocodb/nocodb_scaffold/db/migrate/20250808152016_create_subscribers.rb +10 -0
  43. data/examples/nocodb/nocodb_scaffold/db/migrate/20250808152017_create_email_templates.rb +10 -0
  44. data/examples/nocodb/nocodb_scaffold/db/migrate/20250808152018_create_faqs.rb +10 -0
  45. data/examples/nocodb/nocodb_scaffold/db/migrate/20250808152019_create_webhooks.rb +10 -0
  46. data/examples/nocodb/nocodb_scaffold/db/migrate/20250808152020_create_ideas.rb +10 -0
  47. data/examples/nocodb/swagger.json +4378 -0
  48. data/examples/production_ready_example.rb +78 -58
  49. data/examples/simple_demo_api.rb +40 -7
  50. data/examples/user_api.rb +130 -132
  51. data/lib/rapitapir/auth/oauth2.rb +1 -1
  52. data/lib/rapitapir/cli/command.rb +30 -4
  53. data/lib/rapitapir/cli/scaffold_generator.rb +639 -0
  54. data/lib/rapitapir/cli/validator.rb +1 -1
  55. data/lib/rapitapir/core/enhanced_endpoint.rb +1 -1
  56. data/lib/rapitapir/dsl/enhanced_structures.rb +1 -1
  57. data/lib/rapitapir/openapi/schema_generator.rb +46 -2
  58. data/lib/rapitapir/server/enhanced_rack_adapter.rb +11 -8
  59. data/lib/rapitapir/server/middleware.rb +2 -2
  60. data/lib/rapitapir/types/auto_derivation.rb +2 -4
  61. data/lib/rapitapir/version.rb +1 -1
  62. data/rapitapir.gemspec +3 -2
  63. metadata +70 -8
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative 'lib/rapitapir'
3
+ require_relative '../lib/rapitapir'
4
4
 
5
5
  puts '🌟 RapiTapir Production-Ready API Example'
6
6
  puts '=' * 45
@@ -25,71 +25,91 @@ Error = RapiTapir::Schema.define do
25
25
  end
26
26
 
27
27
  # Define a complete REST API using the fluent DSL
28
- RapiTapir.namespace('/api/v1/users') do
29
- bearer_auth('API Token Required')
30
- tags('users', 'rest-api')
28
+ #
29
+ # GET /api/v1/users - List users with pagination
30
+ RapiTapir.get('/api/v1/users')
31
+ .bearer_auth('API Token Required')
32
+ .tags('users', 'rest-api')
33
+ .summary('List users')
34
+ .description('Retrieve paginated list of users')
35
+ .query(:page, RapiTapir::Types.integer, min: 1, default: 1, description: 'Page number')
36
+ .query(:per_page, RapiTapir::Types.integer, min: 1, max: 100, default: 20, description: 'Items per page')
37
+ .query(:search, RapiTapir::Types.optional(RapiTapir::Types.string), description: 'Search users by name')
38
+ .ok(RapiTapir::Types.array(User), description: 'List of users')
39
+ .bad_request(Error, description: 'Invalid query parameters')
40
+ .unauthorized(Error, description: 'Invalid or missing token')
41
+ .forbidden(Error, description: 'Insufficient permissions')
42
+ .internal_server_error(Error, description: 'Internal server error')
43
+ .build
31
44
 
32
- error_responses do
33
- unauthorized(401, Error, description: 'Invalid or missing token')
34
- forbidden(403, Error, description: 'Insufficient permissions')
35
- server_error(500, Error, description: 'Internal server error')
36
- end
45
+ # GET /api/v1/users/:id - Get specific user
46
+ RapiTapir.get('/api/v1/users/:id')
47
+ .bearer_auth('API Token Required')
48
+ .tags('users', 'rest-api')
49
+ .summary('Get user by ID')
50
+ .description('Retrieve a specific user by their unique identifier')
51
+ .path_param(:id, RapiTapir::Types.uuid, description: 'User unique identifier')
52
+ .ok(User, description: 'User details')
53
+ .not_found(Error, description: 'User not found')
54
+ .unauthorized(Error, description: 'Invalid or missing token')
55
+ .forbidden(Error, description: 'Insufficient permissions')
56
+ .internal_server_error(Error, description: 'Internal server error')
57
+ .build
37
58
 
38
- # GET /api/v1/users - List users with pagination
39
- get('/')
40
- .summary('List users')
41
- .description('Retrieve paginated list of users')
42
- .query(:page, :integer, min: 1, default: 1, description: 'Page number')
43
- .query(:per_page, :integer, min: 1, max: 100, default: 20, description: 'Items per page')
44
- .query(:search, :string, required: false, description: 'Search users by name')
45
- .ok(RapiTapir::Types.array(User), description: 'List of users')
46
- .bad_request(Error, description: 'Invalid query parameters')
59
+ # POST /api/v1/users - Create new user
60
+ RapiTapir.post('/api/v1/users')
61
+ .bearer_auth('API Token Required')
62
+ .tags('users', 'rest-api')
63
+ .summary('Create user')
64
+ .description('Create a new user account')
65
+ .json_body(CreateUser, description: 'User data')
66
+ .requires_scope('users:write')
67
+ .created(User, description: 'User created successfully')
68
+ .bad_request(Error, description: 'Invalid user data')
69
+ .unprocessable_entity(Error, description: 'Validation failed')
70
+ .unauthorized(Error, description: 'Invalid or missing token')
71
+ .forbidden(Error, description: 'Insufficient permissions')
72
+ .internal_server_error(Error, description: 'Internal server error')
73
+ .build
47
74
 
48
- # GET /api/v1/users/{id} - Get specific user
49
- get('/{id}')
50
- .summary('Get user by ID')
51
- .description('Retrieve a specific user by their unique identifier')
52
- .path_param(:id, :uuid, description: 'User unique identifier')
53
- .ok(User, description: 'User details')
54
- .not_found(Error, description: 'User not found')
75
+ # PUT /api/v1/users/:id - Update user
76
+ RapiTapir.put('/api/v1/users/:id')
77
+ .bearer_auth('API Token Required')
78
+ .tags('users', 'rest-api')
79
+ .summary('Update user')
80
+ .description('Update an existing user\'s information')
81
+ .path_param(:id, RapiTapir::Types.uuid, description: 'User unique identifier')
82
+ .json_body(CreateUser, description: 'Updated user data')
83
+ .requires_scope('users:write')
84
+ .ok(User, description: 'User updated successfully')
85
+ .not_found(Error, description: 'User not found')
86
+ .unprocessable_entity(Error, description: 'Validation failed')
87
+ .unauthorized(Error, description: 'Invalid or missing token')
88
+ .forbidden(Error, description: 'Insufficient permissions')
89
+ .internal_server_error(Error, description: 'Internal server error')
90
+ .build
55
91
 
56
- # POST /api/v1/users - Create new user
57
- post('/')
58
- .summary('Create user')
59
- .description('Create a new user account')
60
- .json_body(CreateUser, description: 'User data')
61
- .requires_scope('users:write')
62
- .created(User, description: 'User created successfully')
63
- .bad_request(Error, description: 'Invalid user data')
64
- .unprocessable_entity(Error, description: 'Validation failed')
65
-
66
- # PUT /api/v1/users/{id} - Update user
67
- put('/{id}')
68
- .summary('Update user')
69
- .description("Update an existing user's information")
70
- .path_param(:id, :uuid, description: 'User unique identifier')
71
- .json_body(CreateUser, description: 'Updated user data')
72
- .requires_scope('users:write')
73
- .ok(User, description: 'User updated successfully')
74
- .not_found(Error, description: 'User not found')
75
- .unprocessable_entity(Error, description: 'Validation failed')
76
-
77
- # DELETE /api/v1/users/{id} - Delete user
78
- delete('/{id}')
79
- .summary('Delete user')
80
- .description('Permanently delete a user account')
81
- .path_param(:id, :uuid, description: 'User unique identifier')
82
- .requires_scope('users:delete')
83
- .no_content(description: 'User deleted successfully')
84
- .not_found(Error, description: 'User not found')
85
- end
92
+ # DELETE /api/v1/users/:id - Delete user
93
+ RapiTapir.delete('/api/v1/users/:id')
94
+ .bearer_auth('API Token Required')
95
+ .tags('users', 'rest-api')
96
+ .summary('Delete user')
97
+ .description('Permanently delete a user account')
98
+ .path_param(:id, RapiTapir::Types.uuid, description: 'User unique identifier')
99
+ .requires_scope('users:delete')
100
+ .no_content(description: 'User deleted successfully')
101
+ .not_found(Error, description: 'User not found')
102
+ .unauthorized(Error, description: 'Invalid or missing token')
103
+ .forbidden(Error, description: 'Insufficient permissions')
104
+ .internal_server_error(Error, description: 'Internal server error')
105
+ .build
86
106
 
87
107
  puts '✅ Defined complete REST API with 5 endpoints'
88
108
  puts ' - GET /api/v1/users (list with pagination)'
89
- puts ' - GET /api/v1/users/{id} (get by ID)'
109
+ puts ' - GET /api/v1/users/:id (get by ID)'
90
110
  puts ' - POST /api/v1/users (create)'
91
- puts ' - PUT /api/v1/users/{id} (update)'
92
- puts ' - DELETE /api/v1/users/{id} (delete)'
111
+ puts ' - PUT /api/v1/users/:id (update)'
112
+ puts ' - DELETE /api/v1/users/:id (delete)'
93
113
 
94
114
  puts "\n✅ Features implemented:"
95
115
  puts ' - Bearer token authentication'
@@ -1,18 +1,51 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative 'lib/rapitapir'
4
- include RapiTapir::DSL
3
+ require_relative '../lib/rapitapir'
4
+
5
+ USER_LIST_TYPE = RapiTapir::Types.array(
6
+ RapiTapir::Types.hash({
7
+ 'id' => RapiTapir::Types.integer,
8
+ 'name' => RapiTapir::Types.string,
9
+ 'email' => RapiTapir::Types.string
10
+ })
11
+ )
12
+
13
+ USER_TYPE = RapiTapir::Types.hash({
14
+ 'id' => RapiTapir::Types.integer,
15
+ 'name' => RapiTapir::Types.string,
16
+ 'email' => RapiTapir::Types.string
17
+ })
18
+
19
+ CREATE_USER_TYPE = RapiTapir::Types.hash({
20
+ 'name' => RapiTapir::Types.string,
21
+ 'email' => RapiTapir::Types.string,
22
+ 'password' => RapiTapir::Types.string
23
+ })
24
+
25
+ ERROR_TYPE = RapiTapir::Types.hash({
26
+ 'error' => RapiTapir::Types.string,
27
+ 'message' => RapiTapir::Types.string
28
+ })
29
+
30
+ VALIDATION_ERROR_TYPE = RapiTapir::Types.hash({
31
+ 'error' => RapiTapir::Types.string,
32
+ 'validation_errors' => RapiTapir::Types.array(RapiTapir::Types.string)
33
+ })
5
34
 
6
35
  # Simple API endpoints for demonstration
7
36
  RapiTapir.get('/users')
8
- .out(json_body([{ id: :integer, name: :string, email: :string }]))
37
+ .ok(USER_LIST_TYPE)
9
38
  .summary('List all users')
10
39
  .description('Retrieve a paginated list of all users in the system')
40
+ .build
11
41
 
12
42
  RapiTapir.post('/users')
13
- .in(body({ name: :string, email: :string, password: :string }))
14
- .out(status_code(201), json_body({ id: :integer, name: :string, email: :string }))
15
- .error_out(400, json_body({ error: :string, validation_errors: [:string] }))
16
- .error_out(422, json_body({ error: :string, message: :string }))
43
+ .json_body(CREATE_USER_TYPE)
44
+ .created(USER_TYPE)
45
+ .bad_request(VALIDATION_ERROR_TYPE)
46
+ .unprocessable_entity(ERROR_TYPE)
17
47
  .summary('Create new user')
18
48
  .description('Create a new user account with the provided information')
49
+ .build
50
+
51
+ puts "✅ Defined #{RapiTapir.endpoints.size} endpoints"
data/examples/user_api.rb CHANGED
@@ -5,180 +5,178 @@ require_relative '../lib/rapitapir'
5
5
 
6
6
  # Example: User Management API
7
7
  module UserAPI
8
- extend RapiTapir::DSL
8
+ # Shared type definitions
9
+ USER_TYPE = RapiTapir::Types.hash({
10
+ 'id' => RapiTapir::Types.integer,
11
+ 'name' => RapiTapir::Types.string,
12
+ 'email' => RapiTapir::Types.string
13
+ })
14
+
15
+ USER_DETAIL_TYPE = RapiTapir::Types.hash({
16
+ 'id' => RapiTapir::Types.integer,
17
+ 'name' => RapiTapir::Types.string,
18
+ 'email' => RapiTapir::Types.string,
19
+ 'created_at' => RapiTapir::Types.datetime
20
+ })
21
+
22
+ CREATE_USER_TYPE = RapiTapir::Types.hash({
23
+ 'name' => RapiTapir::Types.string,
24
+ 'email' => RapiTapir::Types.string,
25
+ 'password' => RapiTapir::Types.string
26
+ })
27
+
28
+ LOGIN_TYPE = RapiTapir::Types.hash({
29
+ 'email' => RapiTapir::Types.string,
30
+ 'password' => RapiTapir::Types.string
31
+ })
32
+
33
+ LOGIN_RESULT_TYPE = RapiTapir::Types.hash({
34
+ 'user' => USER_TYPE,
35
+ 'token' => RapiTapir::Types.string,
36
+ 'expires_at' => RapiTapir::Types.datetime
37
+ })
38
+
39
+ ERROR_TYPE = RapiTapir::Types.hash({
40
+ 'error' => RapiTapir::Types.string
41
+ })
42
+
43
+ VALIDATION_ERROR_TYPE = RapiTapir::Types.hash({
44
+ 'error' => RapiTapir::Types.string,
45
+ 'validation_errors' => RapiTapir::Types.array(
46
+ RapiTapir::Types.hash({
47
+ 'field' => RapiTapir::Types.string,
48
+ 'message' => RapiTapir::Types.string
49
+ })
50
+ )
51
+ })
52
+
53
+ USER_LIST_TYPE = RapiTapir::Types.hash({
54
+ 'users' => RapiTapir::Types.array(USER_TYPE),
55
+ 'total' => RapiTapir::Types.integer,
56
+ 'page' => RapiTapir::Types.integer,
57
+ 'limit' => RapiTapir::Types.integer
58
+ })
9
59
 
10
60
  # GET /users - List all users
11
61
  LIST_USERS = RapiTapir.get('/users')
12
- .in(query(:page, :integer, optional: true))
13
- .in(query(:limit, :integer, optional: true))
14
- .in(query(:search, :string, optional: true))
15
- .in(header(:authorization, :string))
16
- .out(status_code(200))
17
- .out(json_body({
18
- users: [{ id: :integer, name: :string, email: :string }],
19
- total: :integer,
20
- page: :integer,
21
- limit: :integer
22
- }))
23
- .error_out(401, json_body({ error: :string }))
24
- .error_out(500, json_body({ error: :string }))
62
+ .query(:page, RapiTapir::Types.optional(RapiTapir::Types.integer))
63
+ .query(:limit, RapiTapir::Types.optional(RapiTapir::Types.integer))
64
+ .query(:search, RapiTapir::Types.optional(RapiTapir::Types.string))
65
+ .header(:authorization, RapiTapir::Types.string)
66
+ .ok(USER_LIST_TYPE)
67
+ .unauthorized(ERROR_TYPE)
68
+ .internal_server_error(ERROR_TYPE)
25
69
  .description('Retrieve a paginated list of users')
26
70
  .summary('List users')
27
- .tag('users')
71
+ .tags('users')
72
+ .build
28
73
 
29
74
  # GET /users/:id - Get specific user
30
75
  GET_USER = RapiTapir.get('/users/:id')
31
- .in(path_param(:id, :integer))
32
- .in(header(:authorization, :string))
33
- .out(status_code(200))
34
- .out(json_body({ id: :integer, name: :string, email: :string, created_at: :datetime }))
35
- .error_out(401, json_body({ error: :string }))
36
- .error_out(404, json_body({ error: :string }))
37
- .error_out(500, json_body({ error: :string }))
76
+ .path_param(:id, RapiTapir::Types.integer)
77
+ .header(:authorization, RapiTapir::Types.string)
78
+ .ok(USER_DETAIL_TYPE)
79
+ .unauthorized(ERROR_TYPE)
80
+ .not_found(ERROR_TYPE)
81
+ .internal_server_error(ERROR_TYPE)
38
82
  .description('Retrieve a specific user by ID')
39
83
  .summary('Get user')
40
- .tag('users')
84
+ .tags('users')
85
+ .build
41
86
 
42
87
  # POST /users - Create new user
43
88
  CREATE_USER = RapiTapir.post('/users')
44
- .in(header(:authorization, :string))
45
- .in(header(:'content-type', :string))
46
- .in(body({ name: :string, email: :string, password: :string }))
47
- .out(status_code(201))
48
- .out(json_body({ id: :integer, name: :string, email: :string, created_at: :datetime }))
49
- .error_out(400, json_body({ error: :string, details: :string }))
50
- .error_out(401, json_body({ error: :string }))
51
- .error_out(422, json_body({ error: :string,
52
- validation_errors: [{ field: :string, message: :string }] }))
53
- .error_out(500, json_body({ error: :string }))
89
+ .header(:authorization, RapiTapir::Types.string)
90
+ .json_body(CREATE_USER_TYPE)
91
+ .created(USER_DETAIL_TYPE)
92
+ .bad_request(ERROR_TYPE)
93
+ .unauthorized(ERROR_TYPE)
94
+ .unprocessable_entity(VALIDATION_ERROR_TYPE)
95
+ .internal_server_error(ERROR_TYPE)
54
96
  .description('Create a new user account')
55
97
  .summary('Create user')
56
- .tag('users')
98
+ .tags('users')
99
+ .build
57
100
 
58
101
  # PUT /users/:id - Update user
59
102
  UPDATE_USER = RapiTapir.put('/users/:id')
60
- .in(path_param(:id, :integer))
61
- .in(header(:authorization, :string))
62
- .in(header(:'content-type', :string))
63
- .in(body({ name: :string, email: :string }))
64
- .out(status_code(200))
65
- .out(json_body({ id: :integer, name: :string, email: :string, updated_at: :datetime }))
66
- .error_out(400, json_body({ error: :string, details: :string }))
67
- .error_out(401, json_body({ error: :string }))
68
- .error_out(404, json_body({ error: :string }))
69
- .error_out(422, json_body({ error: :string,
70
- validation_errors: [{ field: :string, message: :string }] }))
71
- .error_out(500, json_body({ error: :string }))
103
+ .path_param(:id, RapiTapir::Types.integer)
104
+ .header(:authorization, RapiTapir::Types.string)
105
+ .json_body(RapiTapir::Types.hash({
106
+ 'name' => RapiTapir::Types.string,
107
+ 'email' => RapiTapir::Types.string
108
+ }))
109
+ .ok(RapiTapir::Types.hash({
110
+ 'id' => RapiTapir::Types.integer,
111
+ 'name' => RapiTapir::Types.string,
112
+ 'email' => RapiTapir::Types.string,
113
+ 'updated_at' => RapiTapir::Types.datetime
114
+ }))
115
+ .bad_request(ERROR_TYPE)
116
+ .unauthorized(ERROR_TYPE)
117
+ .not_found(ERROR_TYPE)
118
+ .unprocessable_entity(VALIDATION_ERROR_TYPE)
119
+ .internal_server_error(ERROR_TYPE)
72
120
  .description('Update an existing user')
73
121
  .summary('Update user')
74
- .tag('users')
122
+ .tags('users')
123
+ .build
75
124
 
76
125
  # DELETE /users/:id - Delete user
77
126
  DELETE_USER = RapiTapir.delete('/users/:id')
78
- .in(path_param(:id, :integer))
79
- .in(header(:authorization, :string))
80
- .out(status_code(204))
81
- .error_out(401, json_body({ error: :string }))
82
- .error_out(404, json_body({ error: :string }))
83
- .error_out(500, json_body({ error: :string }))
127
+ .path_param(:id, RapiTapir::Types.integer)
128
+ .header(:authorization, RapiTapir::Types.string)
129
+ .no_content
130
+ .unauthorized(ERROR_TYPE)
131
+ .not_found(ERROR_TYPE)
132
+ .internal_server_error(ERROR_TYPE)
84
133
  .description('Delete a user account')
85
134
  .summary('Delete user')
86
- .tag('users')
135
+ .tags('users')
136
+ .build
87
137
 
88
138
  # POST /users/login - User authentication
89
139
  LOGIN_USER = RapiTapir.post('/users/login')
90
- .in(header(:'content-type', :string))
91
- .in(body({ email: :string, password: :string }))
92
- .out(status_code(200))
93
- .out(json_body({
94
- user: { id: :integer, name: :string, email: :string },
95
- token: :string,
96
- expires_at: :datetime
97
- }))
98
- .error_out(400, json_body({ error: :string }))
99
- .error_out(401, json_body({ error: :string }))
100
- .error_out(500, json_body({ error: :string }))
140
+ .json_body(LOGIN_TYPE)
141
+ .ok(LOGIN_RESULT_TYPE)
142
+ .bad_request(ERROR_TYPE)
143
+ .unauthorized(ERROR_TYPE)
144
+ .internal_server_error(ERROR_TYPE)
101
145
  .description('Authenticate user and return access token')
102
146
  .summary('User login')
103
- .tag('authentication')
147
+ .tags('authentication')
148
+ .build
104
149
 
105
150
  # Example usage and validation
106
151
  def self.demo
107
152
  puts '=== RapiTapir User API Demo ==='
108
153
  puts
109
154
 
110
- # Example 1: Valid user creation
111
- puts '1. Valid user creation:'
112
- input_data = {
113
- body: { name: 'John Doe', email: 'john@example.com', password: 'secret123' }
114
- }
115
- output_data = {
116
- id: 1,
117
- name: 'John Doe',
118
- email: 'john@example.com',
119
- created_at: DateTime.now
120
- }
121
-
122
- begin
123
- CREATE_USER.validate!(input_data, output_data)
124
- puts '✓ Validation passed'
125
- rescue StandardError => e
126
- puts "✗ Validation failed: #{e.message}"
127
- end
155
+ # Example 1: Endpoint structure
156
+ puts '1. Endpoint structure:'
157
+ puts ' GET /users:'
158
+ puts " Method: #{LIST_USERS.method} #{LIST_USERS.path}"
159
+ puts " Inputs: #{LIST_USERS.inputs.length}"
160
+ puts " Outputs: #{LIST_USERS.outputs.length}"
161
+ puts " Error responses: #{LIST_USERS.errors.length}"
128
162
  puts
129
163
 
130
- # Example 2: Invalid user creation (missing required field)
131
- puts '2. Invalid user creation (missing password):'
132
- invalid_input = {
133
- body: { name: 'John Doe', email: 'john@example.com' }
134
- }
135
-
136
- begin
137
- CREATE_USER.validate!(invalid_input, output_data)
138
- puts '✓ Validation passed'
139
- rescue StandardError => e
140
- puts "✗ Validation failed: #{e.message}"
141
- end
164
+ # Example 2: Endpoint metadata
165
+ puts '2. Endpoint metadata:'
166
+ puts " Summary: #{GET_USER.metadata[:summary]}"
167
+ puts " Description: #{GET_USER.metadata[:description]}"
142
168
  puts
143
169
 
144
- # Example 3: Valid user listing
145
- puts '3. Valid user listing:'
146
- list_input = {}
147
- list_output = {
148
- users: [
149
- { id: 1, name: 'John Doe', email: 'john@example.com' },
150
- { id: 2, name: 'Jane Smith', email: 'jane@example.com' }
151
- ],
152
- total: 2,
153
- page: 1,
154
- limit: 10
155
- }
156
-
157
- begin
158
- LIST_USERS.validate!(list_input, list_output)
159
- puts '✓ Validation passed'
160
- rescue StandardError => e
161
- puts "✗ Validation failed: #{e.message}"
162
- end
163
- puts
170
+ # Example 3: Built-in validation
171
+ puts '3. Endpoint validation:'
172
+ CREATE_USER.validate!
173
+ puts ' CREATE_USER passed built-in validation ✓'
164
174
 
165
- # Example 4: Endpoint metadata
166
- puts '4. Endpoint metadata:'
167
- puts 'GET /users:'
168
- puts " Description: #{GET_USER.metadata[:description]}"
169
- puts " Summary: #{GET_USER.metadata[:summary]}"
170
- puts " Tag: #{GET_USER.metadata[:tag]}"
171
- puts " Inputs: #{GET_USER.inputs.length}"
172
- puts " Outputs: #{GET_USER.outputs.length}"
173
- puts " Error responses: #{GET_USER.errors.length}"
175
+ # Example 4: All endpoints summary
174
176
  puts
175
-
176
- # Example 5: Serialization
177
- puts '5. Output serialization:'
178
- json_output = CREATE_USER.outputs.find { |o| o.kind == :json }
179
- if json_output
180
- serialized = json_output.serialize(output_data)
181
- puts "JSON: #{serialized}"
177
+ puts '4. All endpoints:'
178
+ [LIST_USERS, GET_USER, CREATE_USER, UPDATE_USER, DELETE_USER, LOGIN_USER].each do |ep|
179
+ puts " #{ep.method.to_s.ljust(6).upcase} #{ep.path}"
182
180
  end
183
181
  puts
184
182
 
@@ -90,8 +90,8 @@ module RapiTapir
90
90
  JWT.decode(
91
91
  token,
92
92
  nil,
93
- true,
94
93
  {
94
+ verify_signature: true,
95
95
  algorithm: @algorithm,
96
96
  iss: domain_url,
97
97
  verify_iss: true,
@@ -171,10 +171,13 @@ module RapiTapir
171
171
  when 'docs'
172
172
  docs_type = args.shift || 'html'
173
173
  generate_docs(docs_type)
174
+ when 'scaffold'
175
+ generate_scaffold(args)
174
176
  when 'mcp'
175
177
  generate_mcp
176
178
  else
177
179
  puts "Error: Unknown generation type: #{type}"
180
+ # Keep this list stable for backward compatibility with existing tests
178
181
  puts 'Available types: openapi, client, docs, mcp'
179
182
  exit 1
180
183
  end
@@ -404,6 +407,31 @@ module RapiTapir
404
407
  end
405
408
  end
406
409
 
410
+ def generate_scaffold(_args)
411
+ unless @options[:input]
412
+ puts 'Error: --input must point to an OpenAPI/Swagger JSON file'
413
+ exit 1
414
+ end
415
+
416
+ output_dir = @options[:output] || 'rapitapir_sinatra_app'
417
+
418
+ begin
419
+ require_relative 'scaffold_generator'
420
+ generator = RapiTapir::CLI::ScaffoldGenerator.new(
421
+ openapi_path: @options[:input],
422
+ output_dir: output_dir,
423
+ config: @options[:config]
424
+ )
425
+ generator.generate!
426
+ puts "Scaffold generated at #{output_dir}"
427
+ puts "Next steps:\n 1) cd #{output_dir}\n 2) bundle install\n 3) bundle exec rake db:create db:migrate\n 4) bundle exec rackup"
428
+ rescue StandardError => e
429
+ puts "Error generating scaffold: #{e.message}"
430
+ puts e.backtrace.first(5).join("\n")
431
+ exit 1
432
+ end
433
+ end
434
+
407
435
  def create_markdown_generator(endpoints)
408
436
  require_relative '../docs/markdown_generator'
409
437
  RapiTapir::Docs::MarkdownGenerator.new(
@@ -500,9 +528,8 @@ module RapiTapir
500
528
  file_dir = File.dirname(File.expand_path(file_path))
501
529
  Dir.chdir(file_dir) do
502
530
  # Execute the file content using load instead of eval for safety
503
- # rubocop:disable Security/Eval
531
+ # rubocop:disable-next Security/Eval
504
532
  eval(content, TOPLEVEL_BINDING, file_path)
505
- # rubocop:enable Security/Eval
506
533
  end
507
534
 
508
535
  find_endpoints_in_variables(content)
@@ -540,9 +567,8 @@ module RapiTapir
540
567
 
541
568
  return nil unless global_endpoints_var
542
569
 
543
- # rubocop:disable Security/Eval
570
+ # rubocop:disable-next Security/Eval
544
571
  endpoints = eval(global_endpoints_var.to_s)
545
- # rubocop:enable Security/Eval
546
572
  normalize_endpoints_array(endpoints)
547
573
  end
548
574