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

Feature/spring5 #2273

Merged
merged 19 commits into from
Jul 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions gradle/dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ ext {
jsonPath = "2.4.0"
jsonAssert = "1.5.0"
mapstruct = "1.2.0.Final"
mockito = "1.10.19"
mockito = "2.15.0"
objenesis = "2.6"
fastClasspathScanner = "3.0.3"
servlet = "3.1.0"
Expand All @@ -34,7 +34,7 @@ ext {
"org.springframework:spring-test:${spring}",
"cglib:cglib-nodep:$cglib",
"org.objenesis:objenesis:$objenesis",
"org.mockito:mockito-all:${mockito}",
"org.mockito:mockito-core:${mockito}",
"com.jayway.jsonpath:json-path:${jsonPath}",
"org.slf4j:slf4j-simple:${slf4j}",
"org.yaml:snakeyaml:${snakeyaml}",
Expand Down
2 changes: 1 addition & 1 deletion gradle/ide.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,4 @@ idea {
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ class ActionSpecification {
private final String path;

public ActionSpecification(
String name,
String path,
Collection<RequestMethod> supportedMethods,
Set<MediaType> produces,
Set<MediaType> consumes,
HandlerMethod handlerMethod,
List<ResolvedMethodParameter> parameters,
ResolvedType returnType) {
String name,
String path,
Collection<RequestMethod> supportedMethods,
Set<MediaType> produces,
Set<MediaType> consumes,
HandlerMethod handlerMethod,
List<ResolvedMethodParameter> parameters,
ResolvedType returnType) {
this.name = name;
this.path = path;
this.supportedMethods = supportedMethods;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
*/
package springfox.documentation.spring.data.rest;

import java.util.Optional;
import org.springframework.data.mapping.Association;
import org.springframework.data.mapping.PersistentEntity;
import org.springframework.data.mapping.PersistentProperty;
import org.springframework.data.rest.core.mapping.ResourceMetadata;

Expand All @@ -42,7 +44,9 @@ public Association<? extends PersistentProperty<?>> getAssociation() {
return association;
}

public ResourceMetadata associationMetadata() {
return entityContext.getAssociations().getMetadataFor(entityContext.entity().getType());
public Optional<ResourceMetadata> associationMetadata() {
return entityContext.entity()
.map(PersistentEntity::getType)
.map(clazz -> entityContext.getAssociations().getMetadataFor(clazz));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,59 +18,44 @@
*/
package springfox.documentation.spring.data.rest;

import com.fasterxml.classmate.TypeResolver;
import org.springframework.data.mapping.Association;
import org.springframework.data.mapping.PersistentEntity;
import org.springframework.data.mapping.PersistentProperty;
import org.springframework.data.repository.core.RepositoryMetadata;
import org.springframework.data.rest.core.mapping.ResourceMapping;
import org.springframework.data.rest.core.mapping.ResourceMetadata;
import org.springframework.data.rest.webmvc.RestMediaTypes;
import org.springframework.web.bind.annotation.RequestMethod;
import springfox.documentation.RequestHandler;
import springfox.documentation.service.ResolvedMethodParameter;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.stream.Stream;

import static java.util.Collections.*;
import static java.util.stream.Collectors.*;
import static springfox.documentation.spring.data.rest.RequestExtractionUtils.*;
import static org.springframework.data.rest.webmvc.RestMediaTypes.SPRING_DATA_COMPACT_JSON;
import static org.springframework.data.rest.webmvc.RestMediaTypes.TEXT_URI_LIST;
import static org.springframework.web.bind.annotation.RequestMethod.*;
import springfox.documentation.spring.data.rest.SpecificationBuilder.Parameter;

public class EntityAssociationDeleteExtractor implements EntityAssociationOperationsExtractor {
@Override
public List<RequestHandler> extract(EntityAssociationContext context) {
List<RequestHandler> handlers = new ArrayList<>();
ResourceMetadata metadata = context.associationMetadata();
Association<? extends PersistentProperty<?>> association = context.getAssociation();
PersistentProperty<?> property = association.getInverse();
ResourceMapping mapping = metadata.getMappingFor(property);
EntityContext entityContext = context.getEntityContext();
PersistentEntity entity = entityContext.entity();
TypeResolver resolver = entityContext.getTypeResolver();
RepositoryMetadata repository = entityContext.getRepositoryMetadata();

ActionSpecification delete = new ActionSpecification(
String.format("%s%s",
lowerCamelCaseName(entity.getType().getSimpleName()),
upperCamelCaseName(property.getName())),
String.format("%s%s/{id}/%s",
entityContext.basePath(),
entityContext.resourcePath(),
mapping.getPath()),
singleton(RequestMethod.DELETE),
new HashSet<>(),
Stream.of(RestMediaTypes.TEXT_URI_LIST, RestMediaTypes.SPRING_DATA_COMPACT_JSON).collect(toSet()),
null,
singletonList(new ResolvedMethodParameter(
0,
"id",
pathAnnotations("id"),
resolver.resolve(repository.getIdType()))),
resolver.resolve(Void.TYPE));
handlers.add(new SpringDataRestRequestHandler(entityContext, delete));
final List<RequestHandler> handlers = new ArrayList<>();
final PersistentProperty<?> property = context.getAssociation().getInverse();

final String mappingPath = context.associationMetadata()
.map(metadata -> metadata.getMappingFor(property))
.map(mapping -> mapping.getPath())
.map(p -> p.toString())
.orElse("");

final String path = String.format("%s%s/{id}/%s",
context.getEntityContext().basePath(),
context.getEntityContext().resourcePath(),
mappingPath);

SpecificationBuilder.getInstance(context, path)
.supportsMethod(DELETE)
.consumes(TEXT_URI_LIST)
.consumes(SPRING_DATA_COMPACT_JSON)
.withParameter(Parameter.ID)
.build()
.map(delete -> new SpringDataRestRequestHandler(context.getEntityContext(), delete))
.ifPresent(handlers::add);

return handlers;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,57 +18,42 @@
*/
package springfox.documentation.spring.data.rest;

import com.fasterxml.classmate.TypeResolver;
import org.springframework.data.mapping.Association;
import org.springframework.data.mapping.PersistentEntity;
import org.springframework.data.mapping.PersistentProperty;
import org.springframework.data.repository.core.RepositoryMetadata;
import org.springframework.data.rest.core.mapping.ResourceMapping;
import org.springframework.data.rest.core.mapping.ResourceMetadata;
import org.springframework.web.bind.annotation.RequestMethod;
import springfox.documentation.RequestHandler;
import springfox.documentation.service.ResolvedMethodParameter;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

import static java.util.Collections.*;
import static org.springframework.data.rest.webmvc.RestMediaTypes.*;
import static springfox.documentation.spring.data.rest.RequestExtractionUtils.*;
import static org.springframework.web.bind.annotation.RequestMethod.GET;
import springfox.documentation.spring.data.rest.SpecificationBuilder.Parameter;

public class EntityAssociationGetExtractor implements EntityAssociationOperationsExtractor {
@Override
public List<RequestHandler> extract(EntityAssociationContext context) {
List<RequestHandler> handlers = new ArrayList<>();
ResourceMetadata metadata = context.associationMetadata();
Association<? extends PersistentProperty<?>> association = context.getAssociation();
PersistentProperty<?> property = association.getInverse();
ResourceMapping mapping = metadata.getMappingFor(property);
EntityContext entityContext = context.getEntityContext();
PersistentEntity entity = entityContext.entity();
TypeResolver resolver = entityContext.getTypeResolver();
RepositoryMetadata repository = entityContext.getRepositoryMetadata();

ActionSpecification get = new ActionSpecification(
String.format("%s%s",
lowerCamelCaseName(entity.getType().getSimpleName()),
upperCamelCaseName(property.getName())),
String.format("%s%s/{id}/%s",
entityContext.basePath(),
entityContext.resourcePath(),
mapping.getPath()),
singleton(RequestMethod.GET),
singleton(HAL_JSON),
new HashSet<>(),
null,
singletonList(new ResolvedMethodParameter(
0,
"id",
pathAnnotations("id"),
resolver.resolve(repository.getIdType()))),
propertyResponse(property, resolver));
handlers.add(new SpringDataRestRequestHandler(context.getEntityContext(), get));

final List<RequestHandler> handlers = new ArrayList<>();
final PersistentProperty<?> property = context.getAssociation().getInverse();

final String mappingPath = context.associationMetadata()
.map(metadata -> metadata.getMappingFor(property))
.map(mapping -> mapping.getPath())
.map(p -> p.toString())
.orElse("");

final String path = String.format("%s%s/{id}/%s",
context.getEntityContext().basePath(),
context.getEntityContext().resourcePath(),
mappingPath);

SpecificationBuilder.getInstance(context, path)
.supportsMethod(GET)
.produces(HAL_JSON)
.withParameter(Parameter.ID)
.build()
.map(get -> new SpringDataRestRequestHandler(context.getEntityContext(), get))
.ifPresent(handlers::add);

return handlers;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,66 +18,51 @@
*/
package springfox.documentation.spring.data.rest;

import com.fasterxml.classmate.TypeResolver;
import org.springframework.data.mapping.Association;
import org.springframework.data.mapping.PersistentEntity;
import org.springframework.data.mapping.PersistentProperty;
import org.springframework.data.repository.core.RepositoryMetadata;
import org.springframework.data.rest.core.mapping.ResourceMapping;
import org.springframework.data.rest.core.mapping.ResourceMetadata;
import org.springframework.data.rest.webmvc.RestMediaTypes;
import org.springframework.web.bind.annotation.RequestMethod;
import springfox.documentation.RequestHandler;
import springfox.documentation.service.ResolvedMethodParameter;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.stream.Stream;

import static java.util.Collections.*;
import static java.util.stream.Collectors.*;
import static org.springframework.data.rest.webmvc.RestMediaTypes.SPRING_DATA_COMPACT_JSON;
import static org.springframework.data.rest.webmvc.RestMediaTypes.TEXT_URI_LIST;
import static org.springframework.web.bind.annotation.RequestMethod.DELETE;
import static springfox.documentation.spring.data.rest.RequestExtractionUtils.*;
import static springfox.documentation.spring.data.rest.SpecificationBuilder.Parameter.ID;
import static springfox.documentation.spring.data.rest.SpecificationBuilder.Parameter.ITEM;

public class EntityAssociationItemDeleteExtractor implements EntityAssociationOperationsExtractor {
@Override
public List<RequestHandler> extract(EntityAssociationContext context) {
List<RequestHandler> handlers = new ArrayList<>();
ResourceMetadata metadata = context.associationMetadata();
Association<? extends PersistentProperty<?>> association = context.getAssociation();
PersistentProperty<?> property = association.getInverse();
ResourceMapping mapping = metadata.getMappingFor(property);
EntityContext entityContext = context.getEntityContext();
PersistentEntity entity = entityContext.entity();
TypeResolver resolver = entityContext.getTypeResolver();
RepositoryMetadata repository = entityContext.getRepositoryMetadata();

final List<RequestHandler> handlers = new ArrayList<>();
final PersistentProperty<?> property = context.getAssociation().getInverse();
final String propertyIdentifier = propertyIdentifierName(property);

final String mappingPath = context.associationMetadata()
.map(metadata -> metadata.getMappingFor(property))
.map(mapping -> mapping.getPath())
.map(p -> p.toString())
.orElse("");

final String path = String.format("%s%s/{id}/%s/{%s}",
context.getEntityContext().basePath(),
context.getEntityContext().resourcePath(),
mappingPath,
propertyIdentifier);

if (property.isMap() || property.isCollectionLike()) {
String propertyIdentifier = propertyIdentifierName(property);
ActionSpecification deleteItem = new ActionSpecification(
String.format("%s%s",
lowerCamelCaseName(entity.getType().getSimpleName()),
upperCamelCaseName(property.getName())),
String.format("%s%s/{id}/%s/{%s}",
entityContext.basePath(),
entityContext.resourcePath(),
mapping.getPath(),
propertyIdentifier),
singleton(RequestMethod.DELETE),
new HashSet<>(),
Stream.of(RestMediaTypes.TEXT_URI_LIST, RestMediaTypes.SPRING_DATA_COMPACT_JSON).collect(toSet()),
null,
Stream.of(new ResolvedMethodParameter(
0,
"id",
pathAnnotations("id"),
resolver.resolve(repository.getIdType())),
new ResolvedMethodParameter(
1,
propertyIdentifier,
pathAnnotations(propertyIdentifier),
resolver.resolve(String.class))).collect(toList()),
resolver.resolve(Void.TYPE));
handlers.add(new SpringDataRestRequestHandler(entityContext, deleteItem));

SpecificationBuilder.getInstance(context, path)
.supportsMethod(DELETE)
.consumes(TEXT_URI_LIST)
.consumes(SPRING_DATA_COMPACT_JSON)
.withParameter(ID)
.withParameter(ITEM)
.build()
.map(deleteItem -> new SpringDataRestRequestHandler(context.getEntityContext(), deleteItem))
.ifPresent(handlers::add);

}
return handlers;
}
Expand Down