Skip to content

onconova.core.management.commands.export_openapi

__doc__ module-attribute

Command

Bases: BaseCommand

Django management command to export the OpenAPI schema from a NinjaAPI instance.

This command allows you to export the OpenAPI schema for your API, either to stdout or to a specified file. You can specify the API instance to use, customize the JSON output formatting, and control key sorting and ASCII encoding.

Options

--api Specify the import path to the NinjaAPI instance (default: 'onconova.api.api'). --output Output the schema to a file (if omitted, outputs to stdout). --indent Indent level for pretty-printing the JSON output. --sorted Sort the JSON keys alphabetically. --ensure-ascii Ensure ASCII encoding for the JSON output.

Example usage

python manage.py export_openapi_schema --api project.urls.api --output schema.json --indent 2 --sorted

help class-attribute instance-attribute

add_arguments(parser)

Source code in onconova/core/management/commands/export_openapi.py
def add_arguments(self, parser: CommandParser) -> None:
    parser.add_argument(
        "--api",
        dest="api",
        default="onconova.api.api",
        type=str,
        help="Specify api instance module",
    )
    parser.add_argument(
        "--output",
        dest="output",
        default=None,
        type=str,
        help="Output schema to a file (outputs to stdout if omitted).",
    )
    parser.add_argument(
        "--indent", dest="indent", default=None, type=int, help="JSON indent"
    )
    parser.add_argument(
        "--sorted",
        dest="sort_keys",
        default=False,
        action="store_true",
        help="Sort Json keys",
    )
    parser.add_argument(
        "--ensure-ascii",
        dest="ensure_ascii",
        default=False,
        action="store_true",
        help="ensure_ascii for JSON output",
    )

handle(*args, **options)

Source code in onconova/core/management/commands/export_openapi.py
def handle(self, *args: Any, **options: Any) -> None:
    api = self._get_api_instance(options["api"])
    schema = api.get_openapi_schema()
    result = json.dumps(
        schema,
        cls=NinjaJSONEncoder,
        indent=options["indent"],
        sort_keys=options["sort_keys"],
        ensure_ascii=options["ensure_ascii"],
    )

    if options["output"]:
        with Path(options["output"]).open("wb") as f:
            f.write(result.encode())
    else:
        self.stdout.write(result)
runner