Skip to content

Commit

Permalink
code review
Browse files Browse the repository at this point in the history
  • Loading branch information
bnasslahsen committed Apr 24, 2021
1 parent 15fba08 commit beeb817
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ public Parameter buildParams(ParameterInfo parameterInfo, int length, Components
}
// By default
if (!isRequestBodyParam(requestMethod, parameterInfo, length)) {
parameterInfo.setRequired(!methodParameter.isOptional());
parameterInfo.setRequired(!((DelegatingMethodParameter) methodParameter).isNotRequired() && !methodParameter.isOptional());
parameterInfo.setParamType(QUERY_PARAM);
parameterInfo.setDefaultValue(null);
return this.buildParam(parameterInfo, components, jsonView);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,20 +68,27 @@ public class DelegatingMethodParameter extends MethodParameter {
*/
private boolean isParameterObject;

/**
* The Is not required.
*/
private boolean isNotRequired;

/**
* Instantiates a new Delegating method parameter.
*
* @param delegate the delegate
* @param parameterName the parameter name
* @param additionalParameterAnnotations the additional parameter annotations
* @param isParameterObject the is parameter object
* @param isNotRequired the is required
*/
DelegatingMethodParameter(MethodParameter delegate, String parameterName, Annotation[] additionalParameterAnnotations, boolean isParameterObject) {
DelegatingMethodParameter(MethodParameter delegate, String parameterName, Annotation[] additionalParameterAnnotations, boolean isParameterObject, boolean isNotRequired) {
super(delegate);
this.delegate = delegate;
this.additionalParameterAnnotations = additionalParameterAnnotations;
this.parameterName = parameterName;
this.isParameterObject = isParameterObject;
this.isNotRequired = isNotRequired;
}

/**
Expand All @@ -106,7 +113,7 @@ public static MethodParameter[] customize(String[] pNames, MethodParameter[] par
}
else {
String name = pNames != null ? pNames[i] : p.getParameterName();
explodedParameters.add(new DelegatingMethodParameter(p, name, null, false));
explodedParameters.add(new DelegatingMethodParameter(p, name, null, false, false));
}
}
return explodedParameters.toArray(new MethodParameter[0]);
Expand Down Expand Up @@ -188,6 +195,24 @@ public void initParameterNameDiscovery(ParameterNameDiscoverer parameterNameDisc
delegate.initParameterNameDiscovery(parameterNameDiscoverer);
}

/**
* Is not required boolean.
*
* @return the boolean
*/
public boolean isNotRequired() {
return isNotRequired;
}

/**
* Sets not required.
*
* @param notRequired the not required
*/
public void setNotRequired(boolean notRequired) {
isNotRequired = notRequired;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,8 @@

import io.swagger.v3.core.util.PrimitiveType;
import io.swagger.v3.oas.annotations.Parameter;
import org.apache.commons.lang3.ArrayUtils;

import org.springframework.core.MethodParameter;
import org.springframework.lang.Nullable;

/**
* The type Method parameter pojo extractor.
Expand All @@ -63,11 +61,6 @@ class MethodParameterPojoExtractor {
private MethodParameterPojoExtractor() {
}

/**
* The constant NULLABLE_ANNOTATION.
*/
private static final Nullable NULLABLE_ANNOTATION = getNullable();

/**
* The constant SIMPLE_TYPE_PREDICATES.
*/
Expand Down Expand Up @@ -172,33 +165,21 @@ private static Stream<MethodParameter> extractTypeParameter(
private static Stream<MethodParameter> fromSimpleClass(Class<?> paramClass, Field field, String fieldNamePrefix) {
Annotation[] fieldAnnotations = field.getDeclaredAnnotations();
try {
Nullable nullableField = NULLABLE_ANNOTATION;
if (isOptional(field))
fieldAnnotations = ArrayUtils.add(fieldAnnotations, nullableField);
Parameter parameter = field.getAnnotation(Parameter.class);
boolean isNotRequired = parameter == null || !parameter.required();
Annotation[] finalFieldAnnotations = fieldAnnotations;
return Stream.of(Introspector.getBeanInfo(paramClass).getPropertyDescriptors())
.filter(d -> d.getName().equals(field.getName()))
.map(PropertyDescriptor::getReadMethod)
.filter(Objects::nonNull)
.map(method -> new MethodParameter(method, -1))
.map(param -> new DelegatingMethodParameter(param, fieldNamePrefix + field.getName(), finalFieldAnnotations, true));
.map(param -> new DelegatingMethodParameter(param, fieldNamePrefix + field.getName(), finalFieldAnnotations, true, isNotRequired));
}
catch (IntrospectionException e) {
return Stream.of();
}
}

/**
* Is optional boolean.
*
* @param field the field
* @return the boolean
*/
private static boolean isOptional(Field field) {
Parameter parameter = field.getAnnotation(Parameter.class);
return parameter == null || !parameter.required();
}

/**
* All fields of list.
*
Expand Down Expand Up @@ -263,29 +244,4 @@ static void removeSimpleTypes(Class<?>... classes) {
SIMPLE_TYPES.removeAll(Arrays.asList(classes));
}

/**
* The type Nullable field class.
* @author bnasslahsen
*/
private class NullableFieldClass {
/**
* The Nullable field.
*/
@Nullable
private String nullableField;
}

/**
* Gets nullable.
*
* @return the nullable
*/
private static Nullable getNullable() {
try {
return NullableFieldClass.class.getDeclaredField("nullableField").getAnnotation(Nullable.class);
}
catch (NoSuchFieldException e) {
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ private Operation buildSearchOperation(HandlerMethod handlerMethod, DataRestRepo
HandlerMethod repositoryHandlerMethod = new HandlerMethod(methodResourceMapping.getMethod().getDeclaringClass(), methodResourceMapping.getMethod());
MethodParameter[] parameters = repositoryHandlerMethod.getMethodParameters();
for (MethodParameter methodParameter : parameters) {
dataRestRequestService.buildCommonParameters(openAPI, requestMethod, methodAttributes, operation, new String[] { methodParameter.getParameterName() }, new MethodParameter[] { methodParameter }, resourceMetadata, dataRestRepository);
dataRestRequestService.buildCommonParameters(openAPI, requestMethod, methodAttributes, operation, new String[] { methodParameter.getParameterName() }, new MethodParameter[] { methodParameter }, dataRestRepository);
}
}

Expand Down Expand Up @@ -225,7 +225,7 @@ private Operation buildSearchOperation(HandlerMethod handlerMethod, DataRestRepo
MethodParameter[] parameters = handlerMethod.getMethodParameters();
Arrays.stream(parameters).filter(methodParameter -> DefaultedPageable.class.equals(methodParameter.getParameterType())).findAny()
.ifPresent(methodParameterPage -> dataRestRequestService.buildCommonParameters(openAPI, requestMethod, methodAttributes, operation,
new String[] { methodParameterPage.getParameterName() }, new MethodParameter[] { methodParameterPage }, resourceMetadata, dataRestRepository));
new String[] { methodParameterPage.getParameterName() }, new MethodParameter[] { methodParameterPage }, dataRestRepository));
}
dataRestResponseService.buildSearchResponse(operation, handlerMethod, openAPI, methodResourceMapping, domainType, methodAttributes, resourceMetadata, dataRestRepository);
tagsBuilder.buildSearchTags(operation, handlerMethod, dataRestRepository, method);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public void buildParameters(OpenAPI openAPI, HandlerMethod handlerMethod, Reques
String[] reflectionParametersNames = Arrays.stream(handlerMethod.getMethod().getParameters()).map(java.lang.reflect.Parameter::getName).toArray(String[]::new);
if (pNames == null || Arrays.stream(pNames).anyMatch(Objects::isNull))
pNames = reflectionParametersNames;
buildCommonParameters(openAPI, requestMethod, methodAttributes, operation, pNames, parameters, resourceMetadata, dataRestRepository);
buildCommonParameters(openAPI, requestMethod, methodAttributes, operation, pNames, parameters, dataRestRepository);
}

/**
Expand All @@ -145,11 +145,10 @@ public void buildParameters(OpenAPI openAPI, HandlerMethod handlerMethod, Reques
* @param operation the operation
* @param pNames the p names
* @param parameters the parameters
* @param resourceMetadata the resource metadata
* @param dataRestRepository the data rest repository
*/
public void buildCommonParameters(OpenAPI openAPI, RequestMethod requestMethod, MethodAttributes methodAttributes, Operation operation, String[] pNames, MethodParameter[] parameters,
ResourceMetadata resourceMetadata, DataRestRepository dataRestRepository) {
DataRestRepository dataRestRepository) {
parameters = DelegatingMethodParameter.customize(pNames, parameters, parameterBuilder.getDelegatingMethodParameterCustomizer());
Class<?> domainType = dataRestRepository.getDomainType();
for (MethodParameter methodParameter : parameters) {
Expand Down

0 comments on commit beeb817

Please sign in to comment.