cwist
Modern C web framework with libttak runtime
Loading...
Searching...
No Matches
openapi.c File Reference

Runtime OpenAPI 3.1 endpoint (#408). More...

#include <cwist/openapi.h>
#include <cwist/core/mem/alloc.h>
#include <cjson/cJSON.h>
#include <pthread.h>
#include <string.h>
#include <strings.h>
#include "routes_internal.h"
Include dependency graph for openapi.c:

Go to the source code of this file.

Macros

#define _POSIX_C_SOURCE   200809L
 

Typedefs

typedef struct openapi_note openapi_note
 
typedef struct openapi_doc openapi_doc
 

Functions

void cwist_app_enable_openapi (cwist_app *app, const char *json_path, const cwist_openapi_meta_t *meta)
 Serve the app's OpenAPI 3.1 document.
 
void cwist_openapi_annotate (cwist_app *app, const char *method, const char *path, const char *summary, const char *tags)
 Attach a summary and tags to one operation of the document.
 

Detailed Description

Runtime OpenAPI 3.1 endpoint (#408).

cwist_app_enable_openapi() mounts a GET route whose context is the app's document state: metadata plus cwist_openapi_annotate() entries. Every request walks the live route table, so a route added anywhere shows up with its path and method; annotations only add a summary and tags.

The route owns the state and frees it with the app. A small registry maps the app back to its state so cwist_openapi_annotate() can find it.

Definition in file openapi.c.

Macro Definition Documentation

◆ _POSIX_C_SOURCE

#define _POSIX_C_SOURCE   200809L

Definition at line 13 of file openapi.c.

Typedef Documentation

◆ openapi_doc

typedef struct openapi_doc openapi_doc

◆ openapi_note

typedef struct openapi_note openapi_note

Function Documentation

◆ cwist_app_enable_openapi()

void cwist_app_enable_openapi ( cwist_app *  app,
const char *  json_path,
const cwist_openapi_meta_t *  meta 
)

Serve the app's OpenAPI 3.1 document.

Registers GET json_path. The document lists every HTTP route of app at request time (the spec route itself and WebSocket routes excepted), so routes added before or after this call are all included. A second call on the same app is ignored.

Parameters
appApplication to serve the document from.
json_pathURL path of the document; NULL means "/openapi.json".
metaDocument metadata; NULL uses the defaults.

Definition at line 210 of file openapi.c.

References cap, cwist_alloc(), cwist_app_get_ex(), cwist_free(), and cwist_strdup().

◆ cwist_openapi_annotate()

void cwist_openapi_annotate ( cwist_app *  app,
const char *  method,
const char *  path,
const char *  summary,
const char *  tags 
)

Attach a summary and tags to one operation of the document.

Takes effect only after cwist_app_enable_openapi() on the same app. Annotating the same method and path again replaces the earlier values.

Parameters
appApplication owning the route.
methodHTTP method name, case-insensitive ("GET", "post", ...).
pathRoute path exactly as registered (e.g. "/users/:id").
summaryOperation summary, or NULL.
tagsComma-separated tag list, or NULL.

Definition at line 242 of file openapi.c.

References cap, cwist_alloc(), cwist_free(), CWIST_HTTP_UNKNOWN, and cwist_strdup().