apipie-rails 0.9.4 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (80) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +8 -14
  3. data/.rubocop_todo.yml +102 -213
  4. data/CHANGELOG.md +17 -6
  5. data/README.rst +12 -12
  6. data/app/controllers/apipie/apipies_controller.rb +4 -4
  7. data/lib/apipie/apipie_module.rb +4 -4
  8. data/lib/apipie/application.rb +70 -54
  9. data/lib/apipie/configuration.rb +19 -26
  10. data/lib/apipie/dsl_definition.rb +4 -5
  11. data/lib/apipie/extractor/collector.rb +2 -2
  12. data/lib/apipie/extractor.rb +1 -1
  13. data/lib/apipie/generator/config.rb +12 -0
  14. data/lib/apipie/generator/swagger/computed_interface_id.rb +23 -0
  15. data/lib/apipie/generator/swagger/config.rb +78 -0
  16. data/lib/apipie/generator/swagger/context.rb +12 -1
  17. data/lib/apipie/generator/swagger/method_description/api_decorator.rb +20 -0
  18. data/lib/apipie/generator/swagger/method_description/api_schema_service.rb +86 -0
  19. data/lib/apipie/generator/swagger/method_description/decorator.rb +22 -0
  20. data/lib/apipie/generator/swagger/method_description/parameters_service.rb +139 -0
  21. data/lib/apipie/generator/swagger/method_description/response_schema_service.rb +46 -0
  22. data/lib/apipie/generator/swagger/method_description/response_service.rb +58 -0
  23. data/lib/apipie/generator/swagger/method_description.rb +2 -0
  24. data/lib/apipie/generator/swagger/operation_id.rb +1 -1
  25. data/lib/apipie/generator/swagger/param_description/builder.rb +4 -4
  26. data/lib/apipie/generator/swagger/param_description/composite.rb +9 -1
  27. data/lib/apipie/generator/swagger/param_description/in.rb +1 -1
  28. data/lib/apipie/generator/swagger/param_description/path_params_composite.rb +61 -0
  29. data/lib/apipie/generator/swagger/param_description/referenced_composite.rb +36 -0
  30. data/lib/apipie/generator/swagger/param_description/type.rb +9 -2
  31. data/lib/apipie/generator/swagger/path_decorator.rb +36 -0
  32. data/lib/apipie/generator/swagger/referenced_definitions.rb +17 -0
  33. data/lib/apipie/generator/swagger/resource_description_collection.rb +30 -0
  34. data/lib/apipie/generator/swagger/resource_description_composite.rb +56 -0
  35. data/lib/apipie/generator/swagger/schema.rb +63 -0
  36. data/lib/apipie/generator/swagger/type_extractor.rb +0 -19
  37. data/lib/apipie/generator/swagger/warning.rb +3 -6
  38. data/lib/apipie/generator/swagger/warning_writer.rb +7 -1
  39. data/lib/apipie/method_description.rb +4 -2
  40. data/lib/apipie/param_description.rb +4 -2
  41. data/lib/apipie/resource_description.rb +1 -1
  42. data/lib/apipie/swagger_generator.rb +27 -551
  43. data/lib/apipie/validator.rb +7 -3
  44. data/lib/apipie/version.rb +1 -1
  45. data/lib/apipie-rails.rb +17 -0
  46. data/lib/tasks/apipie.rake +15 -10
  47. data/spec/controllers/api/v2/nested/resources_controller_spec.rb +2 -2
  48. data/spec/controllers/pets_controller_spec.rb +10 -16
  49. data/spec/controllers/users_controller_spec.rb +2 -2
  50. data/spec/dummy/app/controllers/pets_controller.rb +4 -4
  51. data/spec/dummy/app/controllers/pets_using_self_describing_classes_controller.rb +2 -2
  52. data/spec/dummy/app/controllers/twitter_example_controller.rb +2 -2
  53. data/spec/dummy/app/controllers/users_controller.rb +5 -5
  54. data/spec/lib/apipie/application_spec.rb +16 -7
  55. data/spec/lib/apipie/configuration_spec.rb +15 -0
  56. data/spec/lib/apipie/generator/swagger/config_spec.rb +19 -0
  57. data/spec/lib/apipie/generator/swagger/context_spec.rb +22 -2
  58. data/spec/lib/apipie/generator/swagger/method_description/api_schema_service_spec.rb +106 -0
  59. data/spec/lib/apipie/generator/swagger/method_description/response_schema_service_spec.rb +105 -0
  60. data/spec/lib/apipie/generator/swagger/param_description/builder_spec.rb +1 -1
  61. data/spec/lib/apipie/generator/swagger/param_description/composite_spec.rb +2 -2
  62. data/spec/lib/apipie/generator/swagger/param_description/type_spec.rb +7 -7
  63. data/spec/lib/apipie/generator/swagger/path_decorator_spec.rb +57 -0
  64. data/spec/lib/apipie/generator/swagger/referenced_definitions_spec.rb +35 -0
  65. data/spec/lib/apipie/generator/swagger/resource_description_composite_spec.rb +37 -0
  66. data/spec/lib/apipie/generator/swagger/resource_descriptions_collection_spec.rb +57 -0
  67. data/spec/lib/apipie/generator/swagger/schema_spec.rb +89 -0
  68. data/spec/lib/apipie/generator/swagger/type_extractor_spec.rb +0 -43
  69. data/spec/lib/apipie/generator/swagger/warning_spec.rb +1 -1
  70. data/spec/lib/apipie/generator/swagger/warning_writer_spec.rb +19 -7
  71. data/spec/lib/apipie/method_description_spec.rb +101 -66
  72. data/spec/lib/apipie/param_description_spec.rb +209 -49
  73. data/spec/lib/apipie/param_group_spec.rb +1 -0
  74. data/spec/lib/apipie/resource_description_spec.rb +1 -1
  75. data/spec/lib/apipie/swagger_generator_spec.rb +94 -0
  76. data/spec/lib/apipie/validator_spec.rb +47 -11
  77. data/spec/lib/swagger/rake_swagger_spec.rb +4 -4
  78. data/spec/lib/swagger/swagger_dsl_spec.rb +7 -7
  79. data/spec/lib/validators/array_validator_spec.rb +1 -1
  80. metadata +28 -2
data/CHANGELOG.md CHANGED
@@ -4,24 +4,35 @@
4
4
  Also deleted the `Gemfile` that was now a broken symlink.
5
5
  please use `export BUNDLE_GEMFILE='gemfiles/Gemfile.rails61'; bundle exec rspec` to run the test suite
6
6
 
7
+ ## [v1.0.0](https://github.com/Apipie/apipie-rails/tree/v1.0.0) (2023-04-26)
8
+ [Full Changelog](https://github.com/Apipie/apipie-rails/compare/v0.9.4...v1.0.0)
9
+ * Refactor Swagger generator [#816](https://github.com/Apipie/apipie-rails/pull/816) (Panos Dalitsouris)
10
+ * Take into account validator options for `required` ParamDescription [#863](https://github.com/Apipie/apipie-rails/pull/863) (Panos Dalitsouris)
11
+ * Replace `get_resource_name` with `get_resource_id` [#864](https://github.com/Apipie/apipie-rails/pull/864) (Panos Dalitsouris)
12
+ * Namespace swagger configuration [#862](https://github.com/Apipie/apipie-rails/pull/862) (Panos Dalitsouris)
13
+ * Allow boolean array `[true, false]` to be a valid argument for BooleanValidator [#848](https://github.com/Apipie/apipie-rails/pull/848) (Panos Dalitsouris)
14
+ * Fix swagger warnings [#865](https://github.com/Apipie/apipie-rails/pull/865) (Panos Dalitsouris)
15
+ * Update README - Adds the new Integer option you added recently to the documentation here. [#866](https://github.com/Apipie/apipie-rails/pull/866) (Jeremy Lupoli)
16
+ * [Rubocop] More Rubocop Auto corrections [#859](https://github.com/Apipie/apipie-rails/pull/859)
17
+
7
18
  ## [v0.9.4](https://github.com/Apipie/apipie-rails/tree/v0.9.4) (2023-04-11)
8
19
  [Full Changelog](https://github.com/Apipie/apipie-rails/compare/v0.9.3...v0.9.4)
9
- * [Fix] Separate nested resource name [#855](https://github.com/Apipie/apipie-rails/pull/855)[#455](https://github.com/Apipie/apipie-rails/issues/455) (Panos Dalitsouris)
10
- * [Rubocop] Disable a few Rubocop Rules, run Rubocop with ruby 3.2 [#851](https://github.com/Apipie/apipie-rails/pull/851)[#853](https://github.com/Apipie/apipie-rails/pull/853)[#840](https://github.com/Apipie/apipie-rails/pull/840)[#841](https://github.com/Apipie/apipie-rails/pull/841) (Panos Dalitsouris)
11
- * [Rubocop] More Rubocop Auto corrections [#858](https://github.com/Apipie/apipie-rails/pull/858)[#849](https://github.com/Apipie/apipie-rails/pull/849)[#850](https://github.com/Apipie/apipie-rails/pull/850)[#844](https://github.com/Apipie/apipie-rails/pull/844)[#846](https://github.com/Apipie/apipie-rails/pull/846)[#834](https://github.com/Apipie/apipie-rails/pull/834)[#847](https://github.com/Apipie/apipie-rails/pull/847) (Rubocop Challenger)
20
+ * [Fix] Separate nested resource name [#855](https://github.com/Apipie/apipie-rails/pull/855), [#455](https://github.com/Apipie/apipie-rails/issues/455) (Panos Dalitsouris)
21
+ * [Rubocop] Disable a few Rubocop Rules, run Rubocop with ruby 3.2 [#851](https://github.com/Apipie/apipie-rails/pull/851), [#853](https://github.com/Apipie/apipie-rails/pull/853), [#840](https://github.com/Apipie/apipie-rails/pull/840), [#841](https://github.com/Apipie/apipie-rails/pull/841) (Panos Dalitsouris)
22
+ * [Rubocop] More Rubocop Auto corrections [#858](https://github.com/Apipie/apipie-rails/pull/858), [#849](https://github.com/Apipie/apipie-rails/pull/849), [#850](https://github.com/Apipie/apipie-rails/pull/850), [#844](https://github.com/Apipie/apipie-rails/pull/844), [#846](https://github.com/Apipie/apipie-rails/pull/846), [#834](https://github.com/Apipie/apipie-rails/pull/834), [#847](https://github.com/Apipie/apipie-rails/pull/847) (Rubocop Challenger)
12
23
 
13
24
  ## [v0.9.3](https://github.com/Apipie/apipie-rails/tree/v0.9.3) (2023-03-08)
14
25
  [Full Changelog](https://github.com/Apipie/apipie-rails/compare/v0.9.2...v0.9.3)
15
- * [Feature] Allow Apipie::ParamDescription to be marked as deprecated [#819](https://github.com/Apipie/apipie-rails/pull/819)[#811](https://github.com/Apipie/apipie-rails/pull/811) (Panos Dalitsouris)
26
+ * [Feature] Allow Apipie::ParamDescription to be marked as deprecated [#819](https://github.com/Apipie/apipie-rails/pull/819), [#811](https://github.com/Apipie/apipie-rails/pull/811) (Panos Dalitsouris)
16
27
  * [Fix] Make html markup thread safe ([#822](https://github.com/Apipie/apipie-rails/issues/822)) (Adam Růžička)
17
28
  * [Feature] Allow action matcher strategy to be configured [#821](https://github.com/Apipie/apipie-rails/pull/821) (Panos Dalitsouris)
18
29
  * [CI] Run Rubocop when opening PR [#826](https://github.com/Apipie/apipie-rails/pull/826) (Panos Dalitsouris)
19
30
  * [CI] Green rubocop - Fix after rubocop challenger upgrade [#829](https://github.com/Apipie/apipie-rails/pull/829) (Mathieu Jobin)
20
- * [Rubocop] More Rubocop Auto corrections [#818](https://github.com/Apipie/apipie-rails/pull/818)[#825](https://github.com/Apipie/apipie-rails/pull/825)[#827](https://github.com/Apipie/apipie-rails/pull/827)[#837](https://github.com/Apipie/apipie-rails/pull/837)[#839](https://github.com/Apipie/apipie-rails/pull/839) (Rubocop Challenger)
31
+ * [Rubocop] More Rubocop Auto corrections [#818](https://github.com/Apipie/apipie-rails/pull/818), [#825](https://github.com/Apipie/apipie-rails/pull/825), [#827](https://github.com/Apipie/apipie-rails/pull/827), [#837](https://github.com/Apipie/apipie-rails/pull/837), [#839](https://github.com/Apipie/apipie-rails/pull/839) (Rubocop Challenger)
21
32
 
22
33
  ## [v0.9.2](https://github.com/Apipie/apipie-rails/tree/v0.9.2) (2023-02-07)
23
34
  [Full Changelog](https://github.com/Apipie/apipie-rails/compare/v0.9.1...v0.9.2)
24
- * [Rubocop] More Rubocop Auto corrections [#795](https://github.com/Apipie/apipie-rails/pull/795)[#781](https://github.com/Apipie/apipie-rails/pull/781)[#791](https://github.com/Apipie/apipie-rails/pull/791)[#788](https://github.com/Apipie/apipie-rails/pull/788) (Rubocop Challenger)
35
+ * [Rubocop] More Rubocop Auto corrections [#795](https://github.com/Apipie/apipie-rails/pull/795), [#781](https://github.com/Apipie/apipie-rails/pull/781), [#791](https://github.com/Apipie/apipie-rails/pull/791), [#788](https://github.com/Apipie/apipie-rails/pull/788) (Rubocop Challenger)
25
36
  * [Fix] Can't include translation in full description ([#446](https://github.com/Apipie/apipie-rails/issues/446)) [#808](https://github.com/Apipie/apipie-rails/pull/808) (Peter Nagy)
26
37
  * [Refactor] Move swagger param description creation [#810](https://github.com/Apipie/apipie-rails/pull/810) (Panos Dalitsouris)
27
38
  * [Rubocop] Redo rubocop TODOs, set HashSyntax config to most used style [#814](https://github.com/Apipie/apipie-rails/pull/814) (Mathieu Jobin)
data/README.rst CHANGED
@@ -1205,7 +1205,7 @@ after the ``apipie_validations`` before_action.
1205
1205
 
1206
1206
  TypeValidator
1207
1207
  -------------
1208
- Check the parameter type. Only String, Hash and Array are supported
1208
+ Check the parameter type. Only String, Integer, Hash and Array are supported
1209
1209
  for the sake of simplicity. Read more to find out how to add
1210
1210
  your own validator.
1211
1211
 
@@ -1664,7 +1664,7 @@ Swagger-Specific Configuration Parameters
1664
1664
 
1665
1665
  There are several configuration parameters that determine the structure of the generated swagger file:
1666
1666
 
1667
- ``config.swagger_content_type_input``
1667
+ ``config.generator.swagger.content_type_input``
1668
1668
  If the value is ``:form_data`` - the swagger file will indicate that the server consumes the content types
1669
1669
  ``application/x-www-form-urlencoded`` and ``multipart/form-data``. Non-path parameters will have the
1670
1670
  value ``"in": "formData"``. Note that parameters of type Hash that do not have any fields in them will *be ommitted*
@@ -1677,52 +1677,52 @@ There are several configuration parameters that determine the structure of the g
1677
1677
  You can specify the value of this configuration parameter as an additional input to the rake command (e.g.,
1678
1678
  ``rake apipie:static_swagger_json[2.0,form_data]``).
1679
1679
 
1680
- ``config.swagger_json_input_uses_refs``
1681
- This parameter is only relevant if ``swagger_content_type_input`` is ``:json``.
1680
+ ``config.generator.swagger.json_input_uses_refs``
1681
+ This parameter is only relevant if ``swagger.content_type_input`` is ``:json``.
1682
1682
 
1683
1683
  If ``true``: the schema of the ``"in": "body"`` parameter of each method is given its own entry in the ``definitions``
1684
1684
  section, and is referenced using ``$ref`` from the method definition.
1685
1685
 
1686
1686
  If ``false``: the body parameter definitions are inlined within the method definitions.
1687
1687
 
1688
- ``config.swagger_include_warning_tags``
1688
+ ``config.generator.swagger.include_warning_tags``
1689
1689
  If ``true``: in addition to tagging methods with the name of the resource they belong to, methods for which warnings
1690
1690
  have been issued will be tagged with.
1691
1691
 
1692
- ``config.swagger_suppress_warnings``
1692
+ ``config.generator.swagger.suppress_warnings``
1693
1693
  If ``false``: no warnings will be suppressed
1694
1694
 
1695
1695
  If ``true``: all warnings will be suppressed
1696
1696
 
1697
1697
  If an array of values (e.g., ``[100,102,107]``), only the warnings identified by the numbers in the array will be suppressed.
1698
1698
 
1699
- ``config.swagger_api_host``
1699
+ ``config.generator.swagger.api_host``
1700
1700
  The value to place in the swagger host field.
1701
1701
 
1702
1702
  Default is ``localhost:3000``
1703
1703
 
1704
1704
  If ``nil`` then then host field will not be included.
1705
1705
 
1706
- ``config.swagger_allow_additional_properties_in_response``
1706
+ ``config.generator.swagger.allow_additional_properties_in_response``
1707
1707
  If ``false`` (default): response descriptions in the generated swagger will include an ``additional-properties: false``
1708
1708
  field
1709
1709
 
1710
1710
  If ``true``: the ``additional-properties: false`` field will not be included in response object descriptions
1711
1711
 
1712
- ``config.swagger_schemes``
1712
+ ``config.generator.swagger.schemes``
1713
1713
  An array of transport schemes that the API supports.
1714
1714
  This can include any combination of ``http``, ``https``, ``ws`` and ``wss``.
1715
1715
  By default to encourage good security practices, ``['https']`` is specified.
1716
1716
 
1717
1717
 
1718
- ``config:swagger_security_definitions``
1718
+ ``config:swagger.security_definitions``
1719
1719
  If the API requires authentication, you can specify details of the authentication mechanisms supported as a (Hash) value here.
1720
1720
  See [https://swagger.io/docs/specification/2-0/authentication/] for details of what values can be specified
1721
1721
  By default, no security is defined.
1722
1722
 
1723
- ``config.swagger_global_security``
1723
+ ``config.generator.swagger.global_security``
1724
1724
  If the API requires authentication, you can specify which of the authentication mechanisms are supported by all API operations as an Array of hashes here.
1725
- This should be used in conjunction with the mechanisms defined by ``swagger_security_definitions``.
1725
+ This should be used in conjunction with the mechanisms defined by ``swagger.security_definitions``.
1726
1726
  See [https://swagger.io/docs/specification/2-0/authentication/] for details of what values can be specified
1727
1727
  By default, no security is defined.
1728
1728
 
@@ -39,12 +39,12 @@ module Apipie
39
39
  I18n.locale = @language
40
40
 
41
41
  if should_render_swagger
42
- prev_warning_value = Apipie.configuration.swagger_suppress_warnings
42
+ prev_warning_value = Apipie.configuration.generator.swagger.suppress_warnings
43
43
  begin
44
- Apipie.configuration.swagger_suppress_warnings = true
44
+ Apipie.configuration.generator.swagger.suppress_warnings = true
45
45
  @doc = Apipie.to_swagger_json(params[:version], params[:resource], params[:method], @language)
46
46
  ensure
47
- Apipie.configuration.swagger_suppress_warnings = prev_warning_value
47
+ Apipie.configuration.generator.swagger.suppress_warnings = prev_warning_value
48
48
  end
49
49
  else
50
50
  @doc = Apipie.to_json(params[:version], params[:resource], params[:method], @language)
@@ -119,7 +119,7 @@ module Apipie
119
119
  authorize_resource(resource)
120
120
  end
121
121
  else
122
- @doc[:docs][:resources].select do |_resource_name, resource|
122
+ @doc[:docs][:resources].select do |_resource_id, resource|
123
123
  authorize_resource(resource)
124
124
  end
125
125
  end
@@ -8,14 +8,14 @@ module Apipie
8
8
  @application ||= Apipie::Application.new
9
9
  end
10
10
 
11
- def self.to_json(version = nil, resource_name = nil, method_name = nil, lang = nil)
11
+ def self.to_json(version = nil, resource_id = nil, method_name = nil, lang = nil)
12
12
  version ||= Apipie.configuration.default_version
13
- app.to_json(version, resource_name, method_name, lang)
13
+ app.to_json(version, resource_id, method_name, lang)
14
14
  end
15
15
 
16
- def self.to_swagger_json(version = nil, resource_name = nil, method_name = nil, lang = nil, clear_warnings = true)
16
+ def self.to_swagger_json(version = nil, resource_id = nil, method_name = nil, lang = nil, clear_warnings = true)
17
17
  version ||= Apipie.configuration.default_version
18
- app.to_swagger_json(version, resource_name, method_name, lang, clear_warnings)
18
+ app.to_swagger_json(version, resource_id, method_name, lang, clear_warnings)
19
19
  end
20
20
 
21
21
  def self.json_schema_for_method_response(controller_name, method_name, return_code, allow_nulls)
@@ -25,10 +25,6 @@ module Apipie
25
25
  @resource_descriptions.keys.sort
26
26
  end
27
27
 
28
- def set_resource_id(controller, resource_id)
29
- @controller_to_resource_id[controller] = resource_id
30
- end
31
-
32
28
  def rails_routes(route_set = nil, base_url = "")
33
29
  if route_set.nil? && @rails_routes
34
30
  return @rails_routes
@@ -83,8 +79,8 @@ module Apipie
83
79
  versions = controller_versions(controller) if versions.empty?
84
80
 
85
81
  versions.each do |version|
86
- resource_name_with_version = "#{version}##{get_resource_name(controller)}"
87
- resource_description = get_resource_description(resource_name_with_version)
82
+ resource_id_with_version = "#{version}##{get_resource_id(controller)}"
83
+ resource_description = get_resource_description(resource_id_with_version)
88
84
 
89
85
  if resource_description.nil?
90
86
  resource_description = define_resource_description(controller, version)
@@ -100,24 +96,24 @@ module Apipie
100
96
  resource_description.add_method_description(method_description)
101
97
  end
102
98
 
103
- return ret_method_description
99
+ ret_method_description
104
100
  end
105
101
 
106
102
  # create new resource api description
107
103
  def define_resource_description(controller, version, dsl_data = nil)
108
104
  return if ignored?(controller)
109
105
 
110
- resource_name = get_resource_name(controller)
111
- resource_description = @resource_descriptions[version][resource_name]
106
+ resource_id = get_resource_id(controller)
107
+ resource_description = @resource_descriptions[version][resource_id]
112
108
  if resource_description
113
109
  # we already defined the description somewhere (probably in
114
110
  # some method. Updating just meta data from dsl
115
111
  resource_description.update_from_dsl_data(dsl_data) if dsl_data
116
112
  else
117
- resource_description = Apipie::ResourceDescription.new(controller, resource_name, dsl_data, version)
113
+ resource_description = Apipie::ResourceDescription.new(controller, resource_id, dsl_data, version)
118
114
 
119
- Apipie.debug("@resource_descriptions[#{version}][#{resource_name}] = #{resource_description}")
120
- @resource_descriptions[version][resource_name] ||= resource_description
115
+ Apipie.debug("@resource_descriptions[#{version}][#{resource_id}] = #{resource_description}")
116
+ @resource_descriptions[version][resource_id] ||= resource_description
121
117
  end
122
118
 
123
119
  return resource_description
@@ -158,28 +154,28 @@ module Apipie
158
154
  #
159
155
  # There are two ways how this method can be used:
160
156
  # 1) Specify both parameters
161
- # resource_name:
157
+ # resource_id:
162
158
  # controller class - UsersController
163
159
  # string with resource name (plural) and version - "v1#users"
164
160
  # method_name: name of the method (string or symbol)
165
161
  #
166
162
  # 2) Specify only first parameter:
167
- # resource_name: string containing both resource and method name joined
163
+ # resource_id: string containing both resource and method name joined
168
164
  # with '#' symbol.
169
165
  # - "users#create" get default version
170
166
  # - "v2#users#create" get specific version
171
- def get_method_description(resource_name, method_name = nil)
172
- if resource_name.is_a?(String)
173
- crumbs = resource_name.split('#')
167
+ def get_method_description(resource_id, method_name = nil)
168
+ if resource_id.is_a?(String)
169
+ crumbs = resource_id.split('#')
174
170
  if method_name.nil?
175
171
  method_name = crumbs.pop
176
172
  end
177
- resource_name = crumbs.join("#")
178
- resource_description = get_resource_description(resource_name)
179
- elsif resource_name.respond_to? :apipie_resource_descriptions
180
- resource_description = get_resource_description(resource_name)
173
+ resource_id = crumbs.join("#")
174
+ resource_description = get_resource_description(resource_id)
175
+ elsif resource_id.respond_to? :apipie_resource_descriptions
176
+ resource_description = get_resource_description(resource_id)
181
177
  else
182
- raise ArgumentError.new("Resource #{resource_name} does not exists.")
178
+ raise ArgumentError.new("Resource #{resource_id} does not exists.")
183
179
  end
184
180
  resource_description&.method_description(method_name.to_sym)
185
181
  end
@@ -200,15 +196,15 @@ module Apipie
200
196
  return @resource_descriptions[version][crumbs.last]
201
197
  end
202
198
  else
203
- resource_name = get_resource_name(resource)
199
+ resource_id = get_resource_id(resource)
204
200
  if version
205
- resource_name = "#{version}##{resource_name}"
201
+ resource_id = "#{version}##{resource_id}"
206
202
  end
207
203
 
208
- if resource_name.nil?
204
+ if resource_id.nil?
209
205
  return nil
210
206
  end
211
- resource_description = get_resource_description(resource_name)
207
+ resource_description = get_resource_description(resource_id)
212
208
  if resource_description && resource_description.controller.to_s == resource.to_s
213
209
  return resource_description
214
210
  end
@@ -231,7 +227,7 @@ module Apipie
231
227
 
232
228
  def remove_method_description(resource, versions, method_name)
233
229
  versions.each do |version|
234
- resource = get_resource_name(resource)
230
+ resource = resource_id(resource)
235
231
  if resource_description = get_resource_description("#{version}##{resource}")
236
232
  resource_description.remove_method_description(method_name)
237
233
  end
@@ -243,7 +239,6 @@ module Apipie
243
239
  @resource_descriptions = ActiveSupport::HashWithIndifferentAccess.new { |h, version| h[version] = {} }
244
240
  @controller_to_resource_id = {}
245
241
  @param_groups = {}
246
- @swagger_generator = Apipie::SwaggerGenerator.new(self)
247
242
 
248
243
  # what versions does the controller belong in (specified by resource_description)?
249
244
  @controller_versions = Hash.new { |h, controller| h[controller.to_s] = [] }
@@ -261,36 +256,41 @@ module Apipie
261
256
  def json_schema_for_method_response(version, controller_name, method_name, return_code, allow_nulls)
262
257
  method = @resource_descriptions[version][controller_name].method_description(method_name)
263
258
  raise NoDocumentedMethod.new(controller_name, method_name) if method.nil?
264
- @swagger_generator.json_schema_for_method_response(method, return_code, allow_nulls)
259
+
260
+ Apipie::SwaggerGenerator
261
+ .json_schema_for_method_response(method, return_code, allow_nulls)
265
262
  end
266
263
 
267
264
  def json_schema_for_self_describing_class(cls, allow_nulls)
268
- @swagger_generator.json_schema_for_self_describing_class(cls, allow_nulls)
265
+ Apipie::SwaggerGenerator
266
+ .json_schema_for_self_describing_class(cls, allow_nulls)
269
267
  end
270
268
 
271
- def to_swagger_json(version, resource_name, method_name, lang, clear_warnings = false)
272
- return unless valid_search_args?(version, resource_name, method_name)
269
+ def to_swagger_json(version, resource_id, method_name, language, clear_warnings = false)
270
+ return unless valid_search_args?(version, resource_id, method_name)
273
271
 
274
- # if resource_name is blank, take just resources which have some methods because
275
- # we dont want to show eg ApplicationController as resource
276
- # otherwise, take only the specified resource
277
- _resources = resource_descriptions[version].inject({}) do |result, (k,v)|
278
- if resource_name.blank?
279
- result[k] = v unless v._methods.blank?
280
- else
281
- result[k] = v if k == resource_name
282
- end
283
- result
284
- end
272
+ resources =
273
+ Apipie::Generator::Swagger::ResourceDescriptionsCollection
274
+ .new(resource_descriptions)
275
+ .filter(
276
+ resource_id: resource_id,
277
+ method_name: method_name,
278
+ version: version
279
+ )
285
280
 
286
- @swagger_generator.generate_from_resources(version,_resources, method_name, lang, clear_warnings)
281
+ Apipie::SwaggerGenerator.generate_from_resources(
282
+ resources,
283
+ version: version,
284
+ language: language,
285
+ clear_warnings: clear_warnings
286
+ )
287
287
  end
288
288
 
289
- def to_json(version, resource_name, method_name, lang)
289
+ def to_json(version, resource_id, method_name, lang)
290
290
 
291
- return unless valid_search_args?(version, resource_name, method_name)
291
+ return unless valid_search_args?(version, resource_id, method_name)
292
292
 
293
- _resources = if resource_name.blank?
293
+ _resources = if resource_id.blank?
294
294
  # take just resources which have some methods because
295
295
  # we dont want to show eg ApplicationController as resource
296
296
  resource_descriptions[version].inject({}) do |result, (k,v)|
@@ -298,7 +298,7 @@ module Apipie
298
298
  result
299
299
  end
300
300
  else
301
- [@resource_descriptions[version][resource_name].to_json(method_name, lang)]
301
+ [@resource_descriptions[version][resource_id].to_json(method_name, lang)]
302
302
  end
303
303
 
304
304
  url_args = Apipie.configuration.version_in_url ? version : ''
@@ -368,7 +368,23 @@ module Apipie
368
368
  Apipie.configuration.validate? || ! Apipie.configuration.use_cache? || Apipie.configuration.force_dsl?
369
369
  end
370
370
 
371
+ # @deprecated Use {#get_resource_id} instead
371
372
  def get_resource_name(klass)
373
+ ActiveSupport::Deprecation.warn(
374
+ <<~HEREDOC
375
+ Apipie::Application.get_resource_name is deprecated.
376
+ Use `Apipie::Application.get_resource_id instead.
377
+ HEREDOC
378
+ )
379
+
380
+ get_resource_id(klass)
381
+ end
382
+
383
+ def set_resource_id(controller, resource_id)
384
+ @controller_to_resource_id[controller] = resource_id
385
+ end
386
+
387
+ def get_resource_id(klass)
372
388
  if klass.class == String
373
389
  klass
374
390
  elsif @controller_to_resource_id.key?(klass)
@@ -413,14 +429,14 @@ module Apipie
413
429
 
414
430
  private
415
431
 
416
- # Make sure that the version/resource_name/method_name are valid combination
417
- # resource_name and method_name can be nil
418
- def valid_search_args?(version, resource_name, method_name)
432
+ # Make sure that the version/resource_id/method_name are valid combination
433
+ # resource_id and method_name can be nil
434
+ def valid_search_args?(version, resource_id, method_name)
419
435
  return false unless self.resource_descriptions.key?(version)
420
- if resource_name
421
- return false unless self.resource_descriptions[version].key?(resource_name)
436
+ if resource_id
437
+ return false unless self.resource_descriptions[version].key?(resource_id)
422
438
  if method_name
423
- resource_description = self.resource_descriptions[version][resource_name]
439
+ resource_description = self.resource_descriptions[version][resource_id]
424
440
  return false unless resource_description.valid_method_name?(method_name)
425
441
  end
426
442
  end
@@ -1,5 +1,6 @@
1
1
  module Apipie
2
2
  class Configuration
3
+ extend Forwardable
3
4
 
4
5
  attr_accessor :app_name, :app_info, :copyright, :compress_examples,
5
6
  :markup, :disqus_shortname,
@@ -8,19 +9,21 @@ module Apipie
8
9
  :validate, :validate_value, :validate_presence, :validate_key, :action_on_non_validated_keys, :authenticate, :doc_path,
9
10
  :show_all_examples, :process_params, :update_checksum, :checksum_path,
10
11
  :link_extension, :record, :languages, :translate, :locale, :default_locale,
11
- :persist_show_in_doc, :authorize, :ignore_allow_blank_false,
12
- :swagger_include_warning_tags, :swagger_content_type_input, :swagger_json_input_uses_refs,
13
- :swagger_suppress_warnings, :swagger_api_host, :swagger_generate_x_computed_id_field,
14
- :swagger_allow_additional_properties_in_response, :swagger_responses_use_refs,
15
- :swagger_schemes, :swagger_security_definitions, :swagger_global_security
12
+ :persist_show_in_doc, :authorize, :ignore_allow_blank_false
13
+
14
+ def_delegators :swagger, *Apipie::Generator::Swagger::Config.deprecated_methods
15
+
16
+ def swagger
17
+ Apipie::Generator::Swagger::Config.instance
18
+ end
19
+
20
+ def generator
21
+ Apipie::Generator::Config.instance
22
+ end
16
23
 
17
24
  alias validate? validate
18
25
  alias required_by_default? required_by_default
19
26
  alias namespaced_resources? namespaced_resources
20
- alias swagger_include_warning_tags? swagger_include_warning_tags
21
- alias swagger_json_input_uses_refs? swagger_json_input_uses_refs
22
- alias swagger_responses_use_refs? swagger_responses_use_refs
23
- alias swagger_generate_x_computed_id_field? swagger_generate_x_computed_id_field
24
27
 
25
28
  # matcher to be used in Dir.glob to find controllers to be reloaded e.g.
26
29
  #
@@ -55,21 +58,22 @@ module Apipie
55
58
 
56
59
  def reload_controllers?
57
60
  @reload_controllers = Rails.env.development? unless defined? @reload_controllers
58
- return @reload_controllers && @api_controllers_matcher
61
+
62
+ @reload_controllers && @api_controllers_matcher
59
63
  end
60
64
 
61
65
  def validate_value
62
- return (validate? && @validate_value)
66
+ validate? && @validate_value
63
67
  end
64
68
  alias validate_value? validate_value
65
69
 
66
70
  def validate_presence
67
- return (validate? && @validate_presence)
71
+ validate? && @validate_presence
68
72
  end
69
73
  alias validate_presence? validate_presence
70
74
 
71
75
  def validate_key
72
- return (validate? && @validate_key)
76
+ validate? && @validate_key
73
77
  end
74
78
  alias validate_key? validate_key
75
79
 
@@ -77,7 +81,7 @@ module Apipie
77
81
  @process_params
78
82
  end
79
83
  # set to true if you want to use pregenerated documentation cache and avoid
80
- # generating the documentation on runtime (usefull for production
84
+ # generating the documentation on runtime (useful for production
81
85
  # environment).
82
86
  # You can generate the cache by running
83
87
  #
@@ -109,7 +113,7 @@ module Apipie
109
113
  end
110
114
 
111
115
  # array of controller names (strings) (might include actions as well)
112
- # to be ignored # when generationg the documentation
116
+ # to be ignored # when generating the documentation
113
117
  # e.g. %w[Api::CommentsController Api::PostsController#post]
114
118
  attr_writer :ignored
115
119
  def ignored
@@ -187,17 +191,6 @@ module Apipie
187
191
  @translate = lambda { |str, locale| str }
188
192
  @persist_show_in_doc = false
189
193
  @routes_formatter = RoutesFormatter.new
190
- @swagger_content_type_input = :form_data # this can be :json or :form_data
191
- @swagger_json_input_uses_refs = false
192
- @swagger_include_warning_tags = false
193
- @swagger_suppress_warnings = false #[105,100,102]
194
- @swagger_api_host = "localhost:3000"
195
- @swagger_generate_x_computed_id_field = false
196
- @swagger_allow_additional_properties_in_response = false
197
- @swagger_responses_use_refs = true
198
- @swagger_schemes = [:https]
199
- @swagger_security_definitions = {}
200
- @swagger_global_security = []
201
194
  end
202
195
  end
203
196
  end
@@ -51,9 +51,6 @@ module Apipie
51
51
  end
52
52
 
53
53
  module Resource
54
- # by default, the resource id is derived from controller_name
55
- # it can be overwritten with.
56
- #
57
54
  # resource_id "my_own_resource_id"
58
55
  def resource_id(resource_id)
59
56
  Apipie.set_resource_id(@controller, resource_id)
@@ -529,8 +526,10 @@ module Apipie
529
526
  alias apipie_update_params apipie_update_methods
530
527
 
531
528
  def _apipie_concern_subst
532
- @_apipie_concern_subst ||= {:controller_path => self.controller_path,
533
- :resource_id => Apipie.get_resource_name(self)}
529
+ @_apipie_concern_subst ||= {
530
+ controller_path: self.controller_path,
531
+ resource_id: Apipie.get_resource_id(self)
532
+ }
534
533
  end
535
534
 
536
535
  def _apipie_perform_concern_subst(string)
@@ -19,7 +19,7 @@ module Apipie
19
19
  def ignore_call?(record)
20
20
  return true unless record[:controller]
21
21
  return true if @ignored.include?(record[:controller].name)
22
- return true if @ignored.include?("#{Apipie.get_resource_name(record[:controller].name)}##{record[:action]}")
22
+ return true if @ignored.include?("#{Apipie.resource_id(record[:controller].name)}##{record[:action]}")
23
23
  return true unless @api_controllers_paths.include?(controller_full_path(record[:controller]))
24
24
  end
25
25
 
@@ -33,7 +33,7 @@ module Apipie
33
33
  end
34
34
 
35
35
  def add_to_records(record)
36
- key = "#{Apipie.get_resource_name(record[:controller])}##{record[:action]}"
36
+ key = "#{Apipie.get_resource_id(record[:controller])}##{record[:action]}"
37
37
  @records[key] << record
38
38
  end
39
39
 
@@ -154,7 +154,7 @@ module Apipie
154
154
  def update_api_descriptions
155
155
  apis_from_docs = all_apis_from_docs
156
156
  @apis_from_routes.each do |(controller, action), new_apis|
157
- method_key = "#{Apipie.get_resource_name(controller.safe_constantize || next)}##{action}"
157
+ method_key = "#{Apipie.get_resource_id(controller.safe_constantize || next)}##{action}"
158
158
  old_apis = apis_from_docs[method_key] || []
159
159
  new_apis.each do |new_api|
160
160
  new_api[:path]&.sub!(/\(\.:format\)$/,"")
@@ -0,0 +1,12 @@
1
+ module Apipie
2
+ module Generator
3
+ # Configuration interface for generators
4
+ class Config
5
+ include Singleton
6
+
7
+ def swagger
8
+ Apipie::Generator::Swagger::Config.instance
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,23 @@
1
+ # The {Apipie::Generator::Swagger::ComputedInterfaceId.id} is a number that is
2
+ # uniquely derived from the list of operations added to the swagger definition (in an order-dependent way).
3
+ # it can be used for regression testing, allowing some differentiation between changes that
4
+ # result from changes to the input and those that result from changes to the generation
5
+ # algorithms.
6
+ #
7
+ # @note At the moment, this only takes operation ids into account, and ignores
8
+ # parameter definitions, so it's only partially useful.
9
+ class Apipie::Generator::Swagger::ComputedInterfaceId
10
+ include Singleton
11
+
12
+ def initialize
13
+ @computed_interface_id = 0
14
+ end
15
+
16
+ def add!(operation_id)
17
+ @computed_interface_id = Zlib.crc32("#{@computed_interface_id} #{operation_id}")
18
+ end
19
+
20
+ def id
21
+ @computed_interface_id
22
+ end
23
+ end