Skip to content

Commit

Permalink
fix: Evaluate @Property correctly.
Browse files Browse the repository at this point in the history
Fixes #1271.
  • Loading branch information
michael-simons committed Mar 22, 2024
1 parent 9299ca9 commit 6297f1c
Show file tree
Hide file tree
Showing 29 changed files with 107 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<parent>
<groupId>eu.michael-simons.neo4j</groupId>
<artifactId>neo4j-migrations-annotation-processing</artifactId>
<version>2.9.4-SNAPSHOT</version>
<version>2.10.0-SNAPSHOT</version>
</parent>

<artifactId>neo4j-migrations-annotation-catalog</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion extensions/neo4j-migrations-annotation-processing/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<parent>
<groupId>eu.michael-simons.neo4j</groupId>
<artifactId>neo4j-migrations-parent</artifactId>
<version>2.9.4-SNAPSHOT</version>
<version>2.10.0-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<parent>
<groupId>eu.michael-simons.neo4j</groupId>
<artifactId>neo4j-migrations-annotation-processing</artifactId>
<version>2.9.4-SNAPSHOT</version>
<version>2.10.0-SNAPSHOT</version>
</parent>

<artifactId>neo4j-migrations-annotation-processor-api</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<parent>
<groupId>eu.michael-simons.neo4j</groupId>
<artifactId>neo4j-migrations-annotation-processing</artifactId>
<version>2.9.4-SNAPSHOT</version>
<version>2.10.0-SNAPSHOT</version>
</parent>

<artifactId>neo4j-migrations-annotation-processor</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ private void processOGMPropertyTypes(RoundEnvironment roundEnv) {
.forEach(t -> {
var labels = computeLabelsOGM(t);
var owner = new DefaultNodeType(t.getQualifiedName().toString(), labels);
catalogItems.addAll(t.accept(new PropertyTypeConstraintGenerator<>(labels), owner));
catalogItems.addAll(t.accept(new PropertyTypeConstraintGenerator<>(Mode.OGM, labels), owner));
});

roundEnv.getElementsAnnotatedWith(ogm.relationshipEntity())
Expand All @@ -297,7 +297,7 @@ private void processOGMPropertyTypes(RoundEnvironment roundEnv) {
.forEach(t -> {
var type = computeTypeOGM(t);
var owner = new DefaultRelationshipType(t.getQualifiedName().toString(), type);
catalogItems.addAll(t.accept(new PropertyTypeConstraintGenerator<>(List.of(type)), owner));
catalogItems.addAll(t.accept(new PropertyTypeConstraintGenerator<>(Mode.OGM, List.of(type)), owner));
});
}

Expand Down Expand Up @@ -753,25 +753,28 @@ private void processSDN6IdAnnotations(RoundEnvironment roundEnv) {
List<SchemaName> labels = computeLabelsSDN6(t);
DefaultNodeType owner = new DefaultNodeType(t.getQualifiedName().toString(), labels);
if (requiresPrimaryKeyConstraintSDN6(t)) {
PropertyType<NodeType> idProperty = t.accept(new PropertySelector(supportedSDN6Annotations),
PropertyType<NodeType> idProperty = t.accept(new IdPropertySelector(Mode.SDN6, supportedSDN6Annotations),
owner);
String name = this.constraintNameGenerator.generateName(Constraint.Type.UNIQUE,
Collections.singleton(idProperty));
catalogItems.add(Constraint.forNode(labels.get(0).getValue()).named(name)
.unique(idProperty.getName()));
}
if (generateTypeConstraints) {
catalogItems.addAll(t.accept(new PropertyTypeConstraintGenerator<>(labels), owner));
catalogItems.addAll(t.accept(new PropertyTypeConstraintGenerator<>(Mode.SDN6, labels), owner));
}
});
}

@SuppressWarnings("squid:S110") // Not something we need or can do anything about (Number of parents)
class PropertyTypeConstraintGenerator<E extends ElementType<E>> extends ElementKindVisitor8<List<CatalogItem<?>>, WriteableElementType<E>> {

private final Mode mode;

private final List<SchemaName> labels;

PropertyTypeConstraintGenerator(List<SchemaName> labels) {
PropertyTypeConstraintGenerator(Mode mode, List<SchemaName> labels) {
this.mode = mode;
this.labels = labels;
}

Expand Down Expand Up @@ -803,7 +806,12 @@ List<CatalogItem<?>> checkIfEligibleForPropertyTypeConstraint(Element e, Writeab
return List.of();
}

var property = owner.addProperty(e.getSimpleName().toString());
Optional<String> additionalName = switch (mode) {
case SDN6 -> extractPropertyName(e, sdn6.property());
case OGM -> extractPropertyName(e, ogm.property());
case PURE -> Optional.empty();
};
var property = owner.addProperty(additionalName.orElseGet(() -> e.getSimpleName().toString()));
var name = constraintNameGenerator.generateName(Constraint.Type.PROPERTY_TYPE, List.of(property));
return List.of(Constraint.forNode(labels.get(0).getValue()).named(name).type(property.getName(), TypesEligibleForPropertyTypeConstraints.get(type)));
}
Expand All @@ -822,7 +830,7 @@ private void processOGMIdAnnotations(RoundEnvironment roundEnv) {
.map(TypeElement.class::cast)
.forEach(t -> {
List<SchemaName> labels = computeLabelsOGM(t);
PropertyType<NodeType> idProperty = t.accept(new PropertySelector(Collections.singleton(ogm.id())),
PropertyType<NodeType> idProperty = t.accept(new IdPropertySelector(Mode.OGM, Collections.singleton(ogm.id())),
new DefaultNodeType(t.getQualifiedName().toString(), labels));
String name = this.constraintNameGenerator.generateName(Constraint.Type.UNIQUE,
Collections.singleton(idProperty));
Expand Down Expand Up @@ -1130,11 +1138,14 @@ List<CatalogItem<?>> handleRelationship(PropertyType<E> property, boolean isRequ
}

@SuppressWarnings("squid:S110") // Not something we need or can do anything about (Number of parents)
class PropertySelector extends ElementKindVisitor8<PropertyType<NodeType>, DefaultNodeType> {
class IdPropertySelector extends ElementKindVisitor8<PropertyType<NodeType>, DefaultNodeType> {

private final Mode mode;

private final Set<Element> requiredAnnotations;

PropertySelector(Set<Element> requiredAnnotations) {
IdPropertySelector(Mode mode, Set<Element> requiredAnnotations) {
this.mode = mode;
this.requiredAnnotations = requiredAnnotations;
}

Expand Down Expand Up @@ -1165,7 +1176,18 @@ public PropertyType<NodeType> visitVariableAsField(VariableElement e, DefaultNod
.map(AnnotationMirror::getAnnotationType)
.map(DeclaredType::asElement)
.anyMatch(requiredAnnotations::contains);
return requiredAnnotationPresent ? owner.addProperty(e.getSimpleName().toString()) : null;

if (!requiredAnnotationPresent) {
return null;
}

Optional<String> additionalName = switch (mode) {
case SDN6 -> extractPropertyName(e, sdn6.property());
case OGM -> extractPropertyName(e, ogm.property());
case PURE -> Optional.empty();
};

return owner.addProperty(additionalName.orElseGet(() -> e.getSimpleName().toString()));
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright 2020-2024 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 ac.simons.neo4j.migrations.annotations.proc.sdn6.movies;

import org.springframework.data.neo4j.core.schema.Id;
import org.springframework.data.neo4j.core.schema.Node;
import org.springframework.data.neo4j.core.schema.Property;

/**
* Class for testing renamed id properties.
*/
@Node
public class RenamedIdProperty {
@Id
@Property("myOtherIdName")
private String myId;
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
<property>title</property>
</properties>
</constraint>
<constraint name="ac_simons_neo4j_migrations_annotations_proc_sdn6_movies_renamedidproperty_myOtherIdName_unique" type="unique">
<label>RenamedIdProperty</label>
<properties>
<property>myOtherIdName</property>
</properties>
</constraint>
</constraints>
</catalog>
<apply/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
<property type="STRING">title</property>
</properties>
</constraint>
<constraint name="ac_simons_neo4j_migrations_annotations_proc_sdn6_movies_movie_description_property_type" type="property_type">
<constraint name="ac_simons_neo4j_migrations_annotations_proc_sdn6_movies_movie_tagline_property_type" type="property_type">
<label>Movie</label>
<properties>
<property type="STRING">description</property>
<property type="STRING">tagline</property>
</properties>
</constraint>
<constraint name="ac_simons_neo4j_migrations_annotations_proc_sdn6_movies_movie_released_property_type" type="property_type">
Expand Down Expand Up @@ -58,6 +58,18 @@
<property type="INTEGER">born</property>
</properties>
</constraint>
<constraint name="ac_simons_neo4j_migrations_annotations_proc_sdn6_movies_renamedidproperty_myOtherIdName_unique" type="unique">
<label>RenamedIdProperty</label>
<properties>
<property>myOtherIdName</property>
</properties>
</constraint>
<constraint name="ac_simons_neo4j_migrations_annotations_proc_sdn6_movies_renamedidproperty_myOtherIdName_property_type" type="property_type">
<label>RenamedIdProperty</label>
<properties>
<property type="STRING">myOtherIdName</property>
</properties>
</constraint>
</constraints>
</catalog>
<apply/>
Expand Down
2 changes: 1 addition & 1 deletion extensions/neo4j-migrations-formats-adoc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<parent>
<groupId>eu.michael-simons.neo4j</groupId>
<artifactId>neo4j-migrations-parent</artifactId>
<version>2.9.4-SNAPSHOT</version>
<version>2.10.0-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion extensions/neo4j-migrations-formats-csv/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<parent>
<groupId>eu.michael-simons.neo4j</groupId>
<artifactId>neo4j-migrations-parent</artifactId>
<version>2.9.4-SNAPSHOT</version>
<version>2.10.0-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion extensions/neo4j-migrations-formats-markdown/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<parent>
<groupId>eu.michael-simons.neo4j</groupId>
<artifactId>neo4j-migrations-parent</artifactId>
<version>2.9.4-SNAPSHOT</version>
<version>2.10.0-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion neo4j-migrations-bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<parent>
<groupId>eu.michael-simons.neo4j</groupId>
<artifactId>neo4j-migrations-parent</artifactId>
<version>2.9.4-SNAPSHOT</version>
<version>2.10.0-SNAPSHOT</version>
</parent>

<artifactId>neo4j-migrations-bom</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion neo4j-migrations-cli/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<parent>
<groupId>eu.michael-simons.neo4j</groupId>
<artifactId>neo4j-migrations-parent</artifactId>
<version>2.9.4-SNAPSHOT</version>
<version>2.10.0-SNAPSHOT</version>
</parent>

<artifactId>neo4j-migrations-cli</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion neo4j-migrations-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<parent>
<groupId>eu.michael-simons.neo4j</groupId>
<artifactId>neo4j-migrations-parent</artifactId>
<version>2.9.4-SNAPSHOT</version>
<version>2.10.0-SNAPSHOT</version>
</parent>

<artifactId>neo4j-migrations</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<parent>
<groupId>eu.michael-simons.neo4j</groupId>
<artifactId>neo4j-migrations-examples</artifactId>
<version>2.9.4-SNAPSHOT</version>
<version>2.10.0-SNAPSHOT</version>
</parent>

<artifactId>neo4j-migrations-cluster-tests</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<parent>
<groupId>eu.michael-simons.neo4j</groupId>
<artifactId>neo4j-migrations-examples</artifactId>
<version>2.9.4-SNAPSHOT</version>
<version>2.10.0-SNAPSHOT</version>
</parent>

<artifactId>neo4j-migrations-examples-sb-testharness</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<parent>
<groupId>eu.michael-simons.neo4j</groupId>
<artifactId>neo4j-migrations-examples</artifactId>
<version>2.9.4-SNAPSHOT</version>
<version>2.10.0-SNAPSHOT</version>
</parent>

<artifactId>neo4j-migrations-examples-sb</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion neo4j-migrations-examples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<parent>
<groupId>eu.michael-simons.neo4j</groupId>
<artifactId>neo4j-migrations-parent</artifactId>
<version>2.9.4-SNAPSHOT</version>
<version>2.10.0-SNAPSHOT</version>
</parent>

<artifactId>neo4j-migrations-examples</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion neo4j-migrations-maven-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<parent>
<groupId>eu.michael-simons.neo4j</groupId>
<artifactId>neo4j-migrations-parent</artifactId>
<version>2.9.4-SNAPSHOT</version>
<version>2.10.0-SNAPSHOT</version>
</parent>

<artifactId>neo4j-migrations-maven-plugin</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion neo4j-migrations-quarkus-parent/deployment/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<parent>
<groupId>eu.michael-simons.neo4j</groupId>
<artifactId>neo4j-migrations-quarkus-parent</artifactId>
<version>2.9.4-SNAPSHOT</version>
<version>2.10.0-SNAPSHOT</version>
</parent>

<artifactId>neo4j-migrations-quarkus-deployment</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion neo4j-migrations-quarkus-parent/integration-tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<parent>
<groupId>eu.michael-simons.neo4j</groupId>
<artifactId>neo4j-migrations-quarkus-parent</artifactId>
<version>2.9.4-SNAPSHOT</version>
<version>2.10.0-SNAPSHOT</version>
</parent>

<artifactId>neo4j-migrations-quarkus-integration-tests</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion neo4j-migrations-quarkus-parent/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<parent>
<groupId>eu.michael-simons.neo4j</groupId>
<artifactId>neo4j-migrations-parent</artifactId>
<version>2.9.4-SNAPSHOT</version>
<version>2.10.0-SNAPSHOT</version>
</parent>

<artifactId>neo4j-migrations-quarkus-parent</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion neo4j-migrations-quarkus-parent/runtime/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<parent>
<groupId>eu.michael-simons.neo4j</groupId>
<artifactId>neo4j-migrations-quarkus-parent</artifactId>
<version>2.9.4-SNAPSHOT</version>
<version>2.10.0-SNAPSHOT</version>
</parent>

<artifactId>neo4j-migrations-quarkus</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<parent>
<groupId>eu.michael-simons.neo4j</groupId>
<artifactId>neo4j-migrations-spring-boot-starter-parent</artifactId>
<version>2.9.4-SNAPSHOT</version>
<version>2.10.0-SNAPSHOT</version>
</parent>

<artifactId>neo4j-migrations-spring-boot-autoconfigure</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<parent>
<groupId>eu.michael-simons.neo4j</groupId>
<artifactId>neo4j-migrations-spring-boot-starter-parent</artifactId>
<version>2.9.4-SNAPSHOT</version>
<version>2.10.0-SNAPSHOT</version>
</parent>

<artifactId>neo4j-migrations-spring-boot-starter</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion neo4j-migrations-spring-boot-starter-parent/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<parent>
<groupId>eu.michael-simons.neo4j</groupId>
<artifactId>neo4j-migrations-parent</artifactId>
<version>2.9.4-SNAPSHOT</version>
<version>2.10.0-SNAPSHOT</version>
</parent>

<artifactId>neo4j-migrations-spring-boot-starter-parent</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion neo4j-migrations-test-resources/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<parent>
<groupId>eu.michael-simons.neo4j</groupId>
<artifactId>neo4j-migrations-parent</artifactId>
<version>2.9.4-SNAPSHOT</version>
<version>2.10.0-SNAPSHOT</version>
</parent>

<artifactId>neo4j-migrations-test-resources</artifactId>
Expand Down
Loading

0 comments on commit 6297f1c

Please sign in to comment.