Skip to content

Commit

Permalink
feat: Support plain text responses
Browse files Browse the repository at this point in the history
  • Loading branch information
mattbearman committed May 14, 2024
1 parent fb1843d commit c3e0c6a
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
3 changes: 3 additions & 0 deletions examples/core_api/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
Expand Down
16 changes: 16 additions & 0 deletions examples/core_api/endpoints/plain_text_endpoint.rb
Original file line number Diff line number Diff line change
@@ -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
14 changes: 10 additions & 4 deletions lib/apia/open_api/objects/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -46,17 +47,22 @@ 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?

@route_spec[:responses] = {
"#{@http_status}": {
description: @endpoint.definition.description || "",
content: {
"application/json": {
@response_type => {
schema: content_schema
}
}
Expand Down
26 changes: 26 additions & 0 deletions spec/support/fixtures/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit c3e0c6a

Please # to comment.