forked from juhaku/utoipa
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow value_type serde_json::Value (juhaku#568)
Add support for `"AnyType"` according to https://swagger.io/docs/specification/data-models/data-types/. This is achieved by adding a new virtual type called `Value`. When `Value` is used as type, does not have any type restrictions in OpenAPI. The `Value` type can be used along with `serde_json::Value` to allow usage of dynamic content. Also add a new example `raw-json-actix` to demonstrate the newly added `Value` virtual type and update docs.
- Loading branch information
Showing
11 changed files
with
184 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
[package] | ||
name = "raw-json-actix" | ||
description = "Simple actix-web using raw JSON with utoipa and Swagger" | ||
version = "0.1.0" | ||
edition = "2021" | ||
license = "MIT" | ||
authors = [ | ||
"Example <[email protected]>" | ||
] | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
actix-web = "4" | ||
env_logger = "0.10.0" | ||
serde_json = "1.0" | ||
utoipa = { path = "../../utoipa", features = ["actix_extras"] } | ||
utoipa-swagger-ui = { path = "../../utoipa-swagger-ui", features = ["actix-web"] } | ||
|
||
[workspace] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# raw-json-actix | ||
|
||
This is demo `actix-web` application showing using raw JSON in endpoints. | ||
The API demonstrates `utoipa` with `utoipa-swagger-ui` functionalities. | ||
|
||
Just run command below to run the demo application and browse to `http://localhost:8080/swagger-ui/`. | ||
```bash | ||
cargo run | ||
``` | ||
|
||
In the swagger UI: | ||
|
||
1. Send body `"string"` and the console will show the body was a `serde_json::String`. | ||
2. Send body `1` and the console will show the body was a `serde_json::Number`. | ||
3. Send body `[1, 2]` and the console will show the body was a `serde_json::Array`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
use std::{error::Error, net::Ipv4Addr}; | ||
|
||
use actix_web::{ | ||
middleware::Logger, patch, App, HttpResponse, HttpServer, Responder, Result, web::Json, | ||
}; | ||
use serde_json::Value; | ||
use utoipa::OpenApi; | ||
use utoipa_swagger_ui::SwaggerUi; | ||
|
||
#[utoipa::path( | ||
request_body = Value, | ||
responses( | ||
(status = 200, description = "Patch completed"), | ||
(status = 406, description = "Not accepted"), | ||
), | ||
security( | ||
("api_key" = []) | ||
), | ||
)] | ||
#[patch("/patch_raw")] | ||
pub async fn patch_raw(body: Json<Value>) -> Result<impl Responder> { | ||
let value: Value = body.into_inner(); | ||
eprintln!("body = {:?}", value); | ||
Ok(HttpResponse::Ok()) | ||
} | ||
|
||
#[actix_web::main] | ||
async fn main() -> Result<(), impl Error> { | ||
env_logger::init(); | ||
|
||
#[derive(OpenApi)] | ||
#[openapi(paths(patch_raw))] | ||
struct ApiDoc; | ||
|
||
let openapi = ApiDoc::openapi(); | ||
|
||
HttpServer::new(move || { | ||
App::new() | ||
.wrap(Logger::default()) | ||
.service(patch_raw) | ||
.service( | ||
SwaggerUi::new("/swagger-ui/{_:.*}").url("/api-docs/openapi.json", openapi.clone()), | ||
) | ||
}) | ||
.bind((Ipv4Addr::UNSPECIFIED, 8080))? | ||
.run() | ||
.await | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters