openapi

OpenAPI schema customization and code sample generation.

Module Contents

Functions

_build_query_params(parameters)

Build query string and dict from parameters.

_get_param_placeholder(param_type)

Get placeholder value for parameter type.

create_custom_openapi_schema(app, logger)

Create custom OpenAPI schema with code samples and customizations.

generate_code_sample(language, method, path, parameters)

Generate code sample for a given language, method, and path.

openapi._build_query_params(parameters)

Build query string and dict from parameters.

openapi._get_param_placeholder(param_type)

Get placeholder value for parameter type.

openapi.create_custom_openapi_schema(app, logger)

Create custom OpenAPI schema with code samples and customizations.

This function is compatible with FastAPI 0.124+ which aggressively caches OpenAPI schemas. Instead of mutating a cached schema, we generate a fresh schema using get_openapi() and customize it before caching.

Parameters:
  • app – FastAPI application instance

  • logger – Logger instance for debugging

Returns:

Customized OpenAPI schema dictionary

openapi.generate_code_sample(language, method, path, parameters, base_url='')

Generate code sample for a given language, method, and path.

Uses Jinja2 templates to generate code samples dynamically at runtime. The generated code includes a placeholder __API_SERVER_URL__ that will be replaced with the actual URL during documentation rendering.

Parameters:
  • language – The programming language (shell, python, go, js)

  • method – HTTP method (GET, POST, etc.)

  • path – API endpoint path

  • parameters – List of OpenAPI parameter definitions

  • base_url – Base URL for the API (optional, defaults to empty string)

Returns:

Generated code sample as a string with __API_SERVER_URL__ placeholder