Skip to content

Commit

Permalink
poem-openapi: implements Serialize and Deserialize for `poem_open…
Browse files Browse the repository at this point in the history
…api::types::Any<T>`.
  • Loading branch information
sunli829 committed Sep 11, 2024
1 parent 41524bc commit 1bb87fe
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
4 changes: 4 additions & 0 deletions poem-openapi/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

# Unreleased

- implements `Serialize` and `Deserialize` for `poem_openapi::types::Any<T>`.

# [5.1.0] 2024-09-08

- fix read_only_with_default test when only default features are enabled [#854](https://github.com/poem-web/poem/pulls)
Expand Down
22 changes: 21 additions & 1 deletion poem-openapi/src/types/any.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::borrow::Cow;

use serde::{de::DeserializeOwned, Serialize};
use serde::{de::DeserializeOwned, Deserialize, Deserializer, Serialize, Serializer};
use serde_json::Value;

use crate::{
Expand Down Expand Up @@ -66,6 +66,26 @@ impl<T: Serialize + Send + Sync> ToXML for Any<T> {
}
}

impl<T: Serialize> Serialize for Any<T> {
#[inline]
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
self.0.serialize(serializer)
}
}

impl<'de, T: DeserializeOwned> Deserialize<'de> for Any<T> {
#[inline]
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
T::deserialize(deserializer).map(Self)
}
}

impl Type for Value {
const IS_REQUIRED: bool = true;

Expand Down

0 comments on commit 1bb87fe

Please sign in to comment.