View Source OpenAPI.Renderer.Operation (OpenAPI Generator v0.2.0)
Default implementation for callbacks related to rendering operations
This module contains the default implementations for:
OpenAPI.Renderer.render_operations/2
OpenAPI.Renderer.render_operation/2
OpenAPI.Renderer.render_operation_doc/2
OpenAPI.Renderer.render_operation_function/2
OpenAPI.Renderer.render_operation_spec/2
These focus on the operation functions and surrounding code.
Configuration
All configuration offered by the functions in this module lives under the output
key of the
active configuration profile. For example (default values shown):
# config/config.exs
config :oapi_generator, default: [
output: [
base_module: nil,
operation_call: [
request: :list
],
types: [
error: nil
]
]
]
Summary
Functions
Render a single operation
Render all of the operations contained in a single module
Render a call to client.request/1
in the body of an operation function
Renders a keyword list element containing information about the request body
Render the docstring for an operation function
Render the function definition for an operation function
Render code to handle query params in the body of an operation function
Render the spec of an operation function
Functions
@spec render(OpenAPI.Renderer.State.t(), OpenAPI.Processor.Operation.t()) :: Macro.t()
Render a single operation
Default implementation of OpenAPI.Renderer.render_operation/2
.
This implementation calls the following callbacks and concatenates their results:
@spec render_all(OpenAPI.Renderer.State.t(), OpenAPI.Renderer.File.t()) :: Macro.t()
Render all of the operations contained in a single module
Default implementation of OpenAPI.Renderer.render_operations/2
.
This implementation iterates through the operations contained in a file, sorted by their
function name, and calls both OpenAPI.Renderer.render_operation/2
and
OpenAPI.Renderer.render_schema_types/2
callbacks for each. The latter is only given schemas
that have an output format of :typed_map
and relate exclusively to the operation. Afterwards,
it calls OpenAPI.Renderer.render_schema_field_function/2
for all schemas that had types
output earlier. It returns a list of AST nodes.
@spec render_call(OpenAPI.Renderer.State.t(), OpenAPI.Processor.Operation.t()) :: Macro.t()
Render a call to client.request/1
in the body of an operation function
This function is called by the default implementation of
OpenAPI.Renderer.render_operation_function/2
(see render_function/2
). It returns code
similar to this:
client.request(%{...})
Warning: This function is public for the benefit of plugin implementers who wish to replicate portions of the default implementation. It is subject to change.
@spec render_call_request_info( OpenAPI.Renderer.State.t(), OpenAPI.Processor.Operation.request_body(), atom() ) :: Macro.t()
Renders a keyword list element containing information about the request body
The second argument accepts a format for the output code, which can be :map
or the default of
:list
.
This function is called by the default implementation of
OpenAPI.Renderer.render_operation_function/2
(see render_function/2
). It returns code
similar to this:
# Default format:
request: [{"application/json", {MySchema, :t}}]
# With format `:map`:
request: %{"application/json" => {MySchema, :t}}
Warning: This function is public for the benefit of plugin implementers who wish to replicate portions of the default implementation. It is subject to change.
@spec render_doc(OpenAPI.Renderer.State.t(), OpenAPI.Processor.Operation.t()) :: Macro.t()
Render the docstring for an operation function
Default implementation of OpenAPI.Renderer.render_operation_doc/2
.
This implementation uses the docstring created by the processor without modification.
@spec render_function(OpenAPI.Renderer.State.t(), OpenAPI.Processor.Operation.t()) :: Macro.t()
Render the function definition for an operation function
Default implementation of OpenAPI.Renderer.render_operation_function/2
.
This implementation constructs a function that calls a dynamically chosen client module's
request
function with details about the operation.
Configuration
Use output.operation_call
to modify the format of output code within the function call. For
example, the following will output a map for the request body information:
config :oapi_generator, default: [
output: [
operation_call: [
request: :map
]
]
]
Example
def my_operation(path_param, body, opts \ []) do
client = opts[:client] || @default_client
query = Keyword.take(opts, [:query_param])
client.request(%{
args: [path_param: path_param, body: body],
call: {Example.Operations, :my_operation},
url: "/path/to/#{path_param}",
body: body,
method: :post,
query: query,
request: [{"application/json", :map}],
response: [{200, :map}, {404, {Example.NotFoundError, :t}}],
opts: opts
})
end
@spec render_query(OpenAPI.Processor.Operation.t()) :: Macro.t() | nil
Render code to handle query params in the body of an operation function
This function is called by the default implementation of
OpenAPI.Renderer.render_operation_function/2
(see render_function/2
). It returns code
similar to this:
query = Keyword.take(opts, [:param1, :param2])
Warning: This function is public for the benefit of plugin implementers who wish to replicate portions of the default implementation. It is subject to change.
@spec render_spec(OpenAPI.Renderer.State.t(), OpenAPI.Processor.Operation.t()) :: Macro.t()
Render the spec of an operation function
Default implementation of OpenAPI.Renderer.render_operation_spec/2
.