apipie-rails 0.5.19 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (181) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/build.yml +31 -0
  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 +2056 -0
  8. data/.vscode/settings.json +3 -0
  9. data/CHANGELOG.md +161 -0
  10. data/Gemfile +20 -0
  11. data/README.rst +117 -23
  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 +44 -33
  30. data/lib/apipie/error_description.rb +3 -3
  31. data/lib/apipie/errors.rb +17 -17
  32. data/lib/apipie/extractor/collector.rb +5 -6
  33. data/lib/apipie/extractor/recorder.rb +35 -8
  34. data/lib/apipie/extractor/writer.rb +15 -15
  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 +114 -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 +128 -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 +12 -10
  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 +10 -2
  81. data/lib/apipie/swagger_generator.rb +28 -691
  82. data/lib/apipie/validator.rb +41 -11
  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 +37 -32
  88. data/rel-eng/gem_release.ipynb +41 -9
  89. data/spec/controllers/api/v2/architectures_controller_spec.rb +10 -3
  90. data/spec/controllers/api/v2/empty_middle_controller_spec.rb +23 -0
  91. data/spec/controllers/api/v2/nested/resources_controller_spec.rb +18 -2
  92. data/spec/controllers/api/v2/sub/footguns_controller_spec.rb +19 -0
  93. data/spec/controllers/included_param_group_controller_spec.rb +13 -0
  94. data/spec/{lib/swagger/response_validation_spec.rb → controllers/pets_controller_spec.rb} +26 -32
  95. data/spec/controllers/users_controller_spec.rb +47 -6
  96. data/spec/dummy/Rakefile +1 -1
  97. data/spec/dummy/app/controllers/api/v2/architectures_controller.rb +2 -1
  98. data/spec/dummy/app/controllers/api/v2/base_controller.rb +6 -0
  99. data/spec/dummy/app/controllers/api/v2/empty_middle_controller.rb +14 -0
  100. data/spec/dummy/app/controllers/api/v2/nested/resources_controller.rb +2 -2
  101. data/spec/dummy/app/controllers/api/v2/sub/footguns_controller.rb +30 -0
  102. data/spec/dummy/app/controllers/concerns_controller.rb +1 -1
  103. data/spec/dummy/app/controllers/{concerns/extending_concern.rb → extending_concern.rb} +0 -2
  104. data/spec/dummy/app/controllers/included_param_group_controller.rb +19 -0
  105. data/spec/dummy/app/controllers/overridden_concerns_controller.rb +2 -2
  106. data/spec/dummy/app/controllers/pets_controller.rb +5 -5
  107. data/spec/dummy/app/controllers/pets_using_self_describing_classes_controller.rb +2 -2
  108. data/spec/dummy/app/controllers/{concerns/sample_controller.rb → sample_controller.rb} +0 -2
  109. data/spec/dummy/app/controllers/twitter_example_controller.rb +2 -2
  110. data/spec/dummy/app/controllers/users_controller.rb +17 -5
  111. data/spec/dummy/app/helpers/random_param_group.rb +8 -0
  112. data/spec/dummy/components/test_engine/test_engine.gemspec +1 -1
  113. data/spec/dummy/config/application.rb +2 -5
  114. data/spec/dummy/config/boot.rb +2 -2
  115. data/spec/dummy/config/environment.rb +1 -1
  116. data/spec/dummy/config/environments/development.rb +0 -3
  117. data/spec/dummy/config/environments/production.rb +0 -3
  118. data/spec/dummy/config/environments/test.rb +0 -5
  119. data/spec/dummy/config/initializers/apipie.rb +2 -2
  120. data/spec/dummy/config/routes.rb +8 -0
  121. data/spec/dummy/config.ru +1 -1
  122. data/spec/dummy/script/rails +2 -2
  123. data/spec/{controllers → lib/apipie}/apipies_controller_spec.rb +95 -23
  124. data/spec/lib/apipie/application_spec.rb +62 -0
  125. data/spec/lib/apipie/configuration_spec.rb +38 -0
  126. data/spec/lib/apipie/extractor/collector_spec.rb +57 -0
  127. data/spec/lib/apipie/extractor/recorder_spec.rb +77 -0
  128. data/spec/lib/{extractor → apipie/extractor}/writer_spec.rb +8 -6
  129. data/spec/lib/{extractor → apipie}/extractor_spec.rb +1 -1
  130. data/spec/lib/{file_handler_spec.rb → apipie/file_handler_spec.rb} +7 -0
  131. data/spec/lib/apipie/generator/swagger/config_spec.rb +19 -0
  132. data/spec/lib/apipie/generator/swagger/context_spec.rb +56 -0
  133. data/spec/lib/apipie/generator/swagger/method_description/api_schema_service_spec.rb +119 -0
  134. data/spec/lib/apipie/generator/swagger/method_description/response_schema_service_spec.rb +105 -0
  135. data/spec/lib/apipie/generator/swagger/operation_id_spec.rb +63 -0
  136. data/spec/lib/apipie/generator/swagger/param_description/builder_spec.rb +215 -0
  137. data/spec/lib/apipie/generator/swagger/param_description/composite_spec.rb +95 -0
  138. data/spec/lib/apipie/generator/swagger/param_description/description_spec.rb +79 -0
  139. data/spec/lib/apipie/generator/swagger/param_description/in_spec.rb +86 -0
  140. data/spec/lib/apipie/generator/swagger/param_description/name_spec.rb +81 -0
  141. data/spec/lib/apipie/generator/swagger/param_description/type_spec.rb +183 -0
  142. data/spec/lib/apipie/generator/swagger/param_description_spec.rb +28 -0
  143. data/spec/lib/apipie/generator/swagger/path_decorator_spec.rb +57 -0
  144. data/spec/lib/apipie/generator/swagger/referenced_definitions_spec.rb +35 -0
  145. data/spec/lib/apipie/generator/swagger/resource_description_composite_spec.rb +37 -0
  146. data/spec/lib/apipie/generator/swagger/resource_descriptions_collection_spec.rb +57 -0
  147. data/spec/lib/apipie/generator/swagger/schema_spec.rb +89 -0
  148. data/spec/lib/apipie/generator/swagger/type_extractor_spec.rb +38 -0
  149. data/spec/lib/apipie/generator/swagger/warning_spec.rb +51 -0
  150. data/spec/lib/apipie/generator/swagger/warning_writer_spec.rb +71 -0
  151. data/spec/lib/apipie/method_description/apis_service_spec.rb +60 -0
  152. data/spec/lib/apipie/method_description_spec.rb +133 -0
  153. data/spec/lib/apipie/no_documented_method_spec.rb +17 -0
  154. data/spec/lib/apipie/param_description/deprecation_spec.rb +31 -0
  155. data/spec/lib/{param_description_spec.rb → apipie/param_description_spec.rb} +332 -6
  156. data/spec/lib/{param_group_spec.rb → apipie/param_group_spec.rb} +6 -5
  157. data/spec/lib/apipie/resource_description_spec.rb +91 -0
  158. data/spec/lib/apipie/response_does_not_match_swagger_schema_spec.rb +35 -0
  159. data/spec/lib/apipie/swagger_generator_spec.rb +94 -0
  160. data/spec/lib/{validator_spec.rb → apipie/validator_spec.rb} +48 -12
  161. data/spec/lib/rake_spec.rb +3 -5
  162. data/spec/lib/swagger/openapi_2_0_schema.json +8 -1
  163. data/spec/lib/swagger/rake_swagger_spec.rb +30 -10
  164. data/spec/lib/swagger/swagger_dsl_spec.rb +18 -12
  165. data/spec/lib/validators/array_validator_spec.rb +1 -1
  166. data/spec/spec_helper.rb +10 -32
  167. data/spec/support/custom_bool_validator.rb +17 -0
  168. data/spec/{controllers → test_engine}/memes_controller_spec.rb +1 -1
  169. metadata +173 -125
  170. data/.travis.yml +0 -41
  171. data/Gemfile +0 -1
  172. data/Gemfile.rails41 +0 -7
  173. data/Gemfile.rails42 +0 -14
  174. data/Gemfile.rails50 +0 -9
  175. data/Gemfile.rails51 +0 -9
  176. data/Gemfile.rails60 +0 -10
  177. data/Gemfile.rails61 +0 -10
  178. data/spec/lib/application_spec.rb +0 -49
  179. data/spec/lib/method_description_spec.rb +0 -98
  180. data/spec/lib/resource_description_spec.rb +0 -48
  181. /data/spec/lib/{extractor → apipie/extractor/recorder}/middleware_spec.rb +0 -0
