From 9c297317b2eeb4b4ebaca76c85210fddd56d97f2 Mon Sep 17 00:00:00 2001 From: Oleg Yakovenko Date: Thu, 4 Jun 2020 16:21:43 +0300 Subject: [PATCH 01/17] keep examples content --- README.md | 43 ++++++++++--------- .../lib/generators/rspec/templates/spec.rb | 6 ++- .../lib/rswag/specs/swagger_formatter.rb | 4 +- 3 files changed, 30 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index fd3f05c..a0c5024 100644 --- a/README.md +++ b/README.md @@ -363,8 +363,8 @@ you should use the folowing syntax, making sure there are no whitespaces at the ### Specifying/Testing API Security ### -Swagger allows for the specification of different security schemes and their applicability to operations in an API. -To leverage this in rswag, you define the schemes globally in _swagger_helper.rb_ and then use the "security" attribute at the operation level to specify which schemes, if any, are applicable to that operation. +Swagger allows for the specification of different security schemes and their applicability to operations in an API. +To leverage this in rswag, you define the schemes globally in _swagger_helper.rb_ and then use the "security" attribute at the operation level to specify which schemes, if any, are applicable to that operation. Swagger supports :basic, :bearer, :apiKey and :oauth2 and :openIdConnect scheme types. See [the spec](https://swagger.io/docs/specification/authentication/) for more info, as this underwent major changes between Swagger 2.0 and Open API 3.0 ```ruby @@ -416,7 +416,7 @@ describe 'Blogs API' do end # example of documenting an endpoint that handles basic auth and api key based security -describe 'Auth examples API' do +describe 'Auth examples API' do path '/auth-tests/basic-and-api-key' do post 'Authenticates with basic auth and api key' do tags 'Auth Tests' @@ -437,11 +437,11 @@ describe 'Auth examples API' do end end end - + ``` -__NOTE:__ Depending on the scheme types, you'll be required to assign a corresponding parameter value with each example. +__NOTE:__ Depending on the scheme types, you'll be required to assign a corresponding parameter value with each example. For example, :basic auth is required above and so the :Authorization (header) parameter must be set accordingly ## Configuration & Customization ## @@ -479,9 +479,9 @@ rake rswag:specs:swaggerize PATTERN="spec/swagger/**/*_spec.rb" ### Referenced Parameters and Schema Definitions ### -Swagger allows you to describe JSON structures inline with your operation descriptions OR as referenced globals. +Swagger allows you to describe JSON structures inline with your operation descriptions OR as referenced globals. For example, you might have a standard response structure for all failed operations. -Again, this is a structure that changed since swagger 2.0. Notice the new "schemas" section for these. +Again, this is a structure that changed since swagger 2.0. Notice the new "schemas" section for these. Rather than repeating the schema in every operation spec, you can define it globally and provide a reference to it in each spec: ```ruby @@ -549,7 +549,7 @@ end ### Response headers ### -In Rswag, you could use `header` method inside the response block to specify header objects for this response. +In Rswag, you could use `header` method inside the response block to specify header objects for this response. Rswag will validate your response headers with those header objects and inject them into the generated swagger file: ```ruby @@ -597,16 +597,20 @@ To enable examples generation from responses add callback above run_test! like: ``` after do |example| - example.metadata[:response][:examples] = { 'application/json' => JSON.parse(response.body, symbolize_names: true) } + example.metadata[:response][:content] = { + 'application/json' => { + example: JSON.parse(response.body, symbolize_names: true) + } + } end ``` You need to disable --dry-run option for Rspec > 3 - Add to config/environments/test.rb: @@ -645,8 +649,8 @@ describe 'Blogs API', document: false do ``` ##### rswag helper methods ##### -