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

fixing problem with null headers #631

Merged
merged 3 commits into from
Jul 9, 2014
Merged

fixing problem with null headers #631

merged 3 commits into from
Jul 9, 2014

Conversation

Turini
Copy link
Member

@Turini Turini commented Jun 24, 2014

Closes #595.

@Turini Turini self-assigned this Jun 24, 2014
@Turini Turini added the bug label Jun 24, 2014
@garcia-jj
Copy link
Member

Header have more priority than request parameters. With this code if header
is null, vraptor will use request parameter. But the expected behavior is
get null.

@lucascs
Copy link
Member

lucascs commented Jun 25, 2014

@garcia-jj it makes sense, but should we do a considerably much more complex code to support it?

it seems like an unlikely corner case

@garcia-jj
Copy link
Member

@lucascs Why IOGI don't accept null values? The exception is: br.com.caelum.iogi.parameters.Parameter.notNull(Parameter.java:24)

Adding a NullDecorator to RequestAttributeInstantiator solves this case?

@garcia-jj
Copy link
Member

If not, we can change the docs explaining that header only override request parameters if is not null.

@lucascs
Copy link
Member

lucascs commented Jun 25, 2014

null is the billion dollar mistake. It's evil.
The code is almost always clearer if you don't have to think if something is null or not.

I wouldn't add the docs, because it will only confuse the user. I don't think there is a fair use case when we'd use a @HeaderParam to override a request (query) param.

Anyway, we could (perhaps should) implement the @HeaderParam in such a way that you simply can't set it via request (query) params.

something like

parameterInstantiator
   - get the path params and set on request
   - call iogi to use the params and create the objects
   - override all @HeaderParam's to the header content, even if it's nil

what do you think?

@garcia-jj
Copy link
Member

I agree that nullis too evil, but we need to represent when we don't have a value. We can't use Optional yet.

But, for me, I like your suggestion. +1

@Turini
Copy link
Member Author

Turini commented Jun 27, 2014

@lucascs and @garcia-jj, I can changed ParameterInstantiator from:

for (int i = 0; i < valuedParameters.length; i++) {
    valuedParameters[i].setValue(values[i]);
}

to something like that:

for (int i = 0; i < valuedParameters.length; i++) {
    Parameter parameter = valuedParameters[i].getParameter();
    boolean hasHeaderParam = parameter.isAnnotationPresent(HeaderParam.class);
    if (!hasHeaderParam && headerValues.contains(valuedParameters[i].getName())) {
        continue;
    }
    valuedParameters[i].setValue(values[i]);
}

and it will works (I've tested). But the code seems really ugly. Ideas?

@lucascs
Copy link
Member

lucascs commented Jun 28, 2014

I guess the correct way would be something like:

for (int i = 0; i < valuedParameters.length; i++) {
    Parameter parameter = valuedParameters[i].getParameter();
    boolean hasHeaderParam = 
    if (parameter.isAnnotationPresent(HeaderParam.class)) {
        valuedParameters[i].setValue(request.getHeader(<...>));
    } else {
      valuedParameters[i].setValue(values[i]);
   }
}

maybe we need to use converters before setting the value, in this case.

@garcia-jj
Copy link
Member

I don't have an opinion about this change. @lucascs idea sounds good for me. But if we need to call converters, so I think that we only need to override only if header is not null.

@lucascs
Copy link
Member

lucascs commented Jun 28, 2014

If the header is null, we have to set null (or call the converter to maybe provide a good null object)

@garcia-jj
Copy link
Member

I see your point. Fair enough.

@Turini
Copy link
Member Author

Turini commented Jun 30, 2014

I've updated the code with Lucas' suggestions. What do you think?
I don't think we should to use converters, this code is working as well

@lucascs
Copy link
Member

lucascs commented Jul 1, 2014

This way you only support String headers...

but there are other types of headers, like "If-Modified-Since" (date-time), "Location" (URI), Content-Length (long), If-None-Match (list of e-tags, which could be a value object).

Converters would help with this.

@garcia-jj
Copy link
Member

Ready to me supporting only strings by now.

@Turini
Copy link
Member Author

Turini commented Jul 4, 2014

I agree. What do you think @lucascs?
Can we merge and open an issue for other header types?

@lucascs
Copy link
Member

lucascs commented Jul 4, 2014

It's kinda a breaking change if you only support Strings...

but I don't know how many people use this feature.

@Turini
Copy link
Member Author

Turini commented Jul 8, 2014

what about to merge it and open an issue?
if some user ask for it we can prioritize the feature in the next release.

@renanigt
Copy link
Contributor

renanigt commented Jul 8, 2014

I think it's a good idea by now.

When is the next release of VRaptor final version ?

@Turini
Copy link
Member Author

Turini commented Jul 8, 2014

really soon :)

@renanigt
Copy link
Contributor

renanigt commented Jul 8, 2014

Good. :)

@garcia-jj
Copy link
Member

I agree with you. Merge and wait users ask for object support.

@Turini
Copy link
Member Author

Turini commented Jul 9, 2014

agreed, merging it.

Turini added a commit that referenced this pull request Jul 9, 2014
fixing problem with null headers
@Turini Turini merged commit bef52a7 into master Jul 9, 2014
@Turini Turini deleted the nullHeaderParams branch July 9, 2014 12:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

HeaderParam don't work with null headers
4 participants