[{"data":1,"prerenderedAt":1015},["ShallowReactive",2],{"$fb4evPEo-3_jkAPXgpc0deDYz3TObFZFLuatjY1A2AT8":3},{"slug":4,"title":5,"description":6,"source":7,"document":8,"toc":999,"editUrl":1009,"prev":1010,"next":1013},"fundamentals\u002Fopenapi-swagger","OpenAPI & Swagger","Generate an OpenAPI 3.1 spec from route metadata and browse it in Swagger UI.","---\ntitle: OpenAPI & Swagger\ndescription: Generate an OpenAPI 3.1 spec from route metadata and browse it in Swagger UI.\n---\n\n# OpenAPI & Swagger\n\nFerrite generates an OpenAPI 3.1 specification **from your route metadata** — no annotation sprawl required. Every route, request body, and guard is already recorded by the macros; `ferrite-swagger` turns that into a browsable API.\n\n## Enable it\n\n```rust\nuse ferrite_framework::{module, Ferrite};\n\n#[module(imports = [SwaggerModule])]\npub struct AppModule;\n\n#[bootstrap]\nasync fn main() {\n    let app = Ferrite::create::\u003CAppModule>().await;\n    app.listen(\"0.0.0.0:3000\").await?;\n}\n```\n\n## What you get\n\n| Route | Content |\n| --- | --- |\n| `GET \u002Fopenapi.json` | The full OpenAPI 3.1 document |\n| `GET \u002Fdocs` & `GET \u002Fdocs\u002F` | Swagger UI over the CDN |\n\n## What the generator includes\n\n`build_openapi(&SwaggerConfig)` enumerates the registered `ControllerRoutes` and:\n\n- adds a **bearer HTTP security scheme** (named `bearer`)\n- per-route `200` \u002F `400` \u002F `404` responses\n- a `requestBody` whenever a route recorded body DTO types (`request_body_types`)\n- a `401` response + `security` block whenever a route recorded guards (`guard_types`)\n\nThat last bit is why `#[use_guards(AuthGuard)]` on a route makes the spec grow a security requirement automatically.\n\n## Configuration\n\n```rust\nuse ferrite_swagger::SwaggerConfig;\n\nlet config = SwaggerConfig {\n    title: \"Users API\".into(),\n    version: \"1.2.3\".into(),\n    description: \"Our public API\".into(),\n    server_url: Some(\"https:\u002F\u002Fapi.example.com\".into()),\n};\n```\n\nOr set it from the environment:\n\n| Env | Default |\n| --- | --- |\n| `SWAGGER_TITLE` | `Ferrite API` |\n| `SWAGGER_VERSION` | `0.1.0` |\n| `SWAGGER_DESCRIPTION` | `Generated by Ferrite` |\n| `SWAGGER_SERVER_URL` | unset — `servers` omitted |\n\n`SwaggerService` exposes `config()`, `document()`, and `openapi()` if you want to serve the spec elsewhere.\n\n## Extending with `SwaggerFragment`\n\nRegister DTO schemas, tags, and per-operation metadata at compile time with the fragment API:\n\n```rust\nuse ferrite_swagger::submit_fragment;\n\nsubmit_fragment! { |api: &mut utoipa::openapi::OpenApi| {\n    \u002F\u002F Register DTO schemas from a ToSchema impl:\n    api.components_mut().and_then(|c| c.schemas).map(|schemas| {\n        schemas.insert(\"UserResponse\".into(), ToSchema::schemas()[\"UserResponse\"].clone());\n    });\n    \u002F\u002F tag operations by path prefix:\n    let tag = utoipa::openapi::tag::TagBuilder::new().name(\"users\").build();\n    api.tags.get_or_insert_with(Vec::new).push(tag);\n}}\n```\n\nFragment closures get `&mut utoipa::openapi::OpenApi`, so anything `utoipa` supports is on the table.\n\n## Service access\n\nInject `SwaggerService` and read the document programmatically:\n\n```rust\n#[injectable]\npub struct ApiReporter {\n    swagger: SwaggerService,\n}\n\nimpl ApiReporter {\n    #[inject]\n    pub fn new(swagger: SwaggerService) -> Self {\n        Self { swagger }\n    }\n\n    pub fn summary(&self) -> serde_json::Value {\n        self.swagger.document()\n    }\n}\n```\n\n## CLI note\n\n`fr openapi export` currently writes a static (empty) spec file. For a real spec, hit `GET \u002Fopenapi.json` at runtime, or build it in a test with `build_openapi`.\n\n## Next steps\n\n- [Health checks](\u002Fdocs\u002Ffundamentals\u002Fhealth-checks).\n- [Controllers](\u002Fdocs\u002Foverview\u002Fcontrollers) — route metadata source.",{"frontmatter":9,"meta":10,"nodes":40},{"title":5,"description":6},{"toc":11},{"title":12,"searchDepth":13,"depth":14,"links":15},"On This Page",2,3,[16,19,22,25,28,31,34,37],{"id":17,"depth":13,"text":18},"enable-it","Enable it",{"id":20,"depth":13,"text":21},"what-you-get","What you get",{"id":23,"depth":13,"text":24},"what-the-generator-includes","What the generator includes",{"id":26,"depth":13,"text":27},"configuration","Configuration",{"id":29,"depth":13,"text":30},"extending-with-swaggerfragment","Extending with SwaggerFragment",{"id":32,"depth":13,"text":33},"service-access","Service access",{"id":35,"depth":13,"text":36},"cli-note","CLI note",{"id":38,"depth":13,"text":39},"next-steps","Next steps",[41,45,59,62,216,218,262,264,274,325,332,334,447,450,514,532,538,541,795,806,808,814,967,969,981,983],[42,43,5],"h1",{"id":44},"openapi-swagger",[46,47,48,49,53,54,58],"p",{},"Ferrite generates an OpenAPI 3.1 specification ",[50,51,52],"strong",{},"from your route metadata"," — no annotation sprawl required. Every route, request body, and guard is already recorded by the macros; ",[55,56,57],"code",{},"ferrite-swagger"," turns that into a browsable API.",[60,61,18],"h2",{"id":17},[63,64,67],"pre",{"language":65,"class":66},"rust","shiki shiki-themes github-light github-dark dark:github-dark",[55,68,70,96,97,96,99,96,116,96,130,96,132,96,137,96,151,96,185,96,211],{"class":69},"language-rust",[71,72,75,79,83,86,90,93],"span",{"class":73,"style":74},"line","display: inline",[71,76,78],{"style":77},"color:#D73A49;--shiki-dark:#F97583","use",[71,80,82],{"style":81},"color:#6F42C1;--shiki-dark:#B392F0"," ferrite_framework",[71,84,85],{"style":77},"::",[71,87,89],{"style":88},"color:#24292E;--shiki-dark:#E1E4E8","{module, ",[71,91,92],{"style":81},"Ferrite",[71,94,95],{"style":88},"};","\n",[71,98],{"class":73,"style":74},[71,100,101,104,107,110,113],{"class":73,"style":74},[71,102,103],{"style":88},"#[module(imports ",[71,105,106],{"style":77},"=",[71,108,109],{"style":88}," [",[71,111,112],{"style":81},"SwaggerModule",[71,114,115],{"style":88},"])]",[71,117,118,121,124,127],{"class":73,"style":74},[71,119,120],{"style":77},"pub",[71,122,123],{"style":77}," struct",[71,125,126],{"style":81}," AppModule",[71,128,129],{"style":88},";",[71,131],{"class":73,"style":74},[71,133,134],{"class":73,"style":74},[71,135,136],{"style":88},"#[bootstrap]",[71,138,139,142,145,148],{"class":73,"style":74},[71,140,141],{"style":77},"async",[71,143,144],{"style":77}," fn",[71,146,147],{"style":81}," main",[71,149,150],{"style":88},"() {",[71,152,153,156,159,161,164,166,169,171,174,177,180,183],{"class":73,"style":74},[71,154,155],{"style":77},"    let",[71,157,158],{"style":88}," app ",[71,160,106],{"style":77},[71,162,163],{"style":81}," Ferrite",[71,165,85],{"style":77},[71,167,168],{"style":81},"create",[71,170,85],{"style":77},[71,172,173],{"style":88},"\u003C",[71,175,176],{"style":81},"AppModule",[71,178,179],{"style":88},">()",[71,181,182],{"style":77},".await",[71,184,129],{"style":88},[71,186,187,190,193,196,199,203,206,209],{"class":73,"style":74},[71,188,189],{"style":88},"    app",[71,191,192],{"style":77},".",[71,194,195],{"style":81},"listen",[71,197,198],{"style":88},"(",[71,200,202],{"style":201},"color:#032F62;--shiki-dark:#9ECBFF","\"0.0.0.0:3000\"",[71,204,205],{"style":88},")",[71,207,208],{"style":77},".await?",[71,210,129],{"style":88},[71,212,213],{"class":73,"style":74},[71,214,215],{"style":88},"}",[60,217,21],{"id":20},[219,220,221,234],"table",{},[222,223,224],"thead",{},[225,226,227,231],"tr",{},[228,229,230],"th",{},"Route",[228,232,233],{},"Content",[235,236,237,248],"tbody",{},[225,238,239,245],{},[240,241,242],"td",{},[55,243,244],{},"GET \u002Fopenapi.json",[240,246,247],{},"The full OpenAPI 3.1 document",[225,249,250,259],{},[240,251,252,255,256],{},[55,253,254],{},"GET \u002Fdocs"," & ",[55,257,258],{},"GET \u002Fdocs\u002F",[240,260,261],{},"Swagger UI over the CDN",[60,263,24],{"id":23},[46,265,266,269,270,273],{},[55,267,268],{},"build_openapi(&SwaggerConfig)"," enumerates the registered ",[55,271,272],{},"ControllerRoutes"," and:",[275,276,277,288,302,312],"ul",{},[278,279,280,281,284,285,205],"li",{},"adds a ",[50,282,283],{},"bearer HTTP security scheme"," (named ",[55,286,287],{},"bearer",[278,289,290,291,294,295,294,298,301],{},"per-route ",[55,292,293],{},"200"," \u002F ",[55,296,297],{},"400",[55,299,300],{},"404"," responses",[278,303,304,305,308,309,205],{},"a ",[55,306,307],{},"requestBody"," whenever a route recorded body DTO types (",[55,310,311],{},"request_body_types",[278,313,304,314,317,318,321,322,205],{},[55,315,316],{},"401"," response + ",[55,319,320],{},"security"," block whenever a route recorded guards (",[55,323,324],{},"guard_types",[46,326,327,328,331],{},"That last bit is why ",[55,329,330],{},"#[use_guards(AuthGuard)]"," on a route makes the spec grow a security requirement automatically.",[60,333,27],{"id":26},[63,335,336],{"language":65,"class":66},[55,337,338,96,352,96,354,96,370,96,389,96,405,96,421,96,443],{"class":69},[71,339,340,342,345,347,350],{"class":73,"style":74},[71,341,78],{"style":77},[71,343,344],{"style":81}," ferrite_swagger",[71,346,85],{"style":77},[71,348,349],{"style":81},"SwaggerConfig",[71,351,129],{"style":88},[71,353],{"class":73,"style":74},[71,355,356,359,362,364,367],{"class":73,"style":74},[71,357,358],{"style":77},"let",[71,360,361],{"style":88}," config ",[71,363,106],{"style":77},[71,365,366],{"style":81}," SwaggerConfig",[71,368,369],{"style":88}," {",[71,371,372,375,378,381,383,386],{"class":73,"style":74},[71,373,374],{"style":88},"    title",[71,376,377],{"style":77},":",[71,379,380],{"style":201}," \"Users API\"",[71,382,192],{"style":77},[71,384,385],{"style":81},"into",[71,387,388],{"style":88},"(),",[71,390,391,394,396,399,401,403],{"class":73,"style":74},[71,392,393],{"style":88},"    version",[71,395,377],{"style":77},[71,397,398],{"style":201}," \"1.2.3\"",[71,400,192],{"style":77},[71,402,385],{"style":81},[71,404,388],{"style":88},[71,406,407,410,412,415,417,419],{"class":73,"style":74},[71,408,409],{"style":88},"    description",[71,411,377],{"style":77},[71,413,414],{"style":201}," \"Our public API\"",[71,416,192],{"style":77},[71,418,385],{"style":81},[71,420,388],{"style":88},[71,422,423,426,428,431,433,436,438,440],{"class":73,"style":74},[71,424,425],{"style":88},"    server_url",[71,427,377],{"style":77},[71,429,430],{"style":81}," Some",[71,432,198],{"style":88},[71,434,435],{"style":201},"\"https:\u002F\u002Fapi.example.com\"",[71,437,192],{"style":77},[71,439,385],{"style":81},[71,441,442],{"style":88},"()),",[71,444,445],{"class":73,"style":74},[71,446,95],{"style":88},[46,448,449],{},"Or set it from the environment:",[219,451,452,462],{},[222,453,454],{},[225,455,456,459],{},[228,457,458],{},"Env",[228,460,461],{},"Default",[235,463,464,476,488,500],{},[225,465,466,471],{},[240,467,468],{},[55,469,470],{},"SWAGGER_TITLE",[240,472,473],{},[55,474,475],{},"Ferrite API",[225,477,478,483],{},[240,479,480],{},[55,481,482],{},"SWAGGER_VERSION",[240,484,485],{},[55,486,487],{},"0.1.0",[225,489,490,495],{},[240,491,492],{},[55,493,494],{},"SWAGGER_DESCRIPTION",[240,496,497],{},[55,498,499],{},"Generated by Ferrite",[225,501,502,507],{},[240,503,504],{},[55,505,506],{},"SWAGGER_SERVER_URL",[240,508,509,510,513],{},"unset — ",[55,511,512],{},"servers"," omitted",[46,515,516,519,520,523,524,527,528,531],{},[55,517,518],{},"SwaggerService"," exposes ",[55,521,522],{},"config()",", ",[55,525,526],{},"document()",", and ",[55,529,530],{},"openapi()"," if you want to serve the spec elsewhere.",[60,533,534,535],{"id":29},"Extending with ",[55,536,537],{},"SwaggerFragment",[46,539,540],{},"Register DTO schemas, tags, and per-operation metadata at compile time with the fragment API:",[63,542,543],{"language":65,"class":66},[55,544,545,96,556,96,558,96,594,96,600,96,651,96,696,96,701,96,706,96,758,96,790],{"class":69},[71,546,547,549,551,553],{"class":73,"style":74},[71,548,78],{"style":77},[71,550,344],{"style":81},[71,552,85],{"style":77},[71,554,555],{"style":88},"submit_fragment;",[71,557],{"class":73,"style":74},[71,559,560,563,566,569,572,574,577,580,582,585,587,590,592],{"class":73,"style":74},[71,561,562],{"style":81},"submit_fragment!",[71,564,565],{"style":88}," { ",[71,567,568],{"style":77},"|",[71,570,571],{"style":88},"api",[71,573,377],{"style":77},[71,575,576],{"style":77}," &mut",[71,578,579],{"style":81}," utoipa",[71,581,85],{"style":77},[71,583,584],{"style":81},"openapi",[71,586,85],{"style":77},[71,588,589],{"style":81},"OpenApi",[71,591,568],{"style":77},[71,593,369],{"style":88},[71,595,596],{"class":73,"style":74},[71,597,599],{"style":598},"color:#6A737D;--shiki-dark:#6A737D","    \u002F\u002F Register DTO schemas from a ToSchema impl:",[71,601,602,605,607,610,613,615,618,620,622,625,627,630,632,635,637,640,642,644,647,649],{"class":73,"style":74},[71,603,604],{"style":88},"    api",[71,606,192],{"style":77},[71,608,609],{"style":81},"components_mut",[71,611,612],{"style":88},"()",[71,614,192],{"style":77},[71,616,617],{"style":81},"and_then",[71,619,198],{"style":88},[71,621,568],{"style":77},[71,623,624],{"style":88},"c",[71,626,568],{"style":77},[71,628,629],{"style":88}," c",[71,631,192],{"style":77},[71,633,634],{"style":88},"schemas)",[71,636,192],{"style":77},[71,638,639],{"style":81},"map",[71,641,198],{"style":88},[71,643,568],{"style":77},[71,645,646],{"style":88},"schemas",[71,648,568],{"style":77},[71,650,369],{"style":88},[71,652,653,656,658,661,663,666,668,670,673,676,678,680,683,685,688,690,693],{"class":73,"style":74},[71,654,655],{"style":88},"        schemas",[71,657,192],{"style":77},[71,659,660],{"style":81},"insert",[71,662,198],{"style":88},[71,664,665],{"style":201},"\"UserResponse\"",[71,667,192],{"style":77},[71,669,385],{"style":81},[71,671,672],{"style":88},"(), ",[71,674,675],{"style":81},"ToSchema",[71,677,85],{"style":77},[71,679,646],{"style":81},[71,681,682],{"style":88},"()[",[71,684,665],{"style":201},[71,686,687],{"style":88},"]",[71,689,192],{"style":77},[71,691,692],{"style":81},"clone",[71,694,695],{"style":88},"());",[71,697,698],{"class":73,"style":74},[71,699,700],{"style":88},"    });",[71,702,703],{"class":73,"style":74},[71,704,705],{"style":598},"    \u002F\u002F tag operations by path prefix:",[71,707,708,710,713,715,717,719,721,723,726,728,731,733,736,738,740,743,745,748,750,752,755],{"class":73,"style":74},[71,709,155],{"style":77},[71,711,712],{"style":88}," tag ",[71,714,106],{"style":77},[71,716,579],{"style":81},[71,718,85],{"style":77},[71,720,584],{"style":81},[71,722,85],{"style":77},[71,724,725],{"style":81},"tag",[71,727,85],{"style":77},[71,729,730],{"style":81},"TagBuilder",[71,732,85],{"style":77},[71,734,735],{"style":81},"new",[71,737,612],{"style":88},[71,739,192],{"style":77},[71,741,742],{"style":81},"name",[71,744,198],{"style":88},[71,746,747],{"style":201},"\"users\"",[71,749,205],{"style":88},[71,751,192],{"style":77},[71,753,754],{"style":81},"build",[71,756,757],{"style":88},"();",[71,759,760,762,764,767,769,772,774,777,779,782,784,787],{"class":73,"style":74},[71,761,604],{"style":88},[71,763,192],{"style":77},[71,765,766],{"style":88},"tags",[71,768,192],{"style":77},[71,770,771],{"style":81},"get_or_insert_with",[71,773,198],{"style":88},[71,775,776],{"style":81},"Vec",[71,778,85],{"style":77},[71,780,781],{"style":88},"new)",[71,783,192],{"style":77},[71,785,786],{"style":81},"push",[71,788,789],{"style":88},"(tag);",[71,791,792],{"class":73,"style":74},[71,793,794],{"style":88},"}}",[46,796,797,798,801,802,805],{},"Fragment closures get ",[55,799,800],{},"&mut utoipa::openapi::OpenApi",", so anything ",[55,803,804],{},"utoipa"," supports is on the table.",[60,807,33],{"id":32},[46,809,810,811,813],{},"Inject ",[55,812,518],{}," and read the document programmatically:",[63,815,816],{"language":65,"class":66},[55,817,818,96,823,96,834,96,847,96,851,96,853,96,862,96,867,96,896,96,904,96,909,96,911,96,942,96,959,96,963],{"class":69},[71,819,820],{"class":73,"style":74},[71,821,822],{"style":88},"#[injectable]",[71,824,825,827,829,832],{"class":73,"style":74},[71,826,120],{"style":77},[71,828,123],{"style":77},[71,830,831],{"style":81}," ApiReporter",[71,833,369],{"style":88},[71,835,836,839,841,844],{"class":73,"style":74},[71,837,838],{"style":88},"    swagger",[71,840,377],{"style":77},[71,842,843],{"style":81}," SwaggerService",[71,845,846],{"style":88},",",[71,848,849],{"class":73,"style":74},[71,850,215],{"style":88},[71,852],{"class":73,"style":74},[71,854,855,858,860],{"class":73,"style":74},[71,856,857],{"style":77},"impl",[71,859,831],{"style":81},[71,861,369],{"style":88},[71,863,864],{"class":73,"style":74},[71,865,866],{"style":88},"    #[inject]",[71,868,869,872,874,877,880,882,884,887,890,894],{"class":73,"style":74},[71,870,871],{"style":77},"    pub",[71,873,144],{"style":77},[71,875,876],{"style":81}," new",[71,878,879],{"style":88},"(swagger",[71,881,377],{"style":77},[71,883,843],{"style":81},[71,885,886],{"style":88},") ",[71,888,889],{"style":77},"->",[71,891,893],{"style":892},"color:#005CC5;--shiki-dark:#79B8FF"," Self",[71,895,369],{"style":88},[71,897,898,901],{"class":73,"style":74},[71,899,900],{"style":892},"        Self",[71,902,903],{"style":88}," { swagger }",[71,905,906],{"class":73,"style":74},[71,907,908],{"style":88},"    }",[71,910],{"class":73,"style":74},[71,912,913,915,917,920,922,925,928,930,932,935,937,940],{"class":73,"style":74},[71,914,871],{"style":77},[71,916,144],{"style":77},[71,918,919],{"style":81}," summary",[71,921,198],{"style":88},[71,923,924],{"style":77},"&",[71,926,927],{"style":892},"self",[71,929,886],{"style":88},[71,931,889],{"style":77},[71,933,934],{"style":81}," serde_json",[71,936,85],{"style":77},[71,938,939],{"style":81},"Value",[71,941,369],{"style":88},[71,943,944,947,949,952,954,957],{"class":73,"style":74},[71,945,946],{"style":892},"        self",[71,948,192],{"style":77},[71,950,951],{"style":88},"swagger",[71,953,192],{"style":77},[71,955,956],{"style":81},"document",[71,958,612],{"style":88},[71,960,961],{"class":73,"style":74},[71,962,908],{"style":88},[71,964,965],{"class":73,"style":74},[71,966,215],{"style":88},[60,968,36],{"id":35},[46,970,971,974,975,977,978,192],{},[55,972,973],{},"fr openapi export"," currently writes a static (empty) spec file. For a real spec, hit ",[55,976,244],{}," at runtime, or build it in a test with ",[55,979,980],{},"build_openapi",[60,982,39],{"id":38},[275,984,985,992],{},[278,986,987,192],{},[988,989,991],"a",{"href":990},"\u002Fdocs\u002Ffundamentals\u002Fhealth-checks","Health checks",[278,993,994,998],{},[988,995,997],{"href":996},"\u002Fdocs\u002Foverview\u002Fcontrollers","Controllers"," — route metadata source.",{"title":12,"searchDepth":13,"depth":14,"links":1000},[1001,1002,1003,1004,1005,1006,1007,1008],{"id":17,"depth":13,"text":18},{"id":20,"depth":13,"text":21},{"id":23,"depth":13,"text":24},{"id":26,"depth":13,"text":27},{"id":29,"depth":13,"text":30},{"id":32,"depth":13,"text":33},{"id":35,"depth":13,"text":36},{"id":38,"depth":13,"text":39},"https:\u002F\u002Fgithub.com\u002Fj4flmao\u002Fferrite_rs\u002Fedit\u002Fmain\u002Fdocs\u002Fcontent\u002Ffundamentals\u002Fopenapi-swagger.md",{"title":1011,"slug":1012},"Testing","fundamentals\u002Ftesting",{"title":991,"slug":1014},"fundamentals\u002Fhealth-checks",1789565439199]