@@ -0,0 +1,114 @@
1
+ class Apipie::Generator::Swagger::ParamDescription::Builder
2
+ # @param [Apipie::ParamDescription] param_description
3
+ # @param [TrueClass, FalseClass] in_schema
4
+ # @param [Apipie::MethodDescription] controller_method
5
+ def initialize(param_description, in_schema:, controller_method:)
6
+ @param_description = param_description
7
+ @in_schema = in_schema
8
+ @controller_method = controller_method
9
+ end
10
+
11
+ # @param [String, nil] prefix
12
+ def with_name(prefix: nil)
13
+ @name = Apipie::Generator::Swagger::ParamDescription::Name.
14
+ new(@param_description, prefixed_by: prefix)
15
+
16
+ self
17
+ end
18
+
19
+ # @param [String] language
20
+ def with_description(language:)
21
+ @description = Apipie::Generator::Swagger::ParamDescription::Description.
22
+ new(@param_description, language: language)
23
+
24
+ self
25
+ end
26
+
27
+ # @param [TrueClass, FalseClass] with_null
28
+ def with_type(with_null:)
29
+ @type = Apipie::Generator::Swagger::ParamDescription::Type.
30
+ new(@param_description, with_null: with_null, controller_method: @controller_method)
31
+
32
+ self
33
+ end
34
+
35
+ def with_in(http_method:, default_in_value: nil)
36
+ @in = Apipie::Generator::Swagger::ParamDescription::In.new(
37
+ @param_description,
38
+ in_schema: @in_schema,
39
+ default_in_value: default_in_value,
40
+ http_method: http_method
41
+ )
42
+
43
+ self
44
+ end
45
+
46
+ # @return [Hash]
47
+ def to_swagger
48
+ definition = {}
49
+
50
+ definition.merge!(@name.to_hash) if @name.present?
51
+ definition.merge!(@type.to_hash) if @type.present?
52
+ definition.merge!(@in.to_hash) if @in.present?
53
+
54
+ definition.merge!(for_default)
55
+ definition.merge!(for_required)
56
+ definition.merge!(for_example)
57
+ definition.merge!(@description.to_hash) if @description.present?
58
+
59
+ warn_optional_without_default_value(definition)
60
+
61
+ definition
62
+ end
63
+
64
+ private
65
+
66
+ def for_required
67
+ return {} if !required?
68
+
69
+ {
70
+ required: true
71
+ }
72
+ end
73
+
74
+ def for_default
75
+ return {} unless @param_description.options.key?(:default_value)
76
+
77
+ {
78
+ default: @param_description.options[:default_value],
79
+ }
80
+ end
81
+
82
+ def for_example
83
+ return {} unless @param_description.options.key?(:example)
84
+
85
+ {
86
+ example: @param_description.options[:example],
87
+ }
88
+ end
89
+
90
+ def required?
91
+ required_from_path? || @param_description.required
92
+ end
93
+
94
+ def required_from_path?
95
+ @param_description.options[:added_from_path] == true
96
+ end
97
+
98
+ def warn_optional_without_default_value(definition)
99
+ if !required? && !definition.key?(:default)
100
+ method_id =
101
+ if @param_description.is_a?(Apipie::ResponseDescriptionAdapter::PropDesc)
102
+ @controller_method
103
+ else
104
+ Apipie::Generator::Swagger::MethodDescription::Decorator.new(@controller_method).ruby_name
105
+ end
106
+
107
+ Apipie::Generator::Swagger::Warning.for_code(
108
+ Apipie::Generator::Swagger::Warning::OPTIONAL_WITHOUT_DEFAULT_VALUE_CODE,
109
+ method_id,
110
+ { parameter: @param_description.name }
111
+ ).warn_through_writer
112
+ end
113
+ end
114
+ end
@@ -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,128 @@
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] || 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: array_items_type(items_type).to_s }
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
+ # @param [String] items_type
84
+ #
85
+ # @return [Apipie::Generator::Swagger::Type]
86
+ def array_items_type(items_type)
87
+ type = Apipie::Generator::Swagger::TypeExtractor::TYPES[items_type.downcase.to_sym]
88
+
89
+ if type == 'object' || type.blank?
90
+ Apipie::Generator::Swagger::TypeExtractor::TYPES[:string]
91
+ else
92
+ type
93
+ end
94
+ end
95
+
96
+ def for_enum_type
97
+ {
98
+ type: 'string',
99
+ enum: @param_description.validator.values
100
+ }
101
+ end
102
+
103
+ def for_object_type
104
+ {
105
+ type: 'object',
106
+ additionalProperties: true
107
+ }
108
+ end
109
+
110
+ def validator
111
+ @_validator ||= @param_description.validator
112
+ end
113
+
114
+ def warn_hash_without_internal_typespec
115
+ method_id =
116
+ if @param_description.is_a?(Apipie::ResponseDescriptionAdapter::PropDesc)
117
+ @controller_method.method
118
+ else
119
+ Apipie::Generator::Swagger::MethodDescription::Decorator.new(@param_description.method_description).ruby_name
120
+ end
121
+
122
+ Apipie::Generator::Swagger::Warning.for_code(
123
+ Apipie::Generator::Swagger::Warning::HASH_WITHOUT_INTERNAL_TYPESPEC_CODE,
124
+ method_id,
125
+ { parameter: @param_description.name }
126
+ ).warn_through_writer
127
+ end
128
+ 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.any? do |method_description_name, _|
23
+ method_description_name == method_name
24
+ end
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