Skip to content

Upgrading to Grails 3.3.x #22

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@ plugins {
}

allprojects {
apply plugin: 'com.github.ben-manes.versions'

group = 'io.springfox.grails'
version = '1.0.1-SNAPSHOT'

repositories {
jcenter()
maven { url 'http://oss.jfrog.org/artifactory/oss-snapshot-local/' }
mavenCentral()
maven { url "https://repo.grails.org/grails/core" }
// maven { url 'http://oss.jfrog.org/artifactory/oss-snapshot-local/' }
}

ext {
Expand All @@ -20,7 +22,7 @@ allprojects {
classmate = "1.3.4"
groovy = "2.4.8"
guava = "20.0"
grails = "3.2.4"
grails = "3.3.2"
jackson = '2.7.7'
joda = "2.9.4"
jsonPath = "2.4.0"
Expand All @@ -31,7 +33,7 @@ allprojects {
slf4j = "1.7.22"
snakeyaml = '1.19'
spock = "1.1-groovy-2.4"
spring = "4.2.8.RELEASE"
spring = "4.2.9.RELEASE"
springHateoas = "0.21.0.RELEASE"
springPluginVersion = "1.2.0.RELEASE"
swagger2Core = "1.5.12"
Expand Down
5 changes: 3 additions & 2 deletions springfox-grails-contract-tests/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ buildscript {
}
dependencies {
classpath "org.grails:grails-gradle-plugin:$grailsVersion"
classpath "org.grails.plugins:hibernate5:6.0.2"
classpath "org.grails.plugins:views-gradle:1.1.1"
classpath "org.grails.plugins:hibernate5:6.1.8"
classpath "org.grails.plugins:views-gradle:1.2.6"
}
}

Expand Down Expand Up @@ -66,6 +66,7 @@ dependencies {
runtime "com.h2database:h2"

testCompile "org.grails:grails-plugin-testing"
testCompile "org.grails:grails-web-testing-support"
testCompile "org.grails.plugins:geb"
testCompile "org.grails:grails-datastore-rest-client"
testCompile 'io.rest-assured:rest-assured:3.0.6'
Expand Down
2 changes: 1 addition & 1 deletion springfox-grails-contract-tests/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
grailsVersion=3.2.4
grailsVersion=3.3.2
gradleWrapperVersion=3.0
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package grails.springfox.sample

import grails.test.mixin.integration.Integration
import grails.testing.mixin.integration.Integration
import groovy.json.JsonOutput
import org.junit.Assert
import org.skyscreamer.jsonassert.JSONAssert
Expand Down
5 changes: 3 additions & 2 deletions springfox-grails/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ buildscript {
maven {
url "https://plugins.gradle.org/m2/"
}
maven { url "https://repo.grails.org/grails/core" }
jcenter()
mavenCentral()
}

}

plugins {
Expand Down Expand Up @@ -49,8 +49,9 @@ dependencies {
testCompile "org.spockframework:spock-spring:${spock}"
testCompile "org.spockframework:spock-core:${spock}"
testCompile "org.codehaus.groovy:groovy-all:${groovy}"
testCompile "org.grails:grails-plugin-testing:${grails}"
testCompile "org.grails:grails-web-testing-support:1.1.3"
testCompile "org.grails:grails-plugin-rest:${grails}"
testCompile "org.grails:grails-gsp:3.3.0"

provided "io.swagger:swagger-annotations:${swagger2Core}"
provided "org.springframework:spring-core:$spring"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package springfox.documentation.grails;

import grails.core.GrailsDomainClass;
import org.grails.datastore.mapping.model.PersistentEntity;


@FunctionalInterface
public interface ActionSpecificationFactory {
default Class<?> idType(GrailsDomainClass domain) {
return domain != null ? domain.getIdentifier().getType() : Void.TYPE;
default Class<?> idType(PersistentEntity domain) {
return domain != null ? domain.getIdentity().getType() : Void.TYPE;
}

default Class domainClass(GrailsDomainClass domain) {
default Class domainClass(PersistentEntity domain) {
if (domain != null) {
return domain.getClazz();
return domain.getJavaClass();
}
return Void.TYPE;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,10 @@ public DefaultGrailsAlternateTypeRuleConvention(

@Override
public List<AlternateTypeRule> rules() {
return Arrays.stream(application.getArtefacts("Domain"))
.filter(GrailsDomainClass.class::isInstance)
return application.getMappingContext().getPersistentEntities().stream()
.map(domain -> newRule(
domain.getClazz(),
resolver.resolve(typeGenerator.from((GrailsDomainClass) domain)), getOrder()))
domain.getJavaClass(),
resolver.resolve(typeGenerator.from(domain)), getOrder()))
.collect(Collectors.toList());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package springfox.documentation.grails;

import com.google.common.base.Objects;
import grails.core.GrailsDomainClassProperty;
import org.grails.datastore.mapping.model.PersistentProperty;

public class DefaultGrailsPropertySelector implements GrailsPropertySelector {
@Override
public boolean test(GrailsDomainClassProperty each) {
return each.getReferencedDomainClass() == null
public boolean test(PersistentProperty each) {
return each.getOwner().getAssociations().stream().noneMatch(a -> a.getName().equals(each.getName()))
&& !Objects.equal(each.getName(), "version");
}
}
Original file line number Diff line number Diff line change
@@ -1,29 +1,24 @@
package springfox.documentation.grails;

import grails.core.GrailsDomainClassProperty;
import org.grails.datastore.mapping.model.PersistentProperty;
import springfox.documentation.builders.AlternateTypePropertyBuilder;

public class DefaultGrailsPropertyTransformer implements GrailsPropertyTransformer {
@Override
public AlternateTypePropertyBuilder apply(GrailsDomainClassProperty property) {
Class type = property.getReferencedPropertyType();
if (!property.isPersistent() && property.getName().endsWith("Id")) {
type = relatedDomainIdentifierType(relatedDomainProperty(property));
}

public AlternateTypePropertyBuilder apply(PersistentProperty property) {
return new AlternateTypePropertyBuilder()
.withName(property.getName())
.withType(type)
.withType(property.getType())
.withCanRead(true)
.withCanWrite(true);
}

private Class relatedDomainIdentifierType(GrailsDomainClassProperty property) {
return property.getDomainClass().getIdentifier().getType();
private Class relatedDomainIdentifierType(PersistentProperty property) {
return property.getOwner().getIdentity().getType();
}

private GrailsDomainClassProperty relatedDomainProperty(GrailsDomainClassProperty property) {
private PersistentProperty relatedDomainProperty(PersistentProperty property) {
String entityPropertyName = property.getName().replace("Id", "");
return property.getDomainClass().getPropertyByName(entityPropertyName);
return property.getOwner().getPropertyByName(entityPropertyName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import com.fasterxml.classmate.TypeResolver;
import com.google.common.collect.ImmutableSet;
import grails.core.GrailsControllerClass;
import grails.core.GrailsDomainClass;
import grails.web.mapping.UrlMapping;
import org.grails.datastore.mapping.model.PersistentEntity;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.method.HandlerMethod;
Expand All @@ -22,7 +22,7 @@

class GrailsActionContext {
private final GrailsControllerClass controller;
private final GrailsDomainClass domainClass;
private final PersistentEntity domainClass;
private final GrailsActionAttributes urlProvider;
private final String action;
private final TypeResolver resolver;
Expand All @@ -34,7 +34,7 @@ class GrailsActionContext {

public GrailsActionContext(
GrailsControllerClass controller,
GrailsDomainClass domainClass,
PersistentEntity domainClass,
GrailsActionAttributes urlProvider,
String action,
TypeResolver resolver) {
Expand Down Expand Up @@ -70,7 +70,7 @@ public GrailsControllerClass getController() {
return controller;
}

public GrailsDomainClass getDomainClass() {
public PersistentEntity getDomainClass() {
return domainClass;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package springfox.documentation.grails;

import grails.core.GrailsDomainClassProperty;
import org.grails.datastore.mapping.model.PersistentProperty;

import java.util.function.Predicate;

public interface GrailsPropertySelector extends Predicate<GrailsDomainClassProperty> {
public interface GrailsPropertySelector extends Predicate<PersistentProperty> {
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package springfox.documentation.grails;

import grails.core.GrailsDomainClassProperty;
import org.grails.datastore.mapping.model.PersistentProperty;
import springfox.documentation.builders.AlternateTypePropertyBuilder;

import java.util.function.Function;

public interface GrailsPropertyTransformer
extends Function<GrailsDomainClassProperty, AlternateTypePropertyBuilder> {
extends Function<PersistentProperty, AlternateTypePropertyBuilder> {
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public String getName() {
.map(domain -> String.format(
"%s%s",
actionContext.getAction(),
capitalize(domain.getLogicalPropertyName())))
capitalize(domain.getName())))
.orElse(actionContext.getAction());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
import grails.core.GrailsApplication;
import grails.core.GrailsClass;
import grails.core.GrailsControllerClass;
import grails.core.GrailsDomainClass;
import org.grails.datastore.mapping.model.MappingContext;
import org.grails.datastore.mapping.model.PersistentEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import springfox.documentation.RequestHandler;
Expand Down Expand Up @@ -36,6 +37,7 @@ public GrailsRequestHandlerProvider(
this.urlProvider = urlProvider;
this.grailsApplication = grailsApplication;
this.actionResolver = actionResolver;
grailsApplication.initialise();
}

@Override
Expand All @@ -46,8 +48,9 @@ public List<RequestHandler> requestHandlers() {
}

private Stream<? extends RequestHandler> fromGrailsAction(GrailsClass grailsClass) {
GrailsDomainClass inferredDomain = (GrailsDomainClass) Arrays.stream(grailsApplication.getArtefacts("Domain"))
.filter(d -> Objects.equals(d.getLogicalPropertyName(), grailsClass.getLogicalPropertyName()))
PersistentEntity inferredDomain = grailsApplication.getMappingContext()
.getPersistentEntities().stream()
.filter(d -> Objects.equals(d.getName(), grailsClass.getLogicalPropertyName()))
.findFirst()
.orElse(null);
GrailsControllerClass controller = (GrailsControllerClass) grailsClass;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package springfox.documentation.grails;

import grails.core.GrailsDomainClass;
import org.grails.datastore.mapping.model.PersistentEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import springfox.documentation.builders.AlternateTypeBuilder;
import springfox.documentation.builders.AlternateTypePropertyBuilder;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

Expand All @@ -27,14 +26,14 @@ public GrailsSerializationTypeGenerator(
this.naming = naming;
}

public Class<?> from(GrailsDomainClass domain) {
public Class<?> from(PersistentEntity domain) {
List<AlternateTypePropertyBuilder> properties =
Arrays.stream(domain.getProperties())
domain.getPersistentProperties().stream()
.filter(propertySelector)
.map(propertyTransformer)
.collect(Collectors.toList());
return new AlternateTypeBuilder()
.fullyQualifiedClassName(naming.name(domain.getClazz()))
.fullyQualifiedClassName(naming.name(domain.getJavaClass()))
.withProperties(properties)
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
import com.fasterxml.classmate.ResolvedType;
import com.fasterxml.classmate.TypeResolver;
import com.google.common.base.Strings;
import grails.core.GrailsDomainClass;
import grails.validation.ConstrainedProperty;
import grails.gorm.validation.Constrained;
import grails.gorm.validation.ConstrainedProperty;
import grails.web.mapping.UrlMapping;
import org.grails.datastore.mapping.model.PersistentEntity;
import springfox.documentation.service.ResolvedMethodParameter;

import java.util.Arrays;
Expand All @@ -18,7 +19,7 @@
import java.util.stream.IntStream;

import static com.google.common.collect.Lists.*;
import static springfox.documentation.grails.Parameters.pathParameter;
import static springfox.documentation.grails.Parameters.*;

class UrlMappings {

Expand All @@ -36,10 +37,12 @@ && controllerMatches(u, logicalControllerName)
}

public static Map<String, String> pathParameters(UrlMapping mapping) {
ConstrainedProperty[] constraints = mapping.getConstraints();
Constrained[] constraints = mapping.getConstraints();
return IntStream.range(0, constraints.length)
.filter(indicesToUse(mapping))
.mapToObj(i -> constraints[i])
.filter(c -> c instanceof ConstrainedProperty)
.map(ConstrainedProperty.class::cast)
.collect(Collectors.toMap(
ConstrainedProperty::getPropertyName,
c -> String.format("{%s}", c.getPropertyName())));
Expand All @@ -48,11 +51,13 @@ public static Map<String, String> pathParameters(UrlMapping mapping) {
public static List<ResolvedMethodParameter> resolvedPathParameters(
TypeResolver resolver,
UrlMapping mapping,
GrailsDomainClass domainClass) {
ConstrainedProperty[] constraints = mapping.getConstraints();
PersistentEntity domainClass) {
Constrained[] constraints = mapping.getConstraints();
List<ConstrainedProperty> pathProperties = IntStream.range(0, constraints.length)
.filter(indicesToUse(mapping))
.mapToObj(i -> constraints[i])
.filter(c -> c instanceof ConstrainedProperty)
.map(ConstrainedProperty.class::cast)
.collect(Collectors.toList());
List<ResolvedMethodParameter> resolved = newArrayList();
for (int index = 0; index < pathProperties.size(); index++) {
Expand All @@ -68,18 +73,19 @@ public static List<ResolvedMethodParameter> resolvedPathParameters(

private static IntPredicate indicesToUse(UrlMapping mapping) {
return index -> {
ConstrainedProperty property = mapping.getConstraints()[index];
return !property.getPropertyName().equals("controller")
&& !property.getPropertyName().equals("action")
Constrained property = mapping.getConstraints()[index];
return property instanceof ConstrainedProperty
&& !((ConstrainedProperty)property).getPropertyName().equals("controller")
&& !((ConstrainedProperty)property).getPropertyName().equals("action")
&& !property.isNullable();
};
}

private static ResolvedType resolvedPropertyType(
TypeResolver resolver,
GrailsDomainClass domainClass,
PersistentEntity domainClass,
ConstrainedProperty property) {
if (domainClass.hasProperty(property.getPropertyName())) {
if (domainClass.hasProperty(property.getPropertyName(), domainClass.getJavaClass())) {
return resolver.resolve(domainClass.getPropertyByName(property.getPropertyName()).getType());
}
return resolver.resolve(String.class);
Expand Down Expand Up @@ -121,6 +127,8 @@ private static boolean isWildcardAction(UrlMapping urlMapping) {

private static boolean hasControllerConstraint(UrlMapping urlMapping, String name) {
return !Arrays.stream(urlMapping.getConstraints())
.filter(c -> c instanceof ConstrainedProperty)
.map(ConstrainedProperty.class::cast)
.filter(c -> c.getPropertyName().equals(name))
.collect(Collectors.toList()).isEmpty();
}
Expand Down
Loading