jonasbb / serde_with

This crate provides custom de/serialization helpers to use in combination with serde's `with`-annotation and with the improved `serde_as`-annotation.
https://docs.rs/serde_with
Apache License 2.0
663 stars 72 forks source link

Implement `JsonSchemaAs` for `OneOrMany` #719

Closed swlynch99 closed 7 months ago

swlynch99 commented 7 months ago

Here's the next type. This one implements JsonSchemaAs for OneOrMany. It is fortunately much simpler than KeyValueMap.

For OneOrMany the transformation we want to do is basically to take this schema

{ "$ref": "..." }

and transform it into

{
  "anyOf": [
    { "$ref": "..." },
    {
      "type": "array",
      "items": { "$ref": "..." }
    }
  ]
}

that is, we change it so it can take either an array or a single value. The only difference between the PreferOne and the PreferMany variants is that the inner object is marked as writeOnly: true.

I have also included some tests that should cover pretty much everything.

codecov[bot] commented 7 months ago

Codecov Report

Attention: Patch coverage is 83.33333% with 4 lines in your changes are missing coverage. Please review.

Project coverage is 66.21%. Comparing base (bb2ab60) to head (5e36083). Report is 5 commits behind head on master.

Files Patch % Lines
serde_with/src/schemars_0_8.rs 83.33% 4 Missing :warning:
Additional details and impacted files ```diff @@ Coverage Diff @@ ## master #719 +/- ## ========================================== + Coverage 65.64% 66.21% +0.57% ========================================== Files 38 38 Lines 2343 2445 +102 ========================================== + Hits 1538 1619 +81 - Misses 805 826 +21 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

jonasbb commented 7 months ago

That looks great :) Thank you.

jonasbb commented 7 months ago

I published your new JsonSchemaAs implementations and fix for the serde_as macro in v3.7.0