diff --git a/Gemfile b/Gemfile index f0b1012..3728b11 100644 --- a/Gemfile +++ b/Gemfile @@ -5,7 +5,7 @@ source "https://rubygems.org" # Specify your gem's dependencies in apia-open_api.gemspec gemspec -gem "apia", "~> 3.5" +gem "apia", "~> 3.6" gem "rake", "~> 13.0" gem "rspec", "~> 3.0" gem "rubocop", "~> 1.21" diff --git a/examples/core_api/base.rb b/examples/core_api/base.rb index 21fb169..a77c446 100644 --- a/examples/core_api/base.rb +++ b/examples/core_api/base.rb @@ -3,6 +3,7 @@ require "core_api/authenticators/main_authenticator" require "core_api/controllers/time_controller" require "core_api/endpoints/test_endpoint" +require "core_api/endpoints/plain_text_endpoint" require "core_api/endpoints/legacy_endpoint" module CoreAPI @@ -24,6 +25,8 @@ class Base < Apia::API post "example/format", controller: Controllers::TimeController, endpoint: :format post "example/format_multiple", controller: Controllers::TimeController, endpoint: :format_multiple + get "plain_text", endpoint: Endpoints::PlainTextEndpoint + group :time do name "Time functions" description "Everything related to time elements" diff --git a/examples/core_api/endpoints/plain_text_endpoint.rb b/examples/core_api/endpoints/plain_text_endpoint.rb new file mode 100644 index 0000000..730668c --- /dev/null +++ b/examples/core_api/endpoints/plain_text_endpoint.rb @@ -0,0 +1,16 @@ +# frozen_string_literal: true + +module CoreAPI + module Endpoints + class PlainTextEndpoint < Apia::Endpoint + + description "Return a plain text response" + response_type Apia::Response::PLAIN + + def call + response.body = "hello world!" + end + + end + end +end diff --git a/lib/apia/open_api/objects/response.rb b/lib/apia/open_api/objects/response.rb index cfb33e1..9504315 100644 --- a/lib/apia/open_api/objects/response.rb +++ b/lib/apia/open_api/objects/response.rb @@ -36,6 +36,7 @@ def initialize(spec:, path_ids:, route:, route_spec:, api_authenticator:) @route_spec = route_spec @api_authenticator = api_authenticator @http_status = @endpoint.definition.http_status + @response_type = @endpoint.definition.response_type end def add_to_spec @@ -46,9 +47,14 @@ def add_to_spec private def add_sucessful_response_schema - content_schema = { - properties: generate_properties_for_successful_response - } + if @response_type == Apia::Response::PLAIN + content_schema = { type: "string" } + else + content_schema = { + properties: generate_properties_for_successful_response + } + end + required_fields = @endpoint.definition.fields.select { |_, field| field.condition.nil? } content_schema[:required] = required_fields.keys if required_fields.any? @@ -56,7 +62,7 @@ def add_sucessful_response_schema "#{@http_status}": { description: @endpoint.definition.description || "", content: { - "application/json": { + @response_type => { schema: content_schema } } diff --git a/spec/support/fixtures/openapi.json b/spec/support/fixtures/openapi.json index d50cdc6..59dbafa 100644 --- a/spec/support/fixtures/openapi.json +++ b/spec/support/fixtures/openapi.json @@ -198,6 +198,32 @@ } } }, + "/plain_text": { + "get": { + "operationId": "get:plain_text", + "tags": [ + "Core" + ], + "parameters": [ + + ], + "responses": { + "200": { + "description": "Return a plain text response", + "content": { + "text/plain": { + "schema": { + "type": "string" + } + } + } + }, + "403": { + "$ref": "#/components/responses/APIAuthenticator403Response" + } + } + } + }, "/time/now": { "get": { "operationId": "get:time_now",