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

fix(actix): support parsing arrays for schemas #531

Merged
merged 1 commit into from
Sep 10, 2024
Merged
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
16 changes: 12 additions & 4 deletions macros/src/actix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1443,7 +1443,9 @@ fn add_optional_impl(name: &Ident, generics: &Generics) -> proc_macro2::TokenStr

fn get_field_type(field: &Field) -> Option<proc_macro2::TokenStream> {
match field.ty {
Type::Path(_) | Type::Reference(_) => Some(address_type_for_fn_call(&field.ty)),
Type::Path(_) | Type::Reference(_) | Type::Array(_) => {
Some(address_type_for_fn_call(&field.ty))
}
_ => {
emit_warning!(
field.ty.span().unwrap(),
Expand Down Expand Up @@ -1491,7 +1493,10 @@ fn handle_unnamed_field_struct(
continue;
}

let ty_ref = get_field_type(field);
let ty_ref = match get_field_type(field) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good place for let-else if that's allowed by the msrv

Copy link
Collaborator Author

@tiagolobocastro tiagolobocastro Sep 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm seems that it depends on the imported crates
With my lockfile it's 1.60 but if I delete it it's 1.65 due to:

┌─────────────────────────────────────────────────────────────────────
│ error: package hashbrown v0.14.5 cannot be built because it requires rustc 1.63.0 or newer, while the currently active rustc version is 1.62.1 │
│ error: package serde_yaml v0.9.34+deprecated cannot be built because it requires rustc 1.64 or newer, while the currently active rustc version is 1.63.0 │
│ error: package regex-automata v0.4.7 cannot be built because it requires rustc 1.65 or newer, while the currently active rustc version is 1.64.0 │
└────────────────────────────────────────────────────────────────

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough. The MSRV aware resolver can't come soon enough

Some(ty_ref) => ty_ref,
None => continue,
};

let docs = extract_documentation(&field.attrs);
let docs = docs.trim();
Expand Down Expand Up @@ -1626,7 +1631,10 @@ fn handle_field_struct(
field_name = prop.rename(&field_name);
}

let ty_ref = get_field_type(field);
let ty_ref = match get_field_type(field) {
Some(ty_ref) => ty_ref,
None => continue,
};

let docs = extract_documentation(&field.attrs);
let docs = docs.trim();
Expand Down Expand Up @@ -1729,7 +1737,7 @@ fn handle_enum(e: &DataEnum, serde: &SerdeProps, props_gen: &mut proc_macro2::To
/// `Vec::<T>::foo`. Something similar applies to `str`. This function takes
/// care of that special treatment.
fn address_type_for_fn_call(old_ty: &Type) -> proc_macro2::TokenStream {
if let Type::Reference(_) = old_ty {
if matches!(old_ty, Type::Reference(_) | Type::Array(_)) {
return quote!(<(#old_ty)>);
}

Expand Down
77 changes: 77 additions & 0 deletions tests/test_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5518,3 +5518,80 @@ fn test_wrap() {
},
);
}

#[test]
fn test_array() {
#[derive(Deserialize, Serialize, Apiv2Schema)]
/// Pets are awesome!
struct Pet {
/// Names of other pet friends!
friends: [String; 4],
}

#[api_v2_operation]
fn echo_pets() -> impl Future<Output = Result<web::Json<Vec<Pet>>, Error>> {
fut_ok(web::Json(vec![]))
}

run_and_check_app(
|| {
App::new()
.wrap_api()
.service(web::resource("/pets").route(web::get().to(echo_pets)))
.with_json_spec_at("/api/spec")
.build()
},
|addr| {
let resp = CLIENT
.get(&format!("http://{}/api/spec", addr))
.send()
.expect("request failed?");

check_json(
resp,
json!({
"definitions": {
"Pet": {
"description": "Pets are awesome!",
"properties": {
"friends": {
"description": "Names of other pet friends!",
"type": "array",
"items": {
"type": "string",
}
}
},
"required": [
"friends"
],
"type": "object"
}
},
"info": {
"title": "",
"version": ""
},
"paths": {
"/pets": {
"get": {
"responses": {
"200": {
"description": "OK",
"schema": {
"items": {
"$ref": "#/definitions/Pet"
},
"type": "array"
}
}
}
}
}
},
"swagger": "2.0"
}),
);
},
);
}
Loading