8000 Generate `SchemaReferences` for types in parameters by paolobarbolini · Pull Request #1382 · juhaku/utoipa · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Generate SchemaReferences for types in parameters #1382

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions utoipa-gen/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,16 @@ impl<'p> ToTokensDiagnostics for Path<'p> {
schemas
}

let param_schemas = self
.path_attr
.params
.iter()
.map(|param| param.get_component_schemas())
.collect::<Result<Vec<_>, Diagnostics>>()?
.into_iter()
.flatten()
.fold(TokenStream2::new(), to_schema_references);

let response_schemas = self
.path_attr
.responses
Expand Down Expand Up @@ -604,6 +614,7 @@ impl<'p> ToTokensDiagnostics for Path<'p> {

impl utoipa::__dev::SchemaReferences for #impl_for {
fn schemas(schemas: &mut Vec<(String, utoipa::openapi::RefOr<utoipa::openapi::schema::Schema>)>) {
#param_schemas
#schemas
#response_schemas
}
Expand Down
66 changes: 64 additions & 2 deletions utoipa-gen/src/path/parameter.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{borrow::Cow, fmt::Display};
use std::{borrow::Cow, fmt::Display, iter};

use proc_macro2::{Ident, TokenStream};
use quote::{quote, quote_spanned, ToTokens};
Expand Down Expand Up @@ -29,7 +29,7 @@ use crate::{
parse_utils, Diagnostics, Required, ToTokensDiagnostics,
};

use super::media_type::ParsedType;
use super::{media_type::ParsedType, response::ResponseComponentSchemaIter};

/// Parameter of request such as in path, header, query or cookie
///
Expand All @@ -50,6 +50,68 @@ pub enum Parameter<'a> {
IntoParamsIdent(IntoParamsIdentParameter<'a>),
}

impl<'p> Parameter<'p> {
pub fn get_component_schemas(
&self,
) -> Result<impl Iterator<Item = (bool, ComponentSchema)>, Diagnostics> {
match self {
Self::Value(value) => {
if let Some(parameter_schema) = &value.parameter_schema {
match &parameter_schema.parameter_type {
#[cfg(any(
feature = "actix_extras",
feature = "rocket_extras",
feature = "axum_extras"
))]
ParameterType::External(type_tree) => {
let required = !type_tree.is_option();
let schema = ComponentSchema::for_params(
component::ComponentSchemaProps {
type_tree,
features: parameter_schema.features.clone(),
description: None,
container: &Container {
generics: &Generics::default(),
},
},
parameter_schema.option_is_nullable,
)?;
Ok(ResponseComponentSchemaIter::Iter(Box::new(iter::once((
required, schema,
)))))
}
ParameterType::Parsed(inline_type) => {
let type_tree = TypeTree::from_type(inline_type.ty.as_ref())?;
let required = !type_tree.is_option();
let mut schema_features = Vec::<Feature>::new();
schema_features.clone_from(&parameter_schema.features);
schema_features.push(Feature::Inline(inline_type.is_inline.into()));

let schema = ComponentSchema::for_params(
component::ComponentSchemaProps {
type_tree: &type_tree,
features: schema_features,
description: None,
container: &Container {
generics: &Generics::default(),
},
},
parameter_schema.option_is_nullable,
)?;
Ok(ResponseComponentSchemaIter::Iter(Box::new(iter::once((
required, schema,
)))))
}
}
} else {
Ok(ResponseComponentSchemaIter::Empty)
}
}
Self::IntoParamsIdent(_into_params) => Ok(ResponseComponentSchemaIter::Empty),
}
}
}

#[cfg(any(
feature = "actix_extras",
feature = "rocket_extras",
Expand Down
0