apipie-rails 0.5.20 → 1.2.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (179) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/build.yml +12 -21
  3. data/.github/workflows/rubocop-challenger.yml +28 -0
  4. data/.github/workflows/rubocop.yml +18 -0
  5. data/.gitignore +1 -0
  6. data/.rubocop.yml +128 -0
  7. data/.rubocop_todo.yml +2062 -0
  8. data/.vscode/settings.json +3 -0
  9. data/CHANGELOG.md +144 -0
  10. data/Gemfile +20 -0
  11. data/README.rst +106 -15
  12. data/Rakefile +0 -5
  13. data/apipie-rails.gemspec +18 -9
  14. data/app/controllers/apipie/apipies_controller.rb +14 -29
  15. data/app/helpers/apipie_helper.rb +1 -1
  16. data/app/public/apipie/javascripts/bundled/bootstrap-collapse.js +70 -41
  17. data/app/public/apipie/javascripts/bundled/bootstrap.js +1033 -479
  18. data/app/public/apipie/javascripts/bundled/jquery.js +5 -5
  19. data/app/public/apipie/stylesheets/bundled/bootstrap-responsive.min.css +9 -12
  20. data/app/public/apipie/stylesheets/bundled/bootstrap.min.css +9 -689
  21. data/app/views/apipie/apipies/_deprecation.html.erb +16 -0
  22. data/app/views/apipie/apipies/_params.html.erb +7 -1
  23. data/config/locales/en.yml +8 -0
  24. data/config/locales/ko.yml +31 -0
  25. data/gemfiles/Gemfile.tools +9 -0
  26. data/lib/apipie/apipie_module.rb +7 -7
  27. data/lib/apipie/application.rb +132 -97
  28. data/lib/apipie/configuration.rb +43 -33
  29. data/lib/apipie/dsl_definition.rb +39 -27
  30. data/lib/apipie/error_description.rb +3 -3
  31. data/lib/apipie/errors.rb +16 -16
  32. data/lib/apipie/extractor/collector.rb +4 -5
  33. data/lib/apipie/extractor/recorder.rb +33 -6
  34. data/lib/apipie/extractor/writer.rb +14 -14
  35. data/lib/apipie/extractor.rb +6 -9
  36. data/lib/apipie/generator/config.rb +12 -0
  37. data/lib/apipie/generator/generator.rb +2 -0
  38. data/lib/apipie/generator/swagger/computed_interface_id.rb +23 -0
  39. data/lib/apipie/generator/swagger/config.rb +80 -0
  40. data/lib/apipie/generator/swagger/context.rb +38 -0
  41. data/lib/apipie/generator/swagger/method_description/api_decorator.rb +20 -0
  42. data/lib/apipie/generator/swagger/method_description/api_schema_service.rb +89 -0
  43. data/lib/apipie/generator/swagger/method_description/decorator.rb +22 -0
  44. data/lib/apipie/generator/swagger/method_description/parameters_service.rb +139 -0
  45. data/lib/apipie/generator/swagger/method_description/response_schema_service.rb +46 -0
  46. data/lib/apipie/generator/swagger/method_description/response_service.rb +58 -0
  47. data/lib/apipie/generator/swagger/method_description.rb +2 -0
  48. data/lib/apipie/generator/swagger/operation_id.rb +51 -0
  49. data/lib/apipie/generator/swagger/param_description/builder.rb +105 -0
  50. data/lib/apipie/generator/swagger/param_description/composite.rb +119 -0
  51. data/lib/apipie/generator/swagger/param_description/description.rb +15 -0
  52. data/lib/apipie/generator/swagger/param_description/in.rb +37 -0
  53. data/lib/apipie/generator/swagger/param_description/name.rb +18 -0
  54. data/lib/apipie/generator/swagger/param_description/path_params_composite.rb +61 -0
  55. data/lib/apipie/generator/swagger/param_description/referenced_composite.rb +36 -0
  56. data/lib/apipie/generator/swagger/param_description/type.rb +115 -0
  57. data/lib/apipie/generator/swagger/param_description.rb +18 -0
  58. data/lib/apipie/generator/swagger/path_decorator.rb +36 -0
  59. data/lib/apipie/generator/swagger/referenced_definitions.rb +17 -0
  60. data/lib/apipie/generator/swagger/resource_description_collection.rb +30 -0
  61. data/lib/apipie/generator/swagger/resource_description_composite.rb +56 -0
  62. data/lib/apipie/generator/swagger/schema.rb +63 -0
  63. data/lib/apipie/generator/swagger/swagger.rb +2 -0
  64. data/lib/apipie/generator/swagger/type.rb +16 -0
  65. data/lib/apipie/generator/swagger/type_extractor.rb +51 -0
  66. data/lib/apipie/generator/swagger/warning.rb +74 -0
  67. data/lib/apipie/generator/swagger/warning_writer.rb +54 -0
  68. data/lib/apipie/helpers.rb +3 -3
  69. data/lib/apipie/markup.rb +9 -8
  70. data/lib/apipie/method_description/api.rb +12 -0
  71. data/lib/apipie/method_description/apis_service.rb +82 -0
  72. data/lib/apipie/method_description.rb +12 -56
  73. data/lib/apipie/param_description/deprecation.rb +24 -0
  74. data/lib/apipie/param_description.rb +57 -24
  75. data/lib/apipie/resource_description.rb +42 -14
  76. data/lib/apipie/response_description.rb +3 -3
  77. data/lib/apipie/response_description_adapter.rb +10 -8
  78. data/lib/apipie/routing.rb +1 -1
  79. data/lib/apipie/rspec/response_validation_helper.rb +3 -3
  80. data/lib/apipie/static_dispatcher.rb +3 -1
  81. data/lib/apipie/swagger_generator.rb +28 -691
  82. data/lib/apipie/validator.rb +40 -10
  83. data/lib/apipie/version.rb +1 -1
  84. data/lib/apipie-rails.rb +36 -5
  85. data/lib/generators/apipie/install/install_generator.rb +1 -1
  86. data/lib/generators/apipie/views_generator.rb +1 -1
  87. data/lib/tasks/apipie.rake +35 -30
  88. data/spec/controllers/api/v2/architectures_controller_spec.rb +10 -3
  89. data/spec/controllers/api/v2/empty_middle_controller_spec.rb +23 -0
  90. data/spec/controllers/api/v2/nested/resources_controller_spec.rb +18 -2
  91. data/spec/controllers/api/v2/sub/footguns_controller_spec.rb +19 -0
  92. data/spec/controllers/included_param_group_controller_spec.rb +13 -0
  93. data/spec/{lib/swagger/response_validation_spec.rb → controllers/pets_controller_spec.rb} +26 -32
  94. data/spec/controllers/users_controller_spec.rb +47 -6
  95. data/spec/dummy/Rakefile +1 -1
  96. data/spec/dummy/app/controllers/api/v2/architectures_controller.rb +2 -1
  97. data/spec/dummy/app/controllers/api/v2/base_controller.rb +6 -0
  98. data/spec/dummy/app/controllers/api/v2/empty_middle_controller.rb +14 -0
  99. data/spec/dummy/app/controllers/api/v2/nested/resources_controller.rb +2 -2
  100. data/spec/dummy/app/controllers/api/v2/sub/footguns_controller.rb +30 -0
  101. data/spec/dummy/app/controllers/concerns_controller.rb +1 -1
  102. data/spec/dummy/app/controllers/{concerns/extending_concern.rb → extending_concern.rb} +0 -2
  103. data/spec/dummy/app/controllers/included_param_group_controller.rb +19 -0
  104. data/spec/dummy/app/controllers/overridden_concerns_controller.rb +2 -2
  105. data/spec/dummy/app/controllers/pets_controller.rb +4 -4
  106. data/spec/dummy/app/controllers/pets_using_self_describing_classes_controller.rb +2 -2
  107. data/spec/dummy/app/controllers/{concerns/sample_controller.rb → sample_controller.rb} +0 -2
  108. data/spec/dummy/app/controllers/twitter_example_controller.rb +2 -2
  109. data/spec/dummy/app/controllers/users_controller.rb +17 -5
  110. data/spec/dummy/app/helpers/random_param_group.rb +8 -0
  111. data/spec/dummy/components/test_engine/test_engine.gemspec +1 -1
  112. data/spec/dummy/config/application.rb +2 -5
  113. data/spec/dummy/config/boot.rb +2 -2
  114. data/spec/dummy/config/environment.rb +1 -1
  115. data/spec/dummy/config/environments/development.rb +0 -3
  116. data/spec/dummy/config/environments/production.rb +0 -3
  117. data/spec/dummy/config/environments/test.rb +0 -5
  118. data/spec/dummy/config/initializers/apipie.rb +2 -2
  119. data/spec/dummy/config/routes.rb +8 -0
  120. data/spec/dummy/config.ru +1 -1
  121. data/spec/dummy/script/rails +2 -2
  122. data/spec/{controllers → lib/apipie}/apipies_controller_spec.rb +95 -23
  123. data/spec/lib/apipie/application_spec.rb +62 -0
  124. data/spec/lib/apipie/configuration_spec.rb +38 -0
  125. data/spec/lib/apipie/extractor/collector_spec.rb +57 -0
  126. data/spec/lib/apipie/extractor/recorder_spec.rb +77 -0
  127. data/spec/lib/{extractor → apipie/extractor}/writer_spec.rb +8 -6
  128. data/spec/lib/{file_handler_spec.rb → apipie/file_handler_spec.rb} +7 -0
  129. data/spec/lib/apipie/generator/swagger/config_spec.rb +19 -0
  130. data/spec/lib/apipie/generator/swagger/context_spec.rb +56 -0
  131. data/spec/lib/apipie/generator/swagger/method_description/api_schema_service_spec.rb +119 -0
  132. data/spec/lib/apipie/generator/swagger/method_description/response_schema_service_spec.rb +105 -0
  133. data/spec/lib/apipie/generator/swagger/operation_id_spec.rb +63 -0
  134. data/spec/lib/apipie/generator/swagger/param_description/builder_spec.rb +203 -0
  135. data/spec/lib/apipie/generator/swagger/param_description/composite_spec.rb +95 -0
  136. data/spec/lib/apipie/generator/swagger/param_description/description_spec.rb +79 -0
  137. data/spec/lib/apipie/generator/swagger/param_description/in_spec.rb +86 -0
  138. data/spec/lib/apipie/generator/swagger/param_description/name_spec.rb +81 -0
  139. data/spec/lib/apipie/generator/swagger/param_description/type_spec.rb +178 -0
  140. data/spec/lib/apipie/generator/swagger/param_description_spec.rb +28 -0
  141. data/spec/lib/apipie/generator/swagger/path_decorator_spec.rb +57 -0
  142. data/spec/lib/apipie/generator/swagger/referenced_definitions_spec.rb +35 -0
  143. data/spec/lib/apipie/generator/swagger/resource_description_composite_spec.rb +37 -0
  144. data/spec/lib/apipie/generator/swagger/resource_descriptions_collection_spec.rb +57 -0
  145. data/spec/lib/apipie/generator/swagger/schema_spec.rb +89 -0
  146. data/spec/lib/apipie/generator/swagger/type_extractor_spec.rb +38 -0
  147. data/spec/lib/apipie/generator/swagger/warning_spec.rb +51 -0
  148. data/spec/lib/apipie/generator/swagger/warning_writer_spec.rb +71 -0
  149. data/spec/lib/apipie/method_description/apis_service_spec.rb +60 -0
  150. data/spec/lib/apipie/method_description_spec.rb +133 -0
  151. data/spec/lib/apipie/no_documented_method_spec.rb +17 -0
  152. data/spec/lib/apipie/param_description/deprecation_spec.rb +31 -0
  153. data/spec/lib/{param_description_spec.rb → apipie/param_description_spec.rb} +332 -6
  154. data/spec/lib/{param_group_spec.rb → apipie/param_group_spec.rb} +6 -5
  155. data/spec/lib/apipie/resource_description_spec.rb +91 -0
  156. data/spec/lib/apipie/response_does_not_match_swagger_schema_spec.rb +35 -0
  157. data/spec/lib/apipie/swagger_generator_spec.rb +94 -0
  158. data/spec/lib/{validator_spec.rb → apipie/validator_spec.rb} +48 -12
  159. data/spec/lib/rake_spec.rb +3 -5
  160. data/spec/lib/swagger/openapi_2_0_schema.json +8 -1
  161. data/spec/lib/swagger/rake_swagger_spec.rb +24 -9
  162. data/spec/lib/swagger/swagger_dsl_spec.rb +18 -12
  163. data/spec/lib/validators/array_validator_spec.rb +1 -1
  164. data/spec/spec_helper.rb +10 -32
  165. data/spec/support/custom_bool_validator.rb +17 -0
  166. data/spec/{controllers → test_engine}/memes_controller_spec.rb +1 -1
  167. metadata +169 -122
  168. data/Gemfile +0 -1
  169. data/gemfiles/Gemfile.rails42 +0 -14
  170. data/gemfiles/Gemfile.rails42.lock +0 -160
  171. data/gemfiles/Gemfile.rails52 +0 -9
  172. data/gemfiles/Gemfile.rails60 +0 -10
  173. data/gemfiles/Gemfile.rails61 +0 -10
  174. data/spec/lib/application_spec.rb +0 -49
  175. data/spec/lib/method_description_spec.rb +0 -98
  176. data/spec/lib/resource_description_spec.rb +0 -48
  177. data/spec/support/rails-42-ruby-26.rb +0 -15
  178. /data/spec/lib/{extractor → apipie/extractor/recorder}/middleware_spec.rb +0 -0
  179. /data/spec/lib/{extractor → apipie}/extractor_spec.rb +0 -0
