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

Expose error responses in the spec output #25

Merged
merged 1 commit into from
Nov 21, 2023
Merged

Conversation

paulsturgess
Copy link
Contributor

@paulsturgess paulsturgess commented Nov 21, 2023

Apia defines errors responses like this:

potential_error 'DiskNotFound' do
  code :disk_not_found
  description 'No disk was found matching any of the criteria provided in the arguments'
  http_status 404
end

So now we declare these in the responses for each endpoint.

https://swagger.io/docs/specification/describing-responses/

closes: #2


In swagger:

Screenshot 2023-11-21 at 10 59 33

@paulsturgess paulsturgess self-assigned this Nov 21, 2023

module CoreAPI
module Authenticators
class MainAuthenticator < Apia::Authenticator
Copy link
Contributor Author

@paulsturgess paulsturgess Nov 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this file is the same as before, i just decided to create an /authenticators directory and move it there, seeing as i wanted to add some more.

Comment on lines +22 to +26

return true if component_schema.present?

@spec[:components][:schemas].delete(id)
false
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the schema is blank, then we don't need a $ref for it. This can happen with potential errors when they have no additional fields and thus the detail property is actually just an empty object.

Comment on lines -44 to +54
definition.id.gsub(/\//, "_")
definition.id.gsub(/\//, "")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I decided to get rid of the underscores from our ids that we use for component schemas. I thought it looked odd to have camelcase and underscores together. There is no strict format required for these in the spec, but this follows the naming pattern used in the examples.

Dir.glob(File.join(File.dirname(__FILE__), "objects", "*.rb")).each do |file|
Dir.glob(File.join(File.dirname(__FILE__), "objects", "*.rb")).sort.each do |file|
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make rubocop happy

].flatten.join("_").camelize
end

def add_error_response_schemas
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did think about putting this error response stuff into a separate class. But I thought we might as well keep all the response related logic together in one file.

"$ref": "#/components/schemas/CoreAPI_Objects_TimeZone"
"$ref": "#/components/schemas/CoreAPIObjectsTimeZone"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a good example of how the ref naming has changed to remove the underscores

Comment on lines 215 to 223
[
@route_spec[:operationId].sub(":", "_").gsub(":", "").split("/"),
@http_status,
"Response"
].flatten.join("_").camelize
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If an endpoint defines many potential errors under the same response code, then we name the $ref id after the route itself, with the status code. This makes the ref unique and ensures it won't accidentally be re-used by other endpoints when the client generator runs.

if @definition.type.polymorph?
if @definition.try(:type)&.polymorph?
Copy link
Contributor Author

@paulsturgess paulsturgess Nov 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I needed to add try(:type) in a few places because this schema class can now be called with a Apia::Definitions::Error and that doesn't respond to type.

@paulsturgess paulsturgess marked this pull request as ready for review November 21, 2023 11:33
@paulsturgess paulsturgess merged commit f8e1266 into main Nov 21, 2023
@paulsturgess paulsturgess deleted the error-responses branch November 21, 2023 12:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add error responses to the spec generator
1 participant