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

impl "multipart/form-data" support #418

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open

Conversation

drahnr
Copy link
Contributor

@drahnr drahnr commented Apr 11, 2023

Currently form data handling is not implemented.

This PR has an initial, dirty implementation on which I'll iterate over the next weeks.

Closes #402

@drahnr drahnr force-pushed the main branch 2 times, most recently from 00b610a to 7979fe2 Compare April 11, 2023 16:30
@drahnr drahnr changed the title first take at form data impl "multipart/form-data" support Apr 12, 2023
@drahnr
Copy link
Contributor Author

drahnr commented Apr 12, 2023

@ahl if you have a few minutes to spare, it'd be great to get some feedback, in particular on:

  • is Generator the intended place for additional metadata?
  • is the boundary between parsing and generation sufficiently upheld in your opinion?
  • do you expect to panic on failed assumptions - currently the generation uses a filter iterator modifier and hence does not panic.

Copy link
Collaborator

@ahl ahl left a comment

Choose a reason for hiding this comment

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

Thanks very much for submitting this. A great start. I left some scattered comments at varying levels of detail. The highest order bit comment I offer is to please add test cases--at a minimum modifying one of the openapi documents to include a body parameter of this type.


@ahl if you have a few minutes to spare, it'd be great to get some feedback, in particular on:

is Generator the intended place for additional metadata?

I mean... it can, but in the case of forms my inclination is to inline that code in the body method.

is the boundary between parsing and generation sufficiently upheld in your opinion?

I think so modulo the prior comment.

do you expect to panic on failed assumptions - currently the generation uses a filter iterator modifier and hence does not panic.

I mean... sure? It depends? I think the real answer is "yes" except when it would screw up currently working users in unexpected ways... which is hard to say.

@@ -11,6 +11,7 @@ readme = "../README.md"
heck = "0.4.1"
getopts = "0.2"
indexmap = "1.9"
itertools = "0.10"
Copy link
Collaborator

Choose a reason for hiding this comment

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

is this used?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Woud you mind if I add a deny rule?

Copy link
Contributor Author

@drahnr drahnr Apr 12, 2024

Choose a reason for hiding this comment

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

Addressed in 4323c11

@@ -328,7 +345,7 @@ impl Generator {

let typ = self
.type_space
.add_type_with_name(&schema, Some(name))?;
.add_type_with_name(&schema, Some(dbg!(name)))?;
Copy link
Collaborator

Choose a reason for hiding this comment

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

remove before merging?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I really thought I fixed that..

Comment on lines +878 to +886
let url_renames =
HashMap::from_iter(method.params.iter().filter_map(|param| {
match &param.kind {
OperationParameterKind::Path => {
Some((&param.api_name, &param.name))
}
_ => None,
}
_ => None,
})
.collect();
}));
Copy link
Collaborator

Choose a reason for hiding this comment

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

spurious?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Somewhat yes, collect elides what the elemets are being collected into which I personally find a lot easier to reason about code. Will remove.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Thanks.

progenitor-impl/src/method.rs Outdated Show resolved Hide resolved
progenitor-impl/src/method.rs Outdated Show resolved Hide resolved
fn form_from_raw<
S: AsRef<str>,
T: AsRef<[u8]>,
I: Sized + IntoIterator<Item = (S, T)>,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why IntoIterator?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because a form can contain multiple streams. That's the least limiting for an Impl and sufficient for the R..BuilderExt trait. But with the comments below this might become obsolete

