Skip to content

Commit

Permalink
Merge branch 'ParkerM-repro/rest-enum-array-param'
Browse files Browse the repository at this point in the history
  • Loading branch information
bnasslahsen committed Dec 15, 2022
2 parents 78e136e + b892f35 commit a6228a3
Show file tree
Hide file tree
Showing 13 changed files with 332 additions and 22 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.5</version>
<version>2.7.6</version>
</parent>

<licenses>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@
import io.swagger.v3.oas.models.Paths;
import io.swagger.v3.oas.models.media.StringSchema;
import io.swagger.v3.oas.models.parameters.Parameter;
import io.swagger.v3.oas.models.parameters.PathParameter;
import io.swagger.v3.oas.models.responses.ApiResponses;
import io.swagger.v3.oas.models.servers.Server;
import org.apache.commons.lang3.ArrayUtils;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springdoc.core.converters.AdditionalModelsConverter;
import org.springdoc.core.providers.JavadocProvider;
import org.springdoc.core.providers.ObjectMapperProvider;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
package org.springdoc.core;


import java.util.Collection;

import org.springframework.boot.autoconfigure.condition.AnyNestedCondition;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
package org.springdoc.core;

import java.lang.annotation.Annotation;
import java.math.BigDecimal;
import java.util.function.Predicate;

import io.swagger.v3.oas.models.media.Schema;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.Arrays;

import io.swagger.v3.oas.annotations.enums.ParameterIn;
Expand All @@ -39,6 +40,7 @@
import org.springdoc.core.SpringDocAnnotationsUtils;

import org.springframework.core.MethodParameter;
import org.springframework.core.ResolvableType;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.data.repository.query.Param;
import org.springframework.data.rest.core.mapping.MethodResourceMapping;
Expand All @@ -54,6 +56,7 @@

/**
* The type Data rest operation builder.
*
* @author bnasslahsen
*/
public class DataRestOperationService {
Expand Down Expand Up @@ -202,17 +205,8 @@ private Operation buildSearchOperation(HandlerMethod handlerMethod, DataRestRepo
String pName = parameterMetadatum.getName();
ResourceDescription description = parameterMetadatum.getDescription();
if (description instanceof TypedResourceDescription) {
TypedResourceDescription typedResourceDescription = (TypedResourceDescription) description;
Field fieldType = FieldUtils.getField(TypedResourceDescription.class, "type", true);
Class<?> type;
try {
type = (Class<?>) fieldType.get(typedResourceDescription);
}
catch (IllegalAccessException e) {
LOGGER.warn(e.getMessage());
type = String.class;
}
Schema<?> schema = SpringDocAnnotationsUtils.resolveSchemaFromType(type, openAPI.getComponents(), null, null);
Type type = getParameterType(pName,method,description);
Schema<?> schema = SpringDocAnnotationsUtils.extractSchema(openAPI.getComponents(), type, null, null);
Parameter parameter = getParameterFromAnnotations(openAPI, methodAttributes, method, pName);
if (parameter == null)
parameter = new Parameter().name(pName).in(ParameterIn.QUERY.toString()).schema(schema);
Expand All @@ -231,6 +225,39 @@ private Operation buildSearchOperation(HandlerMethod handlerMethod, DataRestRepo
return operation;
}

/**
* Gets parameter type.
*
* @param pName the p name
* @param method the method
* @param description the description
* @return the parameter type
*/
private Type getParameterType(String pName, Method method, ResourceDescription description) {
Type type = null;
java.lang.reflect.Parameter[] parameters = method.getParameters();
for (int i = 0; i < parameters.length; i++) {
java.lang.reflect.Parameter parameter = parameters[i];
if (pName.equals(parameter.getName()) || pName.equals(parameter.getAnnotation(Param.class).value())) {
ResolvableType resolvableType = ResolvableType.forMethodParameter(method, i);
type = resolvableType.getType();
break;
}
}
if (type == null) {
TypedResourceDescription typedResourceDescription = (TypedResourceDescription) description;
Field fieldType = FieldUtils.getField(TypedResourceDescription.class, "type", true);
try {
type = (Type) fieldType.get(typedResourceDescription);
}
catch (IllegalAccessException e) {
LOGGER.warn(e.getMessage());
type = String.class;
}
}
return type;
}

/**
* Update parameter from annotations parameter.
*
Expand Down Expand Up @@ -279,6 +306,7 @@ private Operation initOperation(HandlerMethod handlerMethod, Class<?> domainType

/**
* Add operation description.
*
* @param operation the operation
* @param requestMethod the request method
* @param entity the entity
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
*
* * Copyright 2019-2022 the original author or authors.
* *
* * Licensed under the Apache License, Version 2.0 (the "License");
* * you may not use this file except in compliance with the License.
* * You may obtain a copy of the License at
* *
* * https://www.apache.org/licenses/LICENSE-2.0
* *
* * Unless required by applicable law or agreed to in writing, software
* * distributed under the License is distributed on an "AS IS" BASIS,
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* * See the License for the specific language governing permissions and
* * limitations under the License.
*
*/

package test.org.springdoc.api.app36;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
public class EnumFieldHolder {

public enum EnumField {

FOO, BAR;

}

@Id
@GeneratedValue
private Long id;

private EnumField enumField;

public Long getId() {
return id;
}

public EnumField getEnumField() {
return enumField;
}

public void setEnumField(EnumField enumField) {
this.enumField = enumField;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
*
* * Copyright 2019-2022 the original author or authors.
* *
* * Licensed under the Apache License, Version 2.0 (the "License");
* * you may not use this file except in compliance with the License.
* * You may obtain a copy of the License at
* *
* * https://www.apache.org/licenses/LICENSE-2.0
* *
* * Unless required by applicable law or agreed to in writing, software
* * distributed under the License is distributed on an "AS IS" BASIS,
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* * See the License for the specific language governing permissions and
* * limitations under the License.
*
*/

package test.org.springdoc.api.app36;

import java.util.List;

import org.springframework.data.repository.Repository;
import org.springframework.data.repository.query.Param;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
import org.springframework.data.util.Streamable;

@RepositoryRestResource
public interface EnumFieldHolderRepository extends Repository<EnumFieldHolder, Long> {

Streamable<EnumFieldHolder> findAllByEnumField(@Param("enumField") EnumFieldHolder.EnumField enumField);

Streamable<EnumFieldHolder> findAllByEnumFieldIn(@Param("enumFields") List<EnumFieldHolder.EnumField> enumFields);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
*
* * Copyright 2019-2022 the original author or authors.
* *
* * Licensed under the Apache License, Version 2.0 (the "License");
* * you may not use this file except in compliance with the License.
* * You may obtain a copy of the License at
* *
* * https://www.apache.org/licenses/LICENSE-2.0
* *
* * Unless required by applicable law or agreed to in writing, software
* * distributed under the License is distributed on an "AS IS" BASIS,
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* * See the License for the specific language governing permissions and
* * limitations under the License.
*
*/

package test.org.springdoc.api.app36;

import test.org.springdoc.api.AbstractSpringDocTest;

import org.springframework.boot.autoconfigure.SpringBootApplication;

public class SpringDocApp36Test extends AbstractSpringDocTest {

@SpringBootApplication
static class SpringDocTestApp {

}

}
Loading

0 comments on commit a6228a3

Please sign in to comment.