Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support more options for openapi3 responses #59

Merged
merged 5 commits into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 32 additions & 23 deletions lib/tomograph/openapi/openapi3.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,46 +9,55 @@ def initialize(prefix, openapi3_yaml_path)
end

def to_tomogram
@tomogram ||= @documentation['paths'].each_with_object([]) do |action, result|
action[1].keys.each do |method|
@tomogram ||= @documentation['paths'].each_with_object([]) do |(path, action_definition), result|
action_definition.keys.each do |method|
result.push(Tomograph::Tomogram::Action.new(
path: "#{@prefix}#{action[0]}",
path: "#{@prefix}#{path}",
method: method.upcase,
content_type: '',
requests: [],
responses: responses(action[1][method]['responses'], @documentation['components']['schemas']),
responses: responses(action_definition[method]['responses']),
resource: ''
))
end
end
end

def responses(resp, defi)
resp.inject([]) do |result, response|
if response[1]['content'].nil?
# TODO: 403Forbidden
result.push(
'status' => response[0],
'body' => {},
'content-type' => 'application/json'
)
elsif response[1]['content'].values[0]['schema']
def responses(responses_definitions)
result = []
responses_definitions.each do |(response_code, response)|
# response can be either Response Object or Reference Object
if response.key?('$ref')
response_description_path = response['$ref'].split('/')[1..] # first one is a '#'
response['content'] = @documentation.dig(*response_description_path)['content']
end

# Content can be nil if response body is not provided
if response['content'].nil?
result.push(
'status' => response[0],
'body' => schema(response[1]['content'].values[0]['schema'], defi),
'content-type' => 'application/json'
'status' => response_code,
'body'=> {},
'content-type' => ''
)
else
result.push(
status: response[0],
body: {},
'content-type': ''
)
result += responses_by_content_types(response['content'], response_code)
end
end
result
end

def responses_by_content_types(content_types, response_code)
content_types.map do |content_type, media_type_obj|
{
'status' => response_code,
'body' => schema(media_type_obj['schema']),
'content-type' => content_type
}
end
end

def schema(sche, defi)
def schema(sche)
defi = @documentation['components']['schemas']
if sche.keys.include?('$ref')
sche.merge!('components' => {})
sche['components'].merge!('schemas' => {})
Expand Down
44 changes: 44 additions & 0 deletions spec/fixtures/openapi3/one_code_two_content_types.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
[
{
"path": "/accounts/{accountId}",
"method": "GET",
"content-type": "",
"requests": [
],
"responses": [
{
"status": "200",
"body": {
"$ref": "#/components/schemas/Account",
"components": {
"schemas": {
"Account": {
"description": "Account data",
"type": "object",
"parameters": {
"id": {
"type": "integer",
"required": true
},
"name": {
"type": "string",
"required": true
}
}
}
}
}
},
"content-type": "application/json"
},
{
"status": "200",
"body": {
"type": "string"
},
"content-type": "text/plain"
}
],
"resource": ""
}
]
41 changes: 41 additions & 0 deletions spec/fixtures/openapi3/one_code_two_content_types.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
openapi: 3.1.3
servers:
- url: 'http://localhost:3000/api/v2/storefront'
info:
version: 2.0.0
title: Test API
description: API for testing specs. It has two responses with same code but different content-types

paths:
/accounts/{accountId}:
get:
summary: Get account data
parameters:
- name: accountId
in: query
schema:
type: integer
required: true
responses:
'200':
description: Account data
content:
application/json:
schema:
$ref: '#/components/schemas/Account'
text/plain:
schema:
type: string

components:
schemas:
Account:
type: object
description: Account data
parameters:
id:
type: integer
required: true
name:
type: string
required: true
76 changes: 76 additions & 0 deletions spec/fixtures/openapi3/response_as_ref_and_plain.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
[
{
"path": "/accounts/{accountId}",
"method": "GET",
"content-type": "",
"requests": [
],
"responses": [
{
"status": "200",
"body": {
"$ref": "#/components/schemas/Account",
"components": {
"schemas": {
"Account": {
"description": "Account data",
"type": "object",
"parameters": {
"id": {
"type": "integer",
"required": true,
"example": 1
},
"name": {
"type": "string",
"required": true,
"example": "Jane Doe"
}
}
}
}
}
},
"content-type": "application/json"
},
{
"status": "403",
"body": {
"type": "object",
"parameters": {
"error": {
"type": "string",
"required": true,
"example": "Access denied"
}
}
},
"content-type": "application/json"
},
{
"status": "404",
"body": {
"$ref": "#/components/schemas/NotFound",
"components": {
"schemas": {
"NotFound": {
"description": "Not found error",
"type": "object",
"parameters": {
"error": {
"type": "string",
"required": true,
"default": "Account not found",
"example": "Account not found"
}
}
}
}
}
},
"content-type": "application/json"
}
],
"resource": ""
}
]
73 changes: 73 additions & 0 deletions spec/fixtures/openapi3/response_as_ref_and_plain.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
openapi: 3.1.3
servers:
- url: 'http://localhost:3000/api/v2/storefront'
info:
version: 2.0.0
title: Test API
description: API for testing specs. It has two responses with same code but different content-types described in a components section

paths:
/accounts/{accountId}:
get:
summary: Get account data
parameters:
- name: accountId
in: query
schema:
type: integer
required: true
responses:
'200':
$ref: '#/components/responses/200Ok'
'403':
description: Forbidden
content:
application/json:
schema:
type: object
parameters:
error:
type: string
required: true
example: Access denied
'404':
description: Not found
content:
application/json:
schema:
$ref: '#/components/schemas/NotFound'

components:
responses:
200Ok:
description: Account data
content:
application/json:
schema:
$ref: '#/components/schemas/Account'
example:
id: 10
name: John Doe

schemas:
Account:
type: object
description: Account data
parameters:
id:
type: integer
required: true
example: 1
name:
type: string
required: true
example: Jane Doe
NotFound:
type: object
description: Not found error
parameters:
error:
type: string
required: true
default: Account not found
example: Account not found
44 changes: 44 additions & 0 deletions spec/fixtures/openapi3/response_is_a_ref.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
[
{
"path": "/accounts/{accountId}",
"method": "GET",
"content-type": "",
"requests": [
],
"responses": [
{
"status": "200",
"body": {
"$ref": "#/components/schemas/Account",
"components": {
"schemas": {
"Account": {
"description": "Account data",
"type": "object",
"parameters": {
"id": {
"type": "integer",
"required": true
},
"name": {
"type": "string",
"required": true
}
}
}
}
}
},
"content-type": "application/json"
},
{
"status": "200",
"body": {
"type": "string"
},
"content-type": "text/plain"
}
],
"resource": ""
}
]
Loading
Loading