@@ -0,0 +1,119 @@
1
+ class Apipie::Generator::Swagger::ParamDescription::Composite
2
+ # @param [Array<Apipie::ParamDescription>] param_descriptions
3
+ # @param [Apipie::Generator::Swagger::Context] context
4
+ def initialize(param_descriptions, context)
5
+ @param_descriptions = param_descriptions
6
+ @context = context
7
+ @schema = {
8
+ type: 'object',
9
+ properties: {}
10
+ }
11
+ @required_params = []
12
+ end
13
+
14
+ def to_swagger
15
+ return if @param_descriptions.blank?
16
+
17
+ @param_descriptions.each do |param_description|
18
+ validate(param_description)
19
+
20
+ if param_description.required
21
+ @required_params.push(param_description.name.to_sym)
22
+ end
23
+
24
+ param_type = Apipie::Generator::Swagger::TypeExtractor.
25
+ new(param_description.validator).
26
+ extract
27
+
28
+ has_nested_params = param_type == 'object' &&
29
+ param_description.validator.params_ordered.present?
30
+
31
+ if has_nested_params
32
+ schema = Apipie::Generator::Swagger::ParamDescription::Composite.new(
33
+ param_description.validator.params_ordered,
34
+ Apipie::Generator::Swagger::Context.new(
35
+ allow_null: @context.allow_null?,
36
+ http_method: @context.http_method,
37
+ controller_method: @context.controller_method
38
+ )
39
+ ).to_swagger
40
+
41
+ return if schema.blank?
42
+
43
+ if param_description.additional_properties
44
+ schema[:additionalProperties] = true
45
+ end
46
+
47
+ if param_description.is_array?
48
+ schema = for_array(schema)
49
+ end
50
+
51
+ if @context.allow_null?
52
+ schema = with_null(schema)
53
+ end
54
+
55
+ if schema.present?
56
+ @schema[:properties][param_description.name.to_sym] = schema
57
+ end
58
+ else
59
+ param_entry = Apipie::Generator::Swagger::ParamDescription::Builder.
60
+ new(
61
+ param_description,
62
+ in_schema: @context.in_schema?,
63
+ controller_method: @context.controller_method
64
+ ).
65
+ with_description(language: @context.language).
66
+ with_type(with_null: @context.allow_null?).
67
+ with_in(
68
+ default_in_value: @context.default_in_value,
69
+ http_method: @context.http_method
70
+ ).
71
+ to_swagger
72
+
73
+ @schema[:properties][param_description.name.to_sym] = param_entry
74
+ end
75
+ end
76
+
77
+ if !Apipie.configuration.generator.swagger.allow_additional_properties_in_response
78
+ @schema[:additionalProperties] = false
79
+ end
80
+
81
+ if @required_params.length > 0
82
+ @schema[:required] = @required_params
83
+ end
84
+
85
+ @schema
86
+ end
87
+
88
+ # @param [Symbol, String] param_type
89
+ #
90
+ # @return [Apipie::Generator::Swagger::ParamDescription::ReferencedComposite]
91
+ def referenced(param_type)
92
+ Apipie::Generator::Swagger::ParamDescription::ReferencedComposite.
93
+ new(self, param_type)
94
+ end
95
+
96
+ private
97
+
98
+ def for_array(schema)
99
+ {
100
+ type: 'array',
101
+ items: schema
102
+ }
103
+ end
104
+
105
+ def with_null(schema)
106
+ # Ideally we would write schema[:type] = ["object", "null"]
107
+ # but due to a bug in the json-schema gem, we need to use anyOf
108
+ # see https://github.com/ruby-json-schema/json-schema/issues/404
109
+ {
110
+ anyOf: [ schema, { type: 'null' } ]
111
+ }
112
+ end
113
+
114
+ def validate(param_description)
115
+ if !param_description.respond_to?(:required)
116
+ raise "Unexpected param_desc format"
117
+ end
118
+ end
119
+ end
@@ -0,0 +1,15 @@
1
+ class Apipie::Generator::Swagger::ParamDescription::Description
2
+ def initialize(param_description, language:)
3
+ @param_description = param_description
4
+ @language = language
5
+ end
6
+
7
+ # @return [Hash]
8
+ def to_hash
9
+ description = @param_description.options[:desc]
10
+
11
+ return {} if description.blank?
12
+
13
+ { description: Apipie.app.translate(description, @language) }
14
+ end
15
+ end
@@ -0,0 +1,37 @@
1
+ class Apipie::Generator::Swagger::ParamDescription::In
2
+ IN_KEYWORD_DEFAULT_VALUES = {
3
+ form_data: 'formData',
4
+ query: 'query'
5
+ }
6
+
7
+ def initialize(param_description, in_schema:, default_in_value:, http_method:)
8
+ @param_description = param_description
9
+ @in_schema = in_schema
10
+ @default_in_value = default_in_value
11
+ @http_method = http_method
12
+ end
13
+
14
+ # @return [Hash]
15
+ def to_hash
16
+ # The "name" and "in" keys can only be set on root parameters (non-nested)
17
+ return {} if @in_schema
18
+
19
+ { in: in_value }
20
+ end
21
+
22
+ private
23
+
24
+ def in_value
25
+ return @default_in_value if @default_in_value.present?
26
+
27
+ if body_allowed_for_current_method?
28
+ IN_KEYWORD_DEFAULT_VALUES[:form_data]
29
+ else
30
+ IN_KEYWORD_DEFAULT_VALUES[:query]
31
+ end
32
+ end
33
+
34
+ def body_allowed_for_current_method?
35
+ %w[get head].exclude?(@http_method)
36
+ end
37
+ end
@@ -0,0 +1,18 @@
1
+ class Apipie::Generator::Swagger::ParamDescription::Name
2
+ def initialize(param_description, prefixed_by: nil)
3
+ @param_description = param_description
4
+ @prefixed_by = prefixed_by
5
+ end
6
+
7
+ # @return [Hash]
8
+ def to_hash
9
+ name =
10
+ if @prefixed_by.present?
11
+ "#{@prefixed_by}[#{@param_description.name}]"
12
+ else
13
+ @param_description.name
14
+ end
15
+
16
+ { name: name }
17
+ end
18
+ end
@@ -0,0 +1,61 @@
1
+ class Apipie::Generator::Swagger::ParamDescription::PathParamsComposite
2
+ # @param [Array<Apipie::ParamDescription>] param_descriptions
3
+ # @param [Apipie::Generator::Swagger::Context] context
4
+ def initialize(param_descriptions, context)
5
+ @param_descriptions = param_descriptions
6
+ @context = context
7
+ @result = []
8
+ end
9
+
10
+ # @return [Array]
11
+ def to_swagger
12
+ @param_descriptions.each do |desc|
13
+ context = @context.dup
14
+
15
+ type = Apipie::Generator::Swagger::TypeExtractor.new(desc.validator).extract
16
+
17
+ if type == 'object' && desc.validator.params_ordered.blank?
18
+ warn_param_ignored_in_form_data(desc.name)
19
+ next
20
+ end
21
+
22
+ has_nested_params = type == 'object' &&
23
+ desc.validator.params_ordered.present?
24
+
25
+ if has_nested_params
26
+ context.add_to_prefix!(desc.name)
27
+
28
+ out = Apipie::Generator::Swagger::ParamDescription::PathParamsComposite
29
+ .new(desc.validator.params_ordered, context)
30
+ .to_swagger
31
+
32
+ @result.concat(out)
33
+ else
34
+ param_entry =
35
+ Apipie::Generator::Swagger::ParamDescription::Builder
36
+ .new(desc, in_schema: false, controller_method: context.controller_method)
37
+ .with_description(language: context.language)
38
+ .with_name(prefix: context.prefix)
39
+ .with_type(with_null: context.allow_null?)
40
+ .with_in(
41
+ http_method: context.http_method,
42
+ default_in_value: context.default_in_value
43
+ ).to_swagger
44
+
45
+ @result << param_entry
46
+ end
47
+ end
48
+
49
+ @result.sort_by { |p| p[:required] ? 0 : 1 }
50
+ end
51
+
52
+ private
53
+
54
+ def warn_param_ignored_in_form_data(name)
55
+ Apipie::Generator::Swagger::Warning.for_code(
56
+ Apipie::Generator::Swagger::Warning::PARAM_IGNORED_IN_FORM_DATA_CODE,
57
+ @context.controller_method.ruby_name,
58
+ { parameter: name }
59
+ ).warn_through_writer
60
+ end
61
+ end
@@ -0,0 +1,36 @@
1
+ # A Composite that keeps track when a param description has been added to references
2
+ # and returns the reference instead of the complete object
3
+ class Apipie::Generator::Swagger::ParamDescription::ReferencedComposite
4
+ # @param [Apipie::Generator::Swagger::ParamDescription::Composite] composite
5
+ # @param [Symbol, String] param_type
6
+ def initialize(composite, param_type)
7
+ @composite = composite
8
+ @param_type = param_type.to_sym
9
+ end
10
+
11
+ def to_swagger
12
+ return ref_to(:name) if added?(:name)
13
+
14
+ schema_obj = @composite.to_swagger
15
+
16
+ return nil if schema_obj.nil?
17
+
18
+ add(schema_obj)
19
+
20
+ { '$ref' => ref_to(@param_type) }
21
+ end
22
+
23
+ private
24
+
25
+ def ref_to(name)
26
+ "#/definitions/#{name}"
27
+ end
28
+
29
+ def add(schema)
30
+ Apipie::Generator::Swagger::ReferencedDefinitions.instance.add!(@param_type, schema)
31
+ end
32
+
33
+ def added?(name)
34
+ Apipie::Generator::Swagger::ReferencedDefinitions.instance.added?(name)
35
+ end
36
+ end
@@ -0,0 +1,115 @@
1
+ class Apipie::Generator::Swagger::ParamDescription::Type
2
+ def initialize(param_description, with_null:, controller_method:)
3
+ @param_description = param_description
4
+ @with_null = with_null
5
+ @controller_method = controller_method
6
+ end
7
+
8
+ # @return [Hash]
9
+ def to_hash
10
+ type_definition = {}
11
+
12
+ case type.to_s
13
+ when 'array'
14
+ type_definition.merge!(for_array_type)
15
+ when 'enum'
16
+ type_definition.merge!(for_enum_type)
17
+ when 'object'
18
+ # We only get here if there is no specification
19
+ # of properties for this object.
20
+ type_definition.merge!(for_object_type)
21
+ warn_hash_without_internal_typespec
22
+ else
23
+ type_definition.merge!({ type: type.to_s })
24
+ end
25
+
26
+ if @param_description.is_array?
27
+ type_definition = {
28
+ items: type_definition,
29
+ type: 'array'
30
+ }
31
+
32
+ if @with_null
33
+ type_definition[:type] = [type_definition[:type], 'null']
34
+ end
35
+ end
36
+
37
+ if @with_null
38
+ type_definition[:type] = [type_definition[:type], 'null']
39
+ end
40
+
41
+ type_definition
42
+ end
43
+
44
+ private
45
+
46
+ def params_in_body_use_reference?
47
+ Apipie.configuration.generator.swagger.json_input_uses_refs
48
+ end
49
+
50
+ # @return [Apipie::Generator::Swagger::Type, String]
51
+ def type
52
+ @_type ||= Apipie::Generator::Swagger::TypeExtractor.
53
+ new(validator).
54
+ extract
55
+ end
56
+
57
+ def for_array_type
58
+ validator_opts = validator.param_description.options
59
+ items_type = validator_opts[:of].to_s || validator_opts[:array_of].to_s
60
+
61
+ if items_type == 'Hash' && params_in_body_use_reference?
62
+ reference_name = Apipie::Generator::Swagger::OperationId.
63
+ from(@param_description.method_description, param: @param_description.name).
64
+ to_s
65
+
66
+ items = {
67
+ '$ref' => reference_name
68
+ }
69
+ else
70
+ items = { type: 'string' }
71
+ end
72
+
73
+ enum = @param_description.options[:in]
74
+
75
+ items[:enum] = enum if enum.present?
76
+
77
+ {
78
+ type: 'array',
79
+ items: items
80
+ }
81
+ end
82
+
83
+ def for_enum_type
84
+ {
85
+ type: 'string',
86
+ enum: @param_description.validator.values
87
+ }
88
+ end
89
+
90
+ def for_object_type
91
+ {
92
+ type: 'object',
93
+ additionalProperties: true
94
+ }
95
+ end
96
+
97
+ def validator
98
+ @_validator ||= @param_description.validator
99
+ end
100
+
101
+ def warn_hash_without_internal_typespec
102
+ method_id =
103
+ if @param_description.is_a?(Apipie::ResponseDescriptionAdapter::PropDesc)
104
+ @controller_method.method
105
+ else
106
+ Apipie::Generator::Swagger::MethodDescription::Decorator.new(@param_description.method_description).ruby_name
107
+ end
108
+
109
+ Apipie::Generator::Swagger::Warning.for_code(
110
+ Apipie::Generator::Swagger::Warning::HASH_WITHOUT_INTERNAL_TYPESPEC_CODE,
111
+ method_id,
112
+ { parameter: @param_description.name }
113
+ ).warn_through_writer
114
+ end
115
+ end
@@ -0,0 +1,18 @@
1
+ module Apipie::Generator::Swagger::ParamDescription
2
+ # @param [Apipie::MethodDescription] method_description
3
+ # @param [String] name
4
+ #
5
+ # @return [Apipie::ParamDescription]
6
+ def self.create_for_missing_param(method_description, name)
7
+ Apipie::ParamDescription.new(
8
+ method_description,
9
+ name,
10
+ Numeric,
11
+ {
12
+ in: "path",
13
+ required: true,
14
+ added_from_path: true,
15
+ }
16
+ )
17
+ end
18
+ end
@@ -0,0 +1,36 @@
1
+ class Apipie::Generator::Swagger::PathDecorator < SimpleDelegator
2
+ def param_names
3
+ @param_names ||= scan(/:(\w+)/).map { |ar| ar[0].to_sym }
4
+ end
5
+
6
+ # @param [Symbol] param_name
7
+ def param?(param_name)
8
+ param_names.include?(param_name)
9
+ end
10
+
11
+ # @param [String] controller_method
12
+ #
13
+ # @return [Apipie::Generator::Swagger::PathDecorator]
14
+ def swagger_path(controller_method = nil)
15
+ current_path = gsub(/:(\w+)/, '{\1}').gsub(%r{/$}, '')
16
+
17
+ unless current_path.starts_with?('/')
18
+ warn_for_missing_slash(controller_method) if controller_method.present?
19
+
20
+ current_path = "/#{current_path}"
21
+ end
22
+
23
+ current_path
24
+ end
25
+
26
+ private
27
+
28
+ # @param [String] controller_method
29
+ def warn_for_missing_slash(controller_method)
30
+ Apipie::Generator::Swagger::Warning.for_code(
31
+ Apipie::Generator::Swagger::Warning::ADDED_MISSING_SLASH_CODE,
32
+ controller_method,
33
+ { path: self }
34
+ ).warn_through_writer
35
+ end
36
+ end
@@ -0,0 +1,17 @@
1
+ class Apipie::Generator::Swagger::ReferencedDefinitions
2
+ include Singleton
3
+
4
+ attr_reader :definitions
5
+
6
+ def initialize
7
+ @definitions = {}
8
+ end
9
+
10
+ def add!(param_type, schema)
11
+ @definitions[param_type] = schema
12
+ end
13
+
14
+ def added?(name)
15
+ @definitions.key?(name)
16
+ end
17
+ end
@@ -0,0 +1,30 @@
1
+ class Apipie::Generator::Swagger::ResourceDescriptionsCollection
2
+ # @param [Hash{String->Apipie::ResourceDescription}] resource_descriptions
3
+ def initialize(resource_descriptions)
4
+ @resource_descriptions = resource_descriptions
5
+ end
6
+
7
+ # @return [Array<Apipie::ResourceDescription>]
8
+ def filter(version:, resource_id:, method_name: nil)
9
+ resources = []
10
+
11
+ # If resource_id is blank, take just resources which have some methods because
12
+ # we dont want to show eg ApplicationController as resource
13
+ # otherwise, take only the specified resource
14
+ @resource_descriptions[version].each do |resource_description_id, resource_description|
15
+ if (resource_id.blank? && resource_description._methods.present?) || resource_description_id == resource_id
16
+ resources << resource_description
17
+ end
18
+ end
19
+
20
+ if method_name.present?
21
+ resources = resources.select do |resource_description|
22
+ resource_description._methods.select do |method_description_name, _|
23
+ method_description_name == method_name
24
+ end.present?
25
+ end
26
+ end
27
+
28
+ resources
29
+ end
30
+ end
@@ -0,0 +1,56 @@
1
+ class Apipie::Generator::Swagger::ResourceDescriptionComposite
2
+ # @param [Array<Apipie::ResourceDescription>] resource_descriptions
3
+ def initialize(resource_descriptions, language:)
4
+ @resource_descriptions = resource_descriptions
5
+ @language = language
6
+ end
7
+
8
+ # @return [Hash{Symbol->Array | Hash}]
9
+ def to_swagger
10
+ {
11
+ tags: tags,
12
+ paths: paths
13
+ }
14
+ end
15
+
16
+ # @return [Array]
17
+ def tags
18
+ results = []
19
+
20
+ @resource_descriptions.each do |resource_description|
21
+ next unless resource_description._full_description
22
+
23
+ results << {
24
+ name: resource_description._id,
25
+ description: Apipie.app.translate(
26
+ resource_description._full_description,
27
+ @language
28
+ )
29
+ }
30
+ end
31
+
32
+ results
33
+ end
34
+
35
+ # @return [Hash]
36
+ def paths
37
+ results = {}
38
+
39
+ @resource_descriptions.each do |resource_description|
40
+ resource_description._methods.each_value do |method_description|
41
+ next unless method_description.show
42
+
43
+ result = Apipie::Generator::Swagger::MethodDescription::ApiSchemaService
44
+ .new(
45
+ Apipie::Generator::Swagger::MethodDescription::Decorator.new(method_description),
46
+ language: @language
47
+ )
48
+ .call
49
+
50
+ results.deep_merge!(result)
51
+ end
52
+ end
53
+
54
+ results
55
+ end
56
+ end
@@ -0,0 +1,63 @@
1
+ class Apipie::Generator::Swagger::Schema
2
+ # @param [Array<Apipie::ResourceDescription>] resource_descriptions
3
+ def initialize(resource_descriptions, version:, language:, clear_warnings:)
4
+ @resource_descriptions = resource_descriptions
5
+ @language = language
6
+ @clear_warnings = clear_warnings
7
+ @swagger = {
8
+ swagger: '2.0',
9
+ info: {
10
+ title: Apipie.configuration.app_name.to_s,
11
+ description: "#{Apipie.app_info(version, @language)}#{Apipie.configuration.copyright}",
12
+ version: version.to_s,
13
+ 'x-copyright': Apipie.configuration.copyright
14
+ },
15
+ basePath: Apipie.api_base_url(version),
16
+ consumes: [],
17
+ paths: {},
18
+ definitions: {},
19
+ schemes: Apipie.configuration.generator.swagger.schemes,
20
+ tags: [],
21
+ securityDefinitions: Apipie.configuration.generator.swagger.security_definitions,
22
+ security: Apipie.configuration.generator.swagger.global_security
23
+ }
24
+ end
25
+
26
+ def generate
27
+ if Apipie.configuration.generator.swagger.api_host.present?
28
+ @swagger[:host] = Apipie.configuration.generator.swagger.api_host
29
+ end
30
+
31
+ if Apipie.configuration.generator.swagger.content_type_input == :json
32
+ @swagger[:consumes] = ['application/json']
33
+ @swagger[:info][:title] += ' (params in:body)'
34
+ else
35
+ @swagger[:consumes] = ['application/x-www-form-urlencoded', 'multipart/form-data']
36
+ @swagger[:info][:title] += ' (params in:formData)'
37
+ end
38
+
39
+ if @clear_warnings
40
+ Apipie::Generator::Swagger::WarningWriter.instance.clear!
41
+ end
42
+
43
+ @swagger.merge!(tags_and_paths)
44
+
45
+ if Apipie.configuration.generator.swagger.generate_x_computed_id_field?
46
+ @swagger[:info]['x-computed-id'] =
47
+ Apipie::Generator::Swagger::ComputedInterfaceId.instance.id
48
+ end
49
+
50
+ @swagger[:definitions] =
51
+ Apipie::Generator::Swagger::ReferencedDefinitions.instance.definitions
52
+
53
+ @swagger
54
+ end
55
+
56
+ private
57
+
58
+ def tags_and_paths
59
+ Apipie::Generator::Swagger::ResourceDescriptionComposite
60
+ .new(@resource_descriptions, language: @language)
61
+ .to_swagger
62
+ end
63
+ end
@@ -0,0 +1,2 @@
1
+ module Apipie::Generator::Swagger
2
+ end
@@ -0,0 +1,16 @@
1
+ class Apipie::Generator::Swagger::Type
2
+ attr_reader :str_format
3
+
4
+ def initialize(type, str_format = nil)
5
+ @type = type
6
+ @str_format = str_format
7
+ end
8
+
9
+ def to_s
10
+ @type
11
+ end
12
+
13
+ def ==(other)
14
+ other.to_s == self.to_s
15
+ end
16
+ end