.map(|prop_typ| (prop_name, prop_typ))
})
);
let properties = syn::punctuated::Punctuated::<_, syn::Token![,]>::from_iter(
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
let properties = syn::punctuated::Punctuated::<_, syn::Token![,]>::from_iter(
let properties =

#(#properties,)* below

Copy link
Contributor Author

@drahnr drahnr Apr 9, 2024

Choose a reason for hiding this comment

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

The above creates a punctuated list, which can both be used with quote to create the correct expansion, while also being iterable, can change to Vec<TokenStream> and use quote!{ #(#propeties),* if desired.

pub fn as_form<'f>(&'f self) -> impl std::iter::Iterator<Item=(&'static str, &'f [u8])> {
[#properties]
.into_iter()
.filter_map(|(name, val)| val.as_ref().map(|val| (name, val.as_slice())))
Copy link
Collaborator

Choose a reason for hiding this comment

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

Does this mean that all val have to be strings?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, not at all. It means all values have to be convertible to a stream of bytes. The representation is limiting and should be improved, i.e. it only allows for basic usage key but not for keys derived from objects where the keys end up as foo[bar].sub

Copy link
Collaborator

Choose a reason for hiding this comment

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

I see. Are there types we might encounter here other than String and Vec[u8] that satisfy this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Any type really, such that we could call as_form for nested types. That was the direction I wanted to go into. Hence avoiding the direct expansion. There are a few pitfalls, i.e. metadata desc such as mime type and filename that are commonly expected but not defined in the openapi spec. I am not yet sure how to handle this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Note that the PR here is in a state of rough draft/idea/make my immediate needs meet, so I am open to changing direction as you see fit.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Gentle ping on this one.

) => {
Some(quote! {
// This uses progenitor_client::RequestBuilderExt which sets up a simple form data based on bytes
.form_from_raw(body.as_form())?
Copy link
Collaborator

Choose a reason for hiding this comment

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

did you think about unrolled the as_form logic here? That seems potentially cleaner than an impl on the generated type.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I try to avoid writing to much code with quote if it can be formalized as a simple trait implementation without many constraints.

@PlamenHristov
Copy link

PlamenHristov commented Apr 2, 2024

Hey team,

Do you think the above has the potential to be merged. If not, is there anything I can do to complete the PR so we can merge it ?

@ahl
Copy link
Collaborator

ahl commented Apr 3, 2024

@PlamenHristov I think you'd be welcome to make a fork of this work, and resolve conflicts. As I recall this was pretty close. I don't see any tests and that would be another good addition to this work.

@drahnr
Copy link
Contributor Author

drahnr commented Apr 4, 2024

Happy to pick this up again, the silence on my last set of comments and the scarcity of time made me question if it's worth continuing. If @ahl believes it's close to merging, I am happy to push it over the finish line. @PlamenHristov if you want to help, adding test cases is probably the most effective way

@PlamenHristov
Copy link

@drahnr/ @ahl thank you for both your responses.

@drahnr yeah could do that. Let me know once you believe the implementation is done.

@ahl
Copy link
Collaborator

ahl commented Apr 4, 2024

Happy to pick this up again, the silence on my last set of comments and the scarcity of time made me question if it's worth continuing. If @ahl believes it's close to merging, I am happy to push it over the finish line. @PlamenHristov if you want to help, adding test cases is probably the most effective way

@drahnr my apologies for discouraging you. I appreciate the contribution and suffer from the same endemic time scarcity!

@drahnr
Copy link
Contributor Author

drahnr commented Apr 11, 2024

@ahl *control is requiring auth. It's not needed unless there are more steps hiding behind it. The PR is still incomplete and requires some more testing.

Comment on lines +18 to +23
# [patch."https://github.com/oxidecomputer/typify"]
# typify = { path = "../typify/typify" }

#[patch.crates-io]
# [patch.crates-io]
#serde_tokenstream = { path = "../serde_tokenstream" }
#typify = { path = "../typify/typify" }
# typify = { path = "../typify/typify" }
Copy link
Collaborator

Choose a reason for hiding this comment

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

revert?

Comment on lines +878 to +886
let url_renames =
HashMap::from_iter(method.params.iter().filter_map(|param| {
match &param.kind {
OperationParameterKind::Path => {
Some((&param.api_name, &param.name))
}
_ => None,
}
_ => None,
})
.collect();
}));
Copy link
Collaborator

Choose a reason for hiding this comment

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

Thanks.

// "format": "binary"
// }

let _mapped = match schema.item(components)? {
Copy link
Collaborator

Choose a reason for hiding this comment

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

we're ignoring the value we create?

};

println!("cargo::rerun-if-changed={}", path_str);
Copy link
Collaborator

Choose a reason for hiding this comment

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

what's going on here?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

form-data as content type yet to be implemented
3 participants