-
Notifications
You must be signed in to change notification settings - Fork 26
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
Proof of concept Json-API with OpenAPI support (take 2) #59
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
defmodule AshHq.Docs.OpenApi do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is indicative of the kind of module Ash users would need to write for the outline of their OpenAPI document. I like that this gives the users a chance to add extra schemas, tags, etc. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed. Its a bit boilerplate-y, but its also very likely that people will eventually want to add their own custom endpoints and things like that. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we'll want these functions to take lists of APIs if they don't already. Then they can set %OpenApi{
info: %Info{
title: "AshHQ Docs API",
version: "1.1"
},
servers: [Server.from_endpoint(AshHqWeb.Endpoint]
}
|> AshJsonApi.OpenApi.extend_open_api_spex(@apis) Either way, this is likely nitpicking as I think you've found a good balance between "boilerplate" and extensibility. |
||
alias OpenApiSpex.{Info, OpenApi, Server, SecurityScheme} | ||
|
||
def spec do | ||
%OpenApi{ | ||
info: %Info{ | ||
title: "AshHQ Docs API", | ||
version: "1.1" | ||
}, | ||
servers: [ | ||
Server.from_endpoint(AshHqWeb.Endpoint) | ||
], | ||
paths: AshJsonApi.OpenApi.paths(AshHq.Docs), | ||
tags: AshJsonApi.OpenApi.tags(AshHq.Docs), | ||
components: %{ | ||
responses: AshJsonApi.OpenApi.responses(), | ||
schemas: AshJsonApi.OpenApi.schemas(AshHq.Docs), | ||
securitySchemes: %{ | ||
"api_key" => %SecurityScheme{ | ||
type: "apiKey", | ||
description: "API Key provided in the Authorization header", | ||
name: "api_key", | ||
in: "header" | ||
} | ||
} | ||
}, | ||
security: [ | ||
%{ | ||
# API Key security applies to all operations | ||
"api_key" => [] | ||
} | ||
] | ||
} | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
defmodule AshHqWeb.DocsJsonApiRouter do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've made some modifications to the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. smart 👍🏻 |
||
use AshJsonApi.Api.Router, api: AshHq.Docs, registry: AshHq.Docs.Registry | ||
|
||
get "/swaggerui", | ||
to: OpenApiSpex.Plug.SwaggerUI, | ||
init_opts: [ | ||
path: "/json_api/openapi", | ||
title: "AshHQ JSON-API - Swagger UI", | ||
default_model_expand_depth: 4 | ||
] | ||
|
||
get "/redoc", | ||
to: Redoc.Plug.RedocUI, | ||
init_opts: [spec_url: "/json_api/openapi"] | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I used an MFA here as the most flexible option, but I'm not sure if it feels like idiomatic Ash?
Just the module name would work, as long as it exposes a
spec/0
callback.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The "ash way" is typically
{:spark_behaviour, BehaviourName}
, which will allow them to passModule
or{Module, opts}
, and you'll always get{Module, opts}
for the value, at which point you can sayModule.spec(opts)
.