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 #1549 - Support bodyParam attribute without form parameter name too map the whole body to a single parameter #1676

Closed
wants to merge 2 commits into from

Conversation

wilzbach
Copy link
Member

@wilzbach wilzbach commented Feb 6, 2017

As discussed in #1549 and #1422:

@bodyParam("obj")
string postFoo(FooType obj);

Moreover if

  • no @bodyParam annotation is provided
  • the method is automatically recognized as POST
  • there's only one @bodyParam parameter
string postFoo(FooType obj);

then the entire Json body is serialized into this parameter. This might be a breaking change, but imho it's a sane default setting as most APIs want to serialize the entire request body and vibe.d hasn't reached stable yet.

…name to map the whole body to a single parameter
ReneZwanenburg added a commit to ReneZwanenburg/ArangoClient that referenced this pull request Feb 13, 2017
This will not be necessary anymore if
vibe-d/vibe.d#1676 gets pulled
@zunkree
Copy link

zunkree commented Feb 27, 2017

Any news?

@s-ludwig
Copy link
Member

there's only one @bodyParam parameter
string postFoo(FooType obj);
then the entire Json body is serialized into this parameter. This might be a breaking change, but imho it's a sane default setting as most APIs want to serialize the entire request body and vibe.d hasn't reached stable yet.

This may go a bit too far. The library may not be declared stable, but is already used in production all over the place, so silent breaking changes like these are usually a no-go. Generally, I think that keeping things orthogonal here may be a good idea. For example, in the web frontend the equivalent rule would be very surprising and generally undesirable (form with only a single input field).

@s-ludwig
Copy link
Member

Apart from the open questions, the implementation looks good and ready to merge!

@@ -463,11 +463,14 @@ package struct WebParamAttribute {
string field;
}

package(vibe.web) enum bodyParamWholeName = "bodyParamWhole";
Copy link
Member

Choose a reason for hiding this comment

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

What about using an empty string here and asserting in the two-argument @bodyParam overload that the field name is always non-empty? That would eliminate any possibility for name clashes. I'd also tend to encapsulate the "whole body" logic into WebParamAttribute (e.g. WebParamAttribute.isAllInOne or similar, which performs the name comparison) - thinking about it, it may also make sense to extend this to query string parameters later on.

@@ -352,6 +352,9 @@ unittest
* This is to be consistent with the way D 'out' and 'ref' works.
* However, it makes no sense to have 'ref' or 'out' parameters on
* body or query parameter, so those are treated as error at compile time.
*
* To serialize the entire Json body into one parameter, simply name the
* parameter "foo"
Copy link
Member

Choose a reason for hiding this comment

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

The "foo" was a surprise here, was that left over from an earlier approach?

@s-ludwig
Copy link
Member

Hm, I can't really adjust to the new review feature. I guess I'll just stick to normal comments.

@wilzbach
Copy link
Member Author

I'd also tend to encapsulate the "whole body" logic into WebParamAttribute (e.g. WebParamAttribute.isAllInOne or similar, which performs the name comparison) - thinking about it, it may also make sense to extend this to query string parameters later on.

I am not sure how I would be able to access the WebParamAttribute object from the jsonMethodHandler

This may go a bit too far. The library may not be declared stable, but is already used in production all over the place, so silent breaking changes like these are usually a no-go. Generally, I think that keeping things orthogonal here may be a good idea. For example, in the web frontend the equivalent rule would be very surprising and generally undesirable (form with only a single input field).

I get your point and I removed the default rule to serialize the entire body into an parameter if only parameter is provided, s.t. this isn't blocking this PR.
However, imho in the context of REST APIs it's very common to accept a custom object - the best example being the GH API itself. Or in Vibe.d "words" a usual API looks like this:

auto postA(FooType1 obj);
auto postB(FooType2 obj);
auto postC(FooType3 obj);
....

Maybe we can use a special name like it's currently done for id?

@s-ludwig
Copy link
Member

I am not sure how I would be able to access the WebParamAttribute object from the jsonMethodHandler

Right, I didn't think of that. But looking at it, the cleanest way to pass that information seems to be to create a new ParameterType for full body parameters. Then RestInterface!T would access the WebParamAttribute and the logic could stay close together.

I get your point and I removed the default rule to serialize the entire body into an parameter if only parameter is provided, s.t. this isn't blocking this PR.
However, imho in the context of REST APIs it's very common to accept a custom object - the best example being the GH API itself. Or in Vibe.d "words" a usual API looks like this:

auto postA(FooType1 obj);
auto postB(FooType2 obj);
auto postC(FooType3 obj);
....

Maybe we can use a special name like it's currently done for id?

I was thinking about using a default name, too, but the "id" feature is rather questionable today when the Collection functionality is taken into account (each "id" needs to have a unique name there, the auth module is in a similar boat). But I can definitely see how the difference that this would make cannot be neglected. I'll have to think about this some more before being able to give a meaningful answer.

@s-ludwig
Copy link
Member

Suggestions implemented in #1723.

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

Successfully merging this pull request may close these issues.

None yet

3 participants