Skip to content

Commit

Permalink
Chore fix ToSchema refactoring aftermath
Browse files Browse the repository at this point in the history
Fix some clippy lints. Move back to component super package so we will
not have package structure `schema/schema`.

Fix examples to use new structre in OpenApi derive
`#[components(schemas(..))]`.
  • Loading branch information
juhaku committed Aug 8, 2022
1 parent 4c6b1e5 commit 2237cc3
Show file tree
Hide file tree
Showing 19 changed files with 35 additions and 23 deletions.
6 changes: 4 additions & 2 deletions examples/rocket-todo/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ fn rocket() -> Rocket<Build> {
todo::delete_todo,
todo::search_todos
),
components(todo::Todo, todo::TodoError),
components(
schemas(todo::Todo, todo::TodoError)
),
tags(
(name = "todo", description = "Todo management endpoints.")
),
Expand Down Expand Up @@ -82,7 +84,7 @@ mod todo {
FromForm, Request, State,
};
use serde::{Deserialize, Serialize};
use utoipa::{ToSchema, IntoParams};
use utoipa::{IntoParams, ToSchema};

pub(super) type TodoStore = Arc<Mutex<Vec<Todo>>>;

Expand Down
4 changes: 3 additions & 1 deletion examples/todo-actix/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ async fn main() -> Result<(), impl Error> {
todo::update_todo,
todo::search_todos
),
components(todo::Todo, todo::TodoUpdateRequest, todo::ErrorResponse),
components(
schemas(todo::Todo, todo::TodoUpdateRequest, todo::ErrorResponse)
),
tags(
(name = "todo", description = "Todo management endpoints.")
),
Expand Down
6 changes: 4 additions & 2 deletions examples/todo-axum/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ async fn main() -> Result<(), Error> {
todo::mark_done,
todo::delete_todo,
),
components(todo::Todo, todo::TodoError),
components(
schemas(todo::Todo, todo::TodoError)
),
modifiers(&SecurityAddon),
tags(
(name = "todo", description = "Todo items management API")
Expand Down Expand Up @@ -74,7 +76,7 @@ mod todo {
use hyper::{HeaderMap, StatusCode};
use serde::{Deserialize, Serialize};
use tokio::sync::Mutex;
use utoipa::{ToSchema, IntoParams};
use utoipa::{IntoParams, ToSchema};

/// In-memonry todo store
pub(super) type Store = Mutex<Vec<Todo>>;
Expand Down
4 changes: 3 additions & 1 deletion examples/todo-tide/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ async fn main() -> std::io::Result<()> {
todo::delete_todo,
todo::mark_done
),
components(todo::Todo, todo::TodoError),
components(
schemas(todo::Todo, todo::TodoError)
),
modifiers(&SecurityAddon),
tags(
(name = "todo", description = "Todo items management endpoints.")
Expand Down
6 changes: 4 additions & 2 deletions examples/todo-warp/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ async fn main() {
#[derive(OpenApi)]
#[openapi(
handlers(todo::list_todos, todo::create_todo, todo::delete_todo),
components(todo::Todo),
components(
schemas(todo::Todo)
),
modifiers(&SecurityAddon),
tags(
(name = "todo", description = "Todo items management API")
Expand Down Expand Up @@ -96,7 +98,7 @@ mod todo {
};

use serde::{Deserialize, Serialize};
use utoipa::{ToSchema, IntoParams};
use utoipa::{IntoParams, ToSchema};
use warp::{hyper::StatusCode, Filter, Reply};

pub type Store = Arc<Mutex<Vec<Todo>>>;
Expand Down
1 change: 1 addition & 0 deletions src/openapi/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -732,6 +732,7 @@ impl ToArray for Ref {}

/// A [`Ref`] or some other type `T`
#[derive(Serialize, Deserialize, Clone)]
#[cfg_attr(feature = "debug", derive(Debug))]
#[serde(untagged)]
pub enum RefOr<T> {
Ref(Ref),
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use quote::{quote, ToTokens};
use syn::{parenthesized, parse::Parse, Attribute, Error, Token};

use crate::{
component::{GenericType, TypeTree},
parse_utils,
schema::{GenericType, TypeTree},
schema_type::SchemaFormat,
AnyValue,
};
Expand Down Expand Up @@ -331,7 +331,7 @@ impl<'c> SchemaAttr<NamedField<'c>> {
fn is_valid_xml_attr(attrs: &SchemaAttr<NamedField>, component_part: &TypeTree) {
if !matches!(
component_part.generic_type,
Some(crate::schema::GenericType::Vec)
Some(crate::component::GenericType::Vec)
) {
if let Some(wrapped_ident) = attrs
.as_ref()
Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions utoipa-gen/src/ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,9 @@ pub mod fn_arg {
use quote::quote;
use syn::{punctuated::Punctuated, token::Comma, PatType, TypePath};

use crate::schema::TypeTree;
use crate::component::TypeTree;
#[cfg(any(feature = "actix_extras", feature = "axum_extras"))]
use crate::schema::ValueType;
use crate::component::ValueType;

use super::IntoParamsType;

Expand Down
2 changes: 1 addition & 1 deletion utoipa-gen/src/ext/actix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ use regex::{Captures, Regex};
use syn::{parse::Parse, punctuated::Punctuated, token::Comma, ItemFn, LitStr};

use crate::{
component::{TypeTree, ValueType},
ext::ArgValue,
path::PathOperation,
schema::{TypeTree, ValueType},
};

use super::{
Expand Down
2 changes: 1 addition & 1 deletion utoipa-gen/src/ext/rocket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ use regex::{Captures, Regex};
use syn::{parse::Parse, LitStr, Token, TypePath};

use crate::{
component::{GenericType, TypeTree, ValueType},
ext::{ArgValue, ArgumentIn, IntoParamsType, MacroArg, ValueArgument},
path::PathOperation,
schema::{GenericType, TypeTree, ValueType},
};

use super::{
Expand Down
6 changes: 3 additions & 3 deletions utoipa-gen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@

use std::{borrow::Cow, mem, ops::Deref};

use component::schema::Schema;
use doc_comment::CommentAttributes;
use schema::schema::Schema;

use component::into_params::IntoParams;
use ext::{PathOperationResolver, PathOperations, PathResolver};
use openapi::OpenApi;
use proc_macro::TokenStream;
use proc_macro_error::{proc_macro_error, OptionExt, ResultExt};
use quote::{quote, ToTokens, TokenStreamExt};
use schema::into_params::IntoParams;

use proc_macro2::{Group, Ident, Punct, TokenStream as TokenStream2};
use syn::{
Expand All @@ -28,11 +28,11 @@ use syn::{
PathArguments, PathSegment, Token, TypePath,
};

mod component;
mod doc_comment;
mod ext;
mod openapi;
mod path;
mod schema;
mod schema_type;
mod security_requirement;

Expand Down
8 changes: 4 additions & 4 deletions utoipa-gen/src/openapi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use proc_macro2::TokenStream;
use quote::{format_ident, quote, quote_spanned, ToTokens};

use crate::{
parse_utils, path::PATH_STRUCT_PREFIX, schema::schema,
component::schema, parse_utils, path::PATH_STRUCT_PREFIX,
security_requirement::SecurityRequirementAttr, Array, ExternalDocs,
};

Expand Down Expand Up @@ -301,7 +301,7 @@ struct Components {

impl Parse for Components {
fn parse(input: ParseStream) -> syn::Result<Self> {
let mut content;
let content;
parenthesized!(content in input);
const EXPECTED_ATTRIBUTE: &str =
"unexpected attribute. expected one of: schemas, responses";
Expand All @@ -318,13 +318,13 @@ impl Parse for Components {
match attribute {
"schemas" => {
let punctuated: Punctuated<Schema, Comma> =
parse_utils::parse_punctuated_within_parenthesis(&mut content)?;
parse_utils::parse_punctuated_within_parenthesis(&content)?;
let mut v: Vec<Schema> = punctuated.into_iter().collect();
schemas.append(&mut v)
}
"responses" => {
let punctuated: Punctuated<Responses, Comma> =
parse_utils::parse_punctuated_within_parenthesis(&mut content)?;
parse_utils::parse_punctuated_within_parenthesis(&content)?;
let mut v: Vec<Responses> = punctuated.into_iter().collect();
responses.append(&mut v)
}
Expand Down
2 changes: 1 addition & 1 deletion utoipa-gen/src/path/parameter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use syn::{
))]
use crate::ext::{ArgumentIn, ValueArgument};
use crate::{
parse_utils, schema::into_params::FieldParamContainerAttributes, AnyValue, Deprecated,
component::into_params::FieldParamContainerAttributes, parse_utils, AnyValue, Deprecated,
Required, Type,
};

Expand Down
2 changes: 1 addition & 1 deletion utoipa-gen/src/path/property.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use quote::{quote, quote_spanned, ToTokens};
use syn::spanned::Spanned;

use crate::{
schema::schema,
component::schema,
schema_type::{SchemaFormat, SchemaType},
Type,
};
Expand Down
1 change: 1 addition & 0 deletions utoipa-gen/src/path/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use crate::{parse_utils, AnyValue, Type};

use super::{property::Property, ContentTypeResolver};

#[cfg_attr(feature = "debug", derive(Debug))]
pub enum Response<'r> {
/// A type that implements `utoipa::IntoResponses`.
IntoResponses(ExprPath),
Expand Down

0 comments on commit 2237cc3

Please sign in to comment.