From 82a5eea386861c9a89b698913db51938313baf67 Mon Sep 17 00:00:00 2001 From: khiav reoy Date: Thu, 16 Apr 2020 05:45:09 +0800 Subject: [PATCH 1/2] Fix: params is not the same in RESTful APIs with different verbs --- .../views/api_blueprint_index.rb | 7 ++--- .../api_blueprint_index.mustache | 26 +++++++++---------- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/lib/rspec_api_documentation/views/api_blueprint_index.rb b/lib/rspec_api_documentation/views/api_blueprint_index.rb index ef42c1fa..2a343c85 100644 --- a/lib/rspec_api_documentation/views/api_blueprint_index.rb +++ b/lib/rspec_api_documentation/views/api_blueprint_index.rb @@ -10,11 +10,14 @@ def sections super.map do |section| routes = section[:examples].group_by { |e| "#{e.route_uri}#{e.route_optionals}#{e.route_name}" }.map do |route, examples| attrs = fields(:attributes, examples) - params = fields(:parameters, examples) methods = examples.group_by(&:http_method).map do |http_method, examples| + params = fields(:parameters, examples) + { + "has_parameters?".to_sym => params.size > 0, http_method: http_method, + parameters: params, description: examples.first.respond_to?(:action_name) && examples.first.action_name, examples: examples } @@ -22,11 +25,9 @@ def sections { "has_attributes?".to_sym => attrs.size > 0, - "has_parameters?".to_sym => params.size > 0, route: format_route(examples[0]), route_name: examples[0][:route_name], attributes: attrs, - parameters: params, http_methods: methods } end diff --git a/templates/rspec_api_documentation/api_blueprint_index.mustache b/templates/rspec_api_documentation/api_blueprint_index.mustache index 8100200e..d0111485 100644 --- a/templates/rspec_api_documentation/api_blueprint_index.mustache +++ b/templates/rspec_api_documentation/api_blueprint_index.mustache @@ -23,10 +23,10 @@ description: {{ description }} explanation: {{ explanation }} {{/ explanation }} -{{# has_parameters? }} +{{# has_attributes? }} -+ Parameters -{{# parameters }} ++ Attributes (object) +{{# attributes }} + {{ name }}{{# example }}: {{ example }}{{/ example }}{{# properties_description }} ({{ properties_description }}){{/ properties_description }}{{# description }} - {{ description }}{{/ description }} {{# has_default?}} + Default: `{{default}}` @@ -40,12 +40,15 @@ explanation: {{ explanation }} {{# annotations }} {{ . }} {{/ annotations }} -{{/ parameters }} -{{/ has_parameters? }} -{{# has_attributes? }} +{{/ attributes }} +{{/ has_attributes? }} +{{# http_methods }} -+ Attributes (object) -{{# attributes }} +### {{ description }} [{{ http_method }}] +{{# has_parameters? }} + ++ Parameters +{{# parameters }} + {{ name }}{{# example }}: {{ example }}{{/ example }}{{# properties_description }} ({{ properties_description }}){{/ properties_description }}{{# description }} - {{ description }}{{/ description }} {{# has_default?}} + Default: `{{default}}` @@ -59,11 +62,8 @@ explanation: {{ explanation }} {{# annotations }} {{ . }} {{/ annotations }} -{{/ attributes }} -{{/ has_attributes? }} -{{# http_methods }} - -### {{ description }} [{{ http_method }}] +{{/ parameters }} +{{/ has_parameters? }} {{# examples }} {{# requests }} {{# has_request? }} From 657cc470cbf63c49e19bfc0f53d969a46afc7102 Mon Sep 17 00:00:00 2001 From: khiav reoy Date: Fri, 11 Sep 2020 12:27:07 +0800 Subject: [PATCH 2/2] Fix test cases --- spec/views/api_blueprint_index_spec.rb | 44 ++++++++++++++++++-------- 1 file changed, 30 insertions(+), 14 deletions(-) diff --git a/spec/views/api_blueprint_index_spec.rb b/spec/views/api_blueprint_index_spec.rb index 1d526597..d99943b6 100644 --- a/spec/views/api_blueprint_index_spec.rb +++ b/spec/views/api_blueprint_index_spec.rb @@ -98,28 +98,36 @@ post_route = sections[1][:routes][1] post_route_with_optionals = sections[1][:routes][2] - comments_examples = comments_route[:http_methods].map { |http_method| http_method[:examples] }.flatten + http_methods = comments_route[:http_methods] + comments_examples = http_methods.map { |http_method| http_method[:examples] }.flatten + examples_has_parameters = http_methods.map { |http_method| http_method[:has_parameters?] } + examples_parameters = http_methods.map { |http_method| http_method[:parameters] } + expect(comments_examples.size).to eq 1 expect(comments_route[:route]).to eq "/comments" expect(comments_route[:route_name]).to eq "Comments Collection" - expect(comments_route[:has_parameters?]).to eq false - expect(comments_route[:parameters]).to eq [] + expect(examples_has_parameters).to eq [false] + expect(examples_parameters).to eq [[]] expect(comments_route[:has_attributes?]).to eq false expect(comments_route[:attributes]).to eq [] - post_examples = post_route[:http_methods].map { |http_method| http_method[:examples] }.flatten + http_methods = post_route[:http_methods] + post_examples = http_methods.map { |http_method| http_method[:examples] }.flatten + examples_has_parameters = http_methods.map { |http_method| http_method[:has_parameters?] } + examples_parameters = http_methods.map { |http_method| http_method[:parameters] } + expect(post_examples.size).to eq 2 expect(post_route[:route]).to eq "/posts/{id}" expect(post_route[:route_name]).to eq "Single Post" - expect(post_route[:has_parameters?]).to eq true - expect(post_route[:parameters]).to eq [{ + expect(examples_has_parameters).to eq [true] + expect(examples_parameters).to eq [[{ required: true, type: "string", example: "1", name: "id", description: "The id", properties_description: "required, string" - }] + }]] expect(post_route[:has_attributes?]).to eq true expect(post_route[:attributes]).to eq [{ required: true, @@ -128,12 +136,16 @@ properties_description: "required" }] - post_w_optionals_examples = post_route_with_optionals[:http_methods].map { |http_method| http_method[:examples] }.flatten + http_methods = post_route_with_optionals[:http_methods] + post_w_optionals_examples = http_methods.map { |http_method| http_method[:examples] }.flatten + examples_has_parameters = http_methods.map { |http_method| http_method[:has_parameters?] } + examples_parameters = http_methods.map { |http_method| http_method[:parameters] } + expect(post_w_optionals_examples.size).to eq 1 expect(post_route_with_optionals[:route]).to eq "/posts/{id}{?option=:option}" expect(post_route_with_optionals[:route_name]).to eq "Single Post" - expect(post_route_with_optionals[:has_parameters?]).to eq true - expect(post_route_with_optionals[:parameters]).to eq [{ + expect(examples_has_parameters).to eq [true] + expect(examples_parameters).to eq [[{ required: true, type: "string", example: "1", @@ -144,16 +156,20 @@ name: "option", description: nil, properties_description: 'optional' - }] + }]] expect(post_route_with_optionals[:has_attributes?]).to eq false expect(post_route_with_optionals[:attributes]).to eq [] - posts_examples = posts_route[:http_methods].map { |http_method| http_method[:examples] }.flatten + http_methods = posts_route[:http_methods] + posts_examples = http_methods.map { |http_method| http_method[:examples] }.flatten + examples_has_parameters = http_methods.map { |http_method| http_method[:has_parameters?] } + examples_parameters = http_methods.map { |http_method| http_method[:parameters] } + expect(posts_examples.size).to eq 1 expect(posts_route[:route]).to eq "/posts" expect(posts_route[:route_name]).to eq "Posts Collection" - expect(posts_route[:has_parameters?]).to eq false - expect(posts_route[:parameters]).to eq [] + expect(examples_has_parameters).to eq [false] + expect(examples_parameters).to eq [[]] expect(posts_route[:has_attributes?]).to eq true expect(posts_route[:attributes]).to eq [{ required: false,