-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Feature/Support for QueryValue as a object #11787
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
base: 4.9.x
Are you sure you want to change the base?
Changes from all commits
8f061b2
90cdb0a
10ed6d2
1c985b7
cac5b32
f0fb8f7
2513fda
a1745b4
6e9a62f
88ddb6a
797c29f
21dd30a
77e0a29
3a7cccf
ed0da60
b816e29
9f096ad
d11b231
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package io.micronaut.http.server.netty.binding; | ||
|
||
import io.micronaut.core.annotation.Introspected; | ||
|
||
@Introspected | ||
public record PaginationRequest(Integer page, Integer size) {} | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,7 +16,11 @@ | |
package io.micronaut.http.bind.binders; | ||
|
||
import io.micronaut.core.annotation.AnnotationMetadata; | ||
import io.micronaut.core.annotation.Nullable; | ||
import io.micronaut.core.beans.BeanIntrospection; | ||
import io.micronaut.core.beans.BeanIntrospector; | ||
import io.micronaut.core.bind.annotation.AbstractArgumentBinder; | ||
import io.micronaut.core.bind.annotation.Bindable; | ||
import io.micronaut.core.convert.ArgumentConversionContext; | ||
import io.micronaut.core.convert.ConversionService; | ||
import io.micronaut.core.convert.format.Format; | ||
|
@@ -28,6 +32,7 @@ | |
import io.micronaut.http.uri.UriMatchVariable; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
/** | ||
|
@@ -53,7 +58,7 @@ public QueryValueArgumentBinder(ConversionService conversionService) { | |
* Constructor. | ||
* | ||
* @param conversionService conversion service | ||
* @param argument The argument | ||
* @param argument The argument | ||
*/ | ||
public QueryValueArgumentBinder(ConversionService conversionService, Argument<T> argument) { | ||
super(conversionService, argument); | ||
|
@@ -91,6 +96,18 @@ public BindingResult<T> bind(ArgumentConversionContext<T> context, HttpRequest<? | |
return BindingResult.unsatisfied(); | ||
} | ||
|
||
BindingResult<T> bindSimpleResult = bindSimple(context, source, annotationMetadata, parameters, argument); | ||
if (bindSimpleResult.isSatisfied()) { | ||
return bindSimpleResult; | ||
} | ||
return bindPojo(context, parameters, argument); | ||
} | ||
|
||
private BindingResult<T> bindSimple(ArgumentConversionContext<T> context, | ||
HttpRequest<?> source, | ||
AnnotationMetadata annotationMetadata, | ||
ConvertibleMultiValues<String> parameters, | ||
Argument<T> argument) { | ||
// First try converting from the ConvertibleMultiValues type and if conversion is successful, return it. | ||
// Otherwise, use the given uri template to deduce what to do with the variable | ||
Optional<T> multiValueConversion; | ||
|
@@ -130,6 +147,50 @@ public BindingResult<T> bind(ArgumentConversionContext<T> context, HttpRequest<? | |
return doBind(context, parameters, BindingResult.unsatisfied()); | ||
} | ||
|
||
private BindingResult<T> bindPojo(ArgumentConversionContext<T> context, | ||
ConvertibleMultiValues<String> parameters, | ||
Argument<T> argument) { | ||
Optional<BeanIntrospection<T>> introspectionOpt = BeanIntrospector.SHARED.findIntrospection(argument.getType()); | ||
if (introspectionOpt.isEmpty()) { | ||
return BindingResult.unsatisfied(); | ||
} | ||
|
||
BeanIntrospection<T> introspection = introspectionOpt.get(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this should be altered to use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I made the changes with the introspection.builder(). Seems to work for object with custom constructor but I made a test for record and don't work. Am I missing something? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is because records are not supported in Groovy and you a rewriting the test in Groovy. Probably will be fixed by #11832 |
||
BeanIntrospection.Builder<T> introspectionBuilder = introspection.builder(); | ||
graemerocher marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Argument<?>[] builderArguments = introspectionBuilder.getBuilderArguments(); | ||
|
||
for (int index = 0; index < builderArguments.length; index++) { | ||
Argument<?> builderArg = builderArguments[index]; | ||
String propertyName = builderArg.getName(); | ||
List<String> values = parameters.getAll(propertyName); | ||
boolean hasNoValue = values.isEmpty(); | ||
@Nullable String defaultValue = hasNoValue ? builderArg | ||
.getAnnotationMetadata() | ||
.stringValue(Bindable.class, "defaultValue").orElse(null) : null; | ||
|
||
ArgumentConversionContext<?> conversionContext = context.with(builderArg); | ||
Optional<?> converted = hasNoValue ? conversionService.convert(defaultValue, conversionContext) :conversionService.convert(values, conversionContext); | ||
if (converted.isPresent()) { | ||
try { | ||
@SuppressWarnings({"unchecked"}) | ||
Argument<Object> rawArg = (Argument<Object>) builderArg; | ||
introspectionBuilder.with(index, rawArg, converted.get()); | ||
} catch (Exception e) { | ||
context.reject(builderArg, e); | ||
return BindingResult.unsatisfied(); | ||
} | ||
} | ||
} | ||
|
||
try { | ||
T instance = introspectionBuilder.build(); | ||
return () -> Optional.of(instance); | ||
} catch (Exception e) { | ||
context.reject(argument, e); | ||
return BindingResult.unsatisfied(); | ||
} | ||
} | ||
|
||
@Override | ||
protected String getParameterName(Argument<T> argument) { | ||
return argument.getAnnotationMetadata().stringValue(QueryValue.class).orElse(argument.getName()); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@MarianConstantinMarica just rename
PaginationRequest.java
toPaginationRequest.groovy
and after this all must work correctly