Skip to content

Commit

Permalink
Use utoipa support for serde_json::Value
Browse files Browse the repository at this point in the history
  • Loading branch information
jayvdb authored and idubrov committed Apr 20, 2023
1 parent 1c1158a commit 0e1f7aa
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 4 deletions.
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ serde = { version = "1.0.159", features = ["derive"] }
serde_json = "1.0.95"
thiserror = "1.0.40"
treediff = { version = "4.0.2", features = ["with-serde-json"], optional = true }
utoipa = { version = "3.2.1", optional = true }
utoipa = { version = "3.3.0", optional = true }

[dev-dependencies]
expectorate = "1.0"
rand = "0.8.5"
serde_json = { version = "1.0.95", features = ["preserve_order"] }
serde_yaml = "0.9.19"
utoipa = { version = "3.3.0", features = ["debug"] }
3 changes: 0 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,6 @@ pub struct AddOperation {
/// within the target document where the operation is performed.
pub path: String,
/// Value to add to the target location.
#[cfg_attr(feature = "utoipa", schema(value_type = Object))]
pub value: Value,
}

Expand All @@ -173,7 +172,6 @@ pub struct ReplaceOperation {
/// within the target document where the operation is performed.
pub path: String,
/// Value to replace with.
#[cfg_attr(feature = "utoipa", schema(value_type = Object))]
pub value: Value,
}

Expand Down Expand Up @@ -215,7 +213,6 @@ pub struct TestOperation {
/// within the target document where the operation is performed.
pub path: String,
/// Value to test against.
#[cfg_attr(feature = "utoipa", schema(value_type = Object))]
pub value: Value,
}

Expand Down
1 change: 1 addition & 0 deletions tests/utoipa.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"openapi":"3.0.3","info":{"title":"json-patch","description":"RFC 6902, JavaScript Object Notation (JSON) Patch","contact":{"name":"Ivan Dubrov","email":"dubrov.ivan@gmail.com"},"license":{"name":"MIT/Apache-2.0"},"version":"1.0.0"},"paths":{"foo":{"get":{"tags":["crate"],"operationId":"get_foo","requestBody":{"content":{"application/json":{"schema":{"$ref":"#/components/schemas/Patch"}}},"required":true},"responses":{"200":{"description":"Patch completed"},"406":{"description":"Not accepted"}}}}},"components":{"schemas":{"AddOperation":{"type":"object","description":"JSON Patch 'add' operation representation","required":["path","value"],"properties":{"path":{"type":"string","description":"JSON-Pointer value [RFC6901](https://tools.ietf.org/html/rfc6901) that references a location\nwithin the target document where the operation is performed."},"value":{"description":"Value to add to the target location."}}},"CopyOperation":{"type":"object","description":"JSON Patch 'copy' operation representation","required":["from","path"],"properties":{"from":{"type":"string","description":"JSON-Pointer value [RFC6901](https://tools.ietf.org/html/rfc6901) that references a location\nto copy value from."},"path":{"type":"string","description":"JSON-Pointer value [RFC6901](https://tools.ietf.org/html/rfc6901) that references a location\nwithin the target document where the operation is performed."}}},"MoveOperation":{"type":"object","description":"JSON Patch 'move' operation representation","required":["from","path"],"properties":{"from":{"type":"string","description":"JSON-Pointer value [RFC6901](https://tools.ietf.org/html/rfc6901) that references a location\nto move value from."},"path":{"type":"string","description":"JSON-Pointer value [RFC6901](https://tools.ietf.org/html/rfc6901) that references a location\nwithin the target document where the operation is performed."}}},"Patch":{"type":"array","items":{"$ref":"#/components/schemas/PatchOperation"}},"PatchOperation":{"oneOf":[{"allOf":[{"$ref":"#/components/schemas/AddOperation"},{"type":"object","required":["op"],"properties":{"op":{"type":"string","enum":["add"]}}}]},{"allOf":[{"$ref":"#/components/schemas/RemoveOperation"},{"type":"object","required":["op"],"properties":{"op":{"type":"string","enum":["remove"]}}}]},{"allOf":[{"$ref":"#/components/schemas/ReplaceOperation"},{"type":"object","required":["op"],"properties":{"op":{"type":"string","enum":["replace"]}}}]},{"allOf":[{"$ref":"#/components/schemas/MoveOperation"},{"type":"object","required":["op"],"properties":{"op":{"type":"string","enum":["move"]}}}]},{"allOf":[{"$ref":"#/components/schemas/CopyOperation"},{"type":"object","required":["op"],"properties":{"op":{"type":"string","enum":["copy"]}}}]},{"allOf":[{"$ref":"#/components/schemas/TestOperation"},{"type":"object","required":["op"],"properties":{"op":{"type":"string","enum":["test"]}}}]}],"description":"JSON Patch single patch operation","discriminator":{"propertyName":"op"}},"RemoveOperation":{"type":"object","description":"JSON Patch 'remove' operation representation","required":["path"],"properties":{"path":{"type":"string","description":"JSON-Pointer value [RFC6901](https://tools.ietf.org/html/rfc6901) that references a location\nwithin the target document where the operation is performed."}}},"ReplaceOperation":{"type":"object","description":"JSON Patch 'replace' operation representation","required":["path","value"],"properties":{"path":{"type":"string","description":"JSON-Pointer value [RFC6901](https://tools.ietf.org/html/rfc6901) that references a location\nwithin the target document where the operation is performed."},"value":{"description":"Value to replace with."}}},"TestOperation":{"type":"object","description":"JSON Patch 'test' operation representation","required":["path","value"],"properties":{"path":{"type":"string","description":"JSON-Pointer value [RFC6901](https://tools.ietf.org/html/rfc6901) that references a location\nwithin the target document where the operation is performed."},"value":{"description":"Value to test against."}}}}}}
38 changes: 38 additions & 0 deletions tests/utoipa.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#[cfg(feature = "utoipa")]
#[test]
fn schema() {
use json_patch::*;
use utoipa::OpenApi;

#[utoipa::path(
get,
path = "foo",
request_body = Patch,
responses(
(status = 200, description = "Patch completed"),
(status = 406, description = "Not accepted"),
),
)]
#[allow(unused)]
fn get_foo(body: Patch) {}

#[derive(OpenApi, Default)]
#[openapi(
paths(get_foo),
components(schemas(
AddOperation,
CopyOperation,
MoveOperation,
PatchOperation,
RemoveOperation,
ReplaceOperation,
TestOperation,
Patch,
))
)]
struct ApiDoc;

let doc = ApiDoc::openapi().to_json().unwrap();

expectorate::assert_contents("tests/utoipa.json", &doc);
}

0 comments on commit 0e1f7aa

Please sign in to comment.