View Source ExOpenAI.Codegen.FunctionDocGenerator (ex_openai.ex v2.0.0-beta2)

Generates @doc and @spec attributes for OpenAPI operation functions.

Summary

Functions

Builds parameter type specifications for a function.

Builds the typespec for a single positional parameter.

Builds the return type specification for a function.

Determines the type specification for a parameter.

Generates @doc attribute for an operation.

Generates @spec attribute for an operation function.

Functions

Link to this function

build_param_specs(operation, arg_names, schemas)

View Source
@spec build_param_specs(ExOpenAI.Codegen.DocsParser.Operation.t(), [atom()], %{
  required(String.t()) => ExOpenAI.Codegen.DocsParser.Schema.t()
}) :: [Macro.t()]

Builds parameter type specifications for a function.

Takes an operation and its argument names, and generates proper typespecs for each parameter. This includes:

  • Path parameters (from operation.parameters where in="path")
  • Required body parameters (from request body schema)
  • Optional parameters in the opts keyword list

Parameters

  • operation - The Operation struct containing parameter definitions
  • arg_names - List of argument names (atoms) for the function
  • schemas - Map of component schemas for type resolution

Returns

A list of AST nodes representing the typespec for each parameter.

Example

iex> operation = %Operation{
...>   parameters: [%{name: "id", in: "path", schema: %{"type" => "string"}}],
...>   request_body: %{content: %{"application/json" => %{...}}}
...> }
iex> build_param_specs(operation, [:id, :name, :opts], schemas)
[{:"::", [], [{:id, [], nil}, {:remote_type, [], [...]}]}, ...]
Link to this function

build_positional_param_spec(operation, arg_name, body_schema, schemas)

View Source

Builds the typespec for a single positional parameter.

Determines whether the parameter is a path parameter or body parameter and generates the appropriate typespec.

Parameters

  • operation - The Operation struct
  • arg_name - The parameter name as an atom
  • body_schema - The resolved request body schema (may be nil)

Returns

An AST node representing the parameter typespec.

Link to this function

build_return_spec(operation, function_name, schemas)

View Source
@spec build_return_spec(ExOpenAI.Codegen.DocsParser.Operation.t(), atom(), %{
  required(String.t()) => ExOpenAI.Codegen.DocsParser.Schema.t()
}) :: Macro.t()

Builds the return type specification for a function.

Determines the appropriate return type based on:

  • Response schemas defined in the operation
  • Whether the function is an explicit streaming helper (*_stream)
  • Whether the endpoint supports stream: true on the normal function

Parameters

  • operation - The Operation struct containing response definitions
  • function_name - The function name (used to detect explicit streaming helpers)
  • schemas - Map of component schemas for type resolution

Returns

An AST node representing the return type, typically:

  • {:ok, ComponentType.t()} | {:error, any()} for normal endpoints

  • {:ok, reference()} | {:error, any()} for explicit streaming helpers

  • {:ok, ComponentType.t() | reference()} | {:error, any()} for endpoints that switch behavior with stream: true

Link to this function

determine_param_type(operation, arg_name, body_schema, schemas)

View Source

Determines the type specification for a parameter.

Checks if the parameter is a path parameter first, then checks if it's a body parameter. Returns the appropriate typespec AST.

Parameters

  • operation - The Operation struct containing parameter definitions
  • arg_name - The parameter name as an atom
  • body_schema - The resolved request body schema (may be nil)

Returns

An AST node representing the type (e.g., String.t(), integer(), etc.)

Link to this function

generate_doc(operation, schemas \\ %{})

View Source

Generates @doc attribute for an operation.

Uses the operation's summary and description, plus parameter documentation.

Link to this function

generate_spec(operation, function_name, arg_names, schemas)

View Source
@spec generate_spec(ExOpenAI.Codegen.DocsParser.Operation.t(), atom(), [atom()], %{
  required(String.t()) => ExOpenAI.Codegen.DocsParser.Schema.t()
}) :: Macro.t()

Generates @spec attribute for an operation function.

Builds proper typespecs for all parameters and return types.