Skip to content

Commit

Permalink
Make sure to not create adapted types in the schema
Browse files Browse the repository at this point in the history
Signed-off-by: Phillip Kruger <[email protected]>
  • Loading branch information
phillip-kruger authored and jmartisk committed Apr 8, 2022
1 parent dda8b5a commit b655a16
Show file tree
Hide file tree
Showing 13 changed files with 969 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,14 @@ public Set<DotName> getAnnotationNames() {
return annotationsMap.keySet();
}

public Annotations removeAnnotations(DotName... annotations) {
Map<DotName, AnnotationInstance> newAnnotationsMap = new HashMap<>(annotationsMap);
for (DotName annotation : annotations) {
newAnnotationsMap.remove(annotation);
}
return new Annotations(newAnnotationsMap, this.parentAnnotations);
}

/**
* Get a specific annotation
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,23 +247,19 @@ public Reference createReference(Direction direction, ClassInfo classInfo, boole
Reference reference = new Reference(className, name, referenceType, parametrizedTypeArgumentsReferences,
addParametrizedTypeNameExtension);

// Adapt to Scalar
boolean shouldCreateAdapedToType = AdaptToHelper.shouldCreateTypeInSchema(annotationsForClass);
// Adaptation
Optional<AdaptTo> adaptTo = AdaptToHelper.getAdaptTo(reference, annotationsForClass);
reference.setAdaptTo(adaptTo.orElse(null));

// Now add it to the correct map
if (shouldCreateAdapedToType && createAdapedToType) {
putIfAbsent(name, reference, referenceType);
}

// Adapt with adapter
boolean shouldCreateAdapedWithType = AdaptWithHelper.shouldCreateTypeInSchema(annotationsForClass);
Optional<AdaptWith> adaptWith = AdaptWithHelper.getAdaptWith(direction, this, reference, annotationsForClass);
reference.setAdaptWith(adaptWith.orElse(null));

// Now add it to the correct map
if (shouldCreateAdapedWithType && createAdapedWithType) {
boolean shouldCreateAdapedToType = AdaptToHelper.shouldCreateTypeInSchema(annotationsForClass);
boolean shouldCreateAdapedWithType = AdaptWithHelper.shouldCreateTypeInSchema(annotationsForClass);

// We ignore the field that is being adapted
if (shouldCreateAdapedToType && createAdapedToType && shouldCreateAdapedWithType && createAdapedWithType) {
putIfAbsent(name, reference, referenceType);
}
return reference;
Expand Down Expand Up @@ -298,7 +294,7 @@ private Reference getReference(Direction direction,
Annotations annotations,
Reference parentObjectReference) {

// In some case, like operations and interfaces, there is not fieldType
// In some case, like operations and interfaces, there is no fieldType
if (fieldType == null) {
fieldType = methodType;
}
Expand All @@ -315,7 +311,7 @@ private Reference getReference(Direction direction,
}
return Scalars.getScalar(fieldTypeName);
} else if (fieldType.kind().equals(Type.Kind.ARRAY)) {
// java Array
// Java Array
Type typeInArray = fieldType.asArrayType().component();
Type typeInMethodArray = methodType.asArrayType().component();
return getReference(direction, typeInArray, typeInMethodArray, annotations, parentObjectReference);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,12 @@ public static Optional<AdaptWith> getAdaptWith(Direction direction, ReferenceCre
if (Scalars.isScalar(to.name().toString())) {
adaptWith.setToReference(Scalars.getScalar(to.name().toString()));
} else {
Reference toRef = referenceCreator.createReferenceForAdapter(direction, to, annotations);
Annotations annotationsAplicableToMe = annotations.removeAnnotations(Annotations.ADAPT_WITH,
Annotations.JSONB_TYPE_ADAPTER);

// Remove the adaption annotation, as this is the type being adapted to
Reference toRef = referenceCreator.createReferenceForAdapter(direction, to,
annotationsAplicableToMe);
toRef.setWrapper(WrapperCreator.createWrapper(to).orElse(null));

adaptWith.setToReference(toRef);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@ private Bootstrap(Schema schema, boolean skipInjectionValidation) {
*/
private void verifyInjectionIsAvailable() {
LookupService lookupService = LookupService.get();
ClassloadingService classloadingService = ClassloadingService.get();
// This crazy stream operation basically collects all class names where we need to verify that
// it belongs to an injectable bean
Stream.of(
Expand Down
2 changes: 1 addition & 1 deletion server/runner/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@
<plugin>
<groupId>org.wildfly.plugins</groupId>
<artifactId>wildfly-maven-plugin</artifactId>
<version>3.0.0.Final</version>
<version>2.1.0.Final</version>
<configuration>
<version>${version.wildfly}</version>
<server-config>standalone-microprofile.xml</server-config>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.smallrye.graphql.test.apps.adapt.to.api;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.eclipse.microprofile.graphql.GraphQLApi;
Expand Down Expand Up @@ -47,6 +48,19 @@ public AdaptToData updateAdaptToData(AdaptToData adaptToData) {
return adaptToData;
}

@Mutation
public Dummy addDummy(Dummy dummy) {
return dummy;
}

@Query
public Dummy getDummy() {
Dummy d = new Dummy();
d.id = new DummyId(new Date());
d.name = "foo";
return d;
}

public static class AdaptToData {
public Long id;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package io.smallrye.graphql.test.apps.adapt.to.api;

import io.smallrye.graphql.api.AdaptToScalar;
import io.smallrye.graphql.api.Scalar;

public class Dummy {

public String name;
@AdaptToScalar(Scalar.String.class)
public DummyId id;

}
Loading

0 comments on commit b655a16

Please sign in to comment.