Skip to content

Commit 0e1f7aa

Browse files
jayvdbidubrov
authored andcommitted
Use utoipa support for serde_json::Value
1 parent 1c1158a commit 0e1f7aa

File tree

4 files changed

+42
-4
lines changed

4 files changed

+42
-4
lines changed

Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@ serde = { version = "1.0.159", features = ["derive"] }
1919
serde_json = "1.0.95"
2020
thiserror = "1.0.40"
2121
treediff = { version = "4.0.2", features = ["with-serde-json"], optional = true }
22-
utoipa = { version = "3.2.1", optional = true }
22+
utoipa = { version = "3.3.0", optional = true }
2323

2424
[dev-dependencies]
25+
expectorate = "1.0"
2526
rand = "0.8.5"
2627
serde_json = { version = "1.0.95", features = ["preserve_order"] }
2728
serde_yaml = "0.9.19"
29+
utoipa = { version = "3.3.0", features = ["debug"] }

src/lib.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,6 @@ pub struct AddOperation {
148148
/// within the target document where the operation is performed.
149149
pub path: String,
150150
/// Value to add to the target location.
151-
#[cfg_attr(feature = "utoipa", schema(value_type = Object))]
152151
pub value: Value,
153152
}
154153

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

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

tests/utoipa.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"openapi":"3.0.3","info":{"title":"json-patch","description":"RFC 6902, JavaScript Object Notation (JSON) Patch","contact":{"name":"Ivan Dubrov","email":"[email protected]"},"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."}}}}}}

tests/utoipa.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#[cfg(feature = "utoipa")]
2+
#[test]
3+
fn schema() {
4+
use json_patch::*;
5+
use utoipa::OpenApi;
6+
7+
#[utoipa::path(
8+
get,
9+
path = "foo",
10+
request_body = Patch,
11+
responses(
12+
(status = 200, description = "Patch completed"),
13+
(status = 406, description = "Not accepted"),
14+
),
15+
)]
16+
#[allow(unused)]
17+
fn get_foo(body: Patch) {}
18+
19+
#[derive(OpenApi, Default)]
20+
#[openapi(
21+
paths(get_foo),
22+
components(schemas(
23+
AddOperation,
24+
CopyOperation,
25+
MoveOperation,
26+
PatchOperation,
27+
RemoveOperation,
28+
ReplaceOperation,
29+
TestOperation,
30+
Patch,
31+
))
32+
)]
33+
struct ApiDoc;
34+
35+
let doc = ApiDoc::openapi().to_json().unwrap();
36+
37+
expectorate::assert_contents("tests/utoipa.json", &doc);
38+
}

0 commit comments

Comments
 (0)