Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serde default with only Deserialize wont compile #115

Closed
ThouCheese opened this issue Oct 13, 2021 · 3 comments
Closed

Serde default with only Deserialize wont compile #115

ThouCheese opened this issue Oct 13, 2021 · 3 comments

Comments

@ThouCheese
Copy link

Hi! I have a bit of a weird issue, when the following two conditions are satisfied:

  1. Some struct Outer has a field annotated with #[serde(default)] or #[serde(default = "some_fn")]
  2. and that field is of a type Inner, which implements serde::Deserialize and schemars::JsonSchema, but not serde::Serialize.

Then the derivation of schemars::JsonSchema on the struct fails with the following message:

error[E0277]: the trait bound `Inner: Serialize` is not satisfied
   --> src/main.rs:3:30
    |
3   | #[derive(serde::Deserialize, schemars::JsonSchema)]
    |                              ^^^^^^^^^^^^^^^^^^^^ the trait `Serialize` is not implemented for `Inner`
    | 
   ::: /home/luuk.local/.cargo/registry/src/github.com-1ecc6299db9ec823/serde_json-1.0.68/src/value/mod.rs:940:8
    |
940 |     T: Serialize,
    |        --------- required by this bound in `to_value`
    |
    = note: required because of the requirements on the impl of `Serialize` for `std::vec::Vec<Inner>`
    = note: this error originates in the derive macro `schemars::JsonSchema` (in Nightly builds, run with -Z macro-backtrace for more info)

Minimal example

#[derive(serde::Deserialize, schemars::JsonSchema)]
struct Outer {
    #[serde(default)]
    field: Vec<Inner>,
}

#[derive(serde::Deserialize, schemars::JsonSchema)]
struct Inner {
    a: String,
}
@ede1998
Copy link

ede1998 commented Nov 11, 2021

I ran into the same issue. You can work around it by adding a skip_serializing annotation:

#[derive(serde::Deserialize, schemars::JsonSchema)]
struct Outer {
    #[serde(default)]
    #[schemars(skip_serializing)]
    field: Vec<Inner>,
}

#[derive(serde::Deserialize, schemars::JsonSchema)]
struct Inner {
    a: String,
}

At least that's what did the trick for me.

@GREsau GREsau closed this as completed in 690fe44 Nov 14, 2021
@GREsau
Copy link
Owner

GREsau commented Nov 14, 2021

Thanks for the report, this should be fixed in 0.8.7
https://crates.io/crates/schemars/0.8.7

sanlee42 added a commit to sanlee42/schemars that referenced this issue Jan 2, 2023
* Read #[validate(...)] attributes

* Handle required flattened Option fields

* Refactor out `add_schema_as_property`

* Process validation attributes in newtype structs

* Process validation attributes in tuple structs

* Refactor out "local_id" for type definitions

* Refactoring

* Support inline regex

* Allow setting validation attributes via #[schemars(...)]

* Add some doc comments

* Fix doc test

* Emit compilation errors for duplicate validation attributes

* Fix indexmap tests for rust 1.37

* upgrade diem dep (#1)

* Update changelog and docs

* Add newline to attributes docs

* v0.8.4

* Allow empty #[validate] attributes.

Fixes GREsau#109

* v0.8.5

* Use oneOf for enums when possible (GREsau#108)

* v0.8.6

* Correct latest changelog entry

* update diem dep

* Implement JsonSchema on EnumSet type

* update diem dep

* Upgrade move deps (#3)

* [deps] Upgrade move types dep.

* Update examples after 0a1200b

* Allow non-Serialize default values.

Default values that don't implement Serialize are now ignored, rather than causing a compile error.
This is done by simulating specialization using a technique copied from Rocket:
https://github.com/SergioBenitez/Rocket/blob/5ebefa97c992c37bdc476299304a339d429a43fc/core/lib/src/sentinel.rs#L391-L445

Fixes GREsau#115

* v0.8.7

* update diem deps

* Add example for optional dependency in readme

Based on https://github.com/GREsau/schemars/pull/118/files

* Add support for rust_decimal and bigdecimal (GREsau#101)

* Document new optional dependencies

* Internally tagged enums don't honor deny_unknown_fields as precisely as
they might.

flatten doesn't act quite as intended with regard to
additional_properties

* v0.8.8

* update diem deps to latest

* update diem deps

* Update dep

* update diem deps

* Update rust toolchain and dep

* update diem dep

* Update diem dep

* [crypto] Update dependency crypto to use starcoinorg/starcoin-crypto repo (#5)

* [crypto] Update dependency crypto to use starcoinorg/starcoin-crypto repo.

* [deps] Update move-core-types to starcoinorg/move

* Remove diem types

Co-authored-by: Graham Esau <gesau@hotmail.co.uk>
Co-authored-by: lerencao <funfriendcjf@gmail.com>
Co-authored-by: Graham Esau <graham.esau@vonage.com>
Co-authored-by: Adam Leventhal <adam.leventhal@gmail.com>
Co-authored-by: Matt Campbell <mattcampbell@pobox.com>
Co-authored-by: jolestar <jolestar@gmail.com>
Co-authored-by: timando <github@timando.net>
Co-authored-by: Adam H. Leventhal <ahl@oxide.computer>
@ModProg
Copy link

ModProg commented Jan 17, 2023

I ran into this issue when using # [schemars(with = "SomeTypeWithOnlyDeserialize")] the issue is, when I add #[schemars(skip_serializing)] it makes the field required even when marked as #[serde(default)]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants