Skip to content

Commit

Permalink
Merge pull request smallrye#1395 from phillip-kruger/main
Browse files Browse the repository at this point in the history
Sync with 1.5.x
  • Loading branch information
phillip-kruger authored May 13, 2022
2 parents 46f2689 + c88e65d commit 1d83c1d
Show file tree
Hide file tree
Showing 13 changed files with 295 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ public DirectiveType create(ClassInfo classInfo) {
}

private String toDirectiveName(ClassInfo classInfo, Annotations annotations) {
String name = TypeNameHelper.getAnyTypeName((String) null, null, classInfo, annotations,
referenceCreator.getTypeAutoNameStrategy());
String name = TypeNameHelper.getAnyTypeName(classInfo, annotations, referenceCreator.getTypeAutoNameStrategy());
if (Character.isUpperCase(name.charAt(0)))
name = Character.toLowerCase(name.charAt(0)) + name.substring(1);
return name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,12 +237,11 @@ public Reference createReference(Direction direction, ClassInfo classInfo, boole
// Now we should have the correct reference type.
String className = classInfo.name().toString();

String name = TypeNameHelper.getAnyTypeName(
addParametrizedTypeNameExtension
? TypeNameHelper.createParametrizedTypeNameExtension(parametrizedTypeArgumentsReferences)
: null,
referenceType, classInfo, annotationsForClass,
this.autoNameStrategy);
String name = TypeNameHelper.getAnyTypeName(classInfo,
annotationsForClass,
this.autoNameStrategy,
referenceType,
parametrizedTypeArgumentsReferences);

Reference reference = new Reference(className, name, referenceType, parametrizedTypeArgumentsReferences,
addParametrizedTypeNameExtension);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,11 @@ public Type create(ClassInfo classInfo, Reference reference) {
Annotations annotations = Annotations.getAnnotationsForClass(classInfo);

// Name
String name = TypeNameHelper.getAnyTypeName(reference, referenceType(), classInfo, annotations,
referenceCreator.getTypeAutoNameStrategy());
String name = TypeNameHelper.getAnyTypeName(classInfo,
annotations,
referenceCreator.getTypeAutoNameStrategy(),
referenceType(),
reference.getParametrizedTypeArguments());

// Description
String description = DescriptionHelper.getDescriptionForType(annotations).orElse(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@ public EnumType create(ClassInfo classInfo, Reference reference) {
Annotations annotations = Annotations.getAnnotationsForClass(classInfo);

// Name
String name = TypeNameHelper.getAnyTypeName(reference, ReferenceType.ENUM, classInfo, annotations, autoNameStrategy);
String name = TypeNameHelper.getAnyTypeName(classInfo,
annotations,
autoNameStrategy,
ReferenceType.ENUM,
reference.getParametrizedTypeArguments());

// Description
Optional<String> maybeDescription = DescriptionHelper.getDescriptionForType(annotations);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,11 @@ public InputType create(ClassInfo classInfo, Reference reference) {
Annotations annotations = Annotations.getAnnotationsForClass(classInfo);

// Name
String name = TypeNameHelper.getAnyTypeName(reference, ReferenceType.INPUT, classInfo, annotations,
fieldCreator.getTypeAutoNameStrategy());
String name = TypeNameHelper.getAnyTypeName(classInfo,
annotations,
fieldCreator.getTypeAutoNameStrategy(),
ReferenceType.INPUT,
reference.getParametrizedTypeArguments());

// Description
String description = DescriptionHelper.getDescriptionForType(annotations).orElse(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,37 @@ public class TypeNameHelper {
private TypeNameHelper() {
}

public static String getAnyTypeName(Reference reference, ReferenceType referenceType, ClassInfo classInfo,
Annotations annotationsForThisClass, TypeAutoNameStrategy autoNameStrategy) {
String parametrizedTypeNameExtension = createParametrizedTypeNameExtension(reference);
return getAnyTypeName(parametrizedTypeNameExtension, referenceType, classInfo, annotationsForThisClass,
autoNameStrategy);
public static String getAnyTypeName(ClassInfo classInfo,
Annotations annotationsForThisClass,
TypeAutoNameStrategy autoNameStrategy) {

return getAnyTypeName(classInfo,
annotationsForThisClass,
autoNameStrategy,
null, // referenceType
null); //classParametrizedTypes
}

public static String getAnyTypeName(String parametrizedTypeNameExtension, ReferenceType referenceType, ClassInfo classInfo,
Annotations annotationsForThisClass, TypeAutoNameStrategy autoNameStrategy) {
public static String getAnyTypeName(ClassInfo classInfo,
Annotations annotationsForThisClass,
TypeAutoNameStrategy autoNameStrategy,
ReferenceType referenceType) {

return getAnyTypeName(classInfo,
annotationsForThisClass,
autoNameStrategy,
referenceType,
null); //classParametrizedTypes
}

public static String getAnyTypeName(ClassInfo classInfo,
Annotations annotationsForThisClass,
TypeAutoNameStrategy autoNameStrategy,
ReferenceType referenceType,
Map<String, Reference> classParametrizedTypes) {

String parametrizedTypeNameExtension = createParametrizedTypeNameExtension(classParametrizedTypes);

if (Classes.isEnum(classInfo)) {
return getNameForClassType(classInfo, annotationsForThisClass, Annotations.ENUM, parametrizedTypeNameExtension,
autoNameStrategy);
Expand All @@ -52,6 +74,17 @@ public static String getAnyTypeName(String parametrizedTypeNameExtension, Refere
}
}

public static String createParametrizedTypeNameExtension(Map<String, Reference> classParametrizedTypes) {
if (classParametrizedTypes == null || classParametrizedTypes.isEmpty())
return null;
StringBuilder sb = new StringBuilder();
for (Reference gp : classParametrizedTypes.values()) {
sb.append(UNDERSCORE);
sb.append(gp.getName());
}
return sb.toString();
}

private static String getNameForClassType(ClassInfo classInfo, Annotations annotations, DotName typeName,
String parametrizedTypeNameExtension, TypeAutoNameStrategy autoNameStrategy) {
return getNameForClassType(classInfo, annotations, typeName, parametrizedTypeNameExtension, null, autoNameStrategy);
Expand Down Expand Up @@ -86,33 +119,6 @@ private static String getNameForClassType(ClassInfo classInfo, Annotations annot
return sb.toString();
}

public static String createParametrizedTypeNameExtension(Map<String, Reference> parametrizedTypeArgumentsReferences) {
if (parametrizedTypeArgumentsReferences == null || parametrizedTypeArgumentsReferences.isEmpty())
return null;
StringBuilder sb = new StringBuilder();
for (Reference gp : parametrizedTypeArgumentsReferences.values()) {
appendParametrizedArgumet(sb, gp);
}
return sb.toString();
}

private static final void appendParametrizedArgumet(StringBuilder sb, Reference gp) {
sb.append(UNDERSCORE);
sb.append(gp.getName());
}

public static String createParametrizedTypeNameExtension(Reference reference) {
if (!reference.isAddParametrizedTypeNameExtension() || reference.getParametrizedTypeArguments() == null
|| reference.getParametrizedTypeArguments().isEmpty())
return null;
StringBuilder sb = new StringBuilder();
for (Reference gp : reference.getParametrizedTypeArguments().values()) {
sb.append("_");
sb.append(gp.getName());
}
return sb.toString();
}

private static String applyNamingStrategy(ClassInfo classInfo, TypeAutoNameStrategy autoNameStrategy) {
if (autoNameStrategy.equals(TypeAutoNameStrategy.Full)) {
return classInfo.name().toString().replaceAll("\\.", UNDERSCORE).replaceAll("\\$", "");
Expand All @@ -127,4 +133,4 @@ private static String applyNamingStrategy(ClassInfo classInfo, TypeAutoNameStrat

private static final String INPUT = "Input";
private static final String UNDERSCORE = "_";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package io.smallrye.graphql.test.apps.generics.inheritance.api;

public abstract class AbstractHasID<I extends ID<I>> {
protected I id;

public I getId() {
return this.id;
}

public void setId(I id) {
this.id = id;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package io.smallrye.graphql.test.apps.generics.inheritance.api;

public abstract class AbstractID<I extends ID<I>> {

private String id;

public AbstractID(final String id) {
this.id = id;
}

public String getValue() {
return this.id;
}

public void setValue(String id) {
this.id = id;
}

@Override
public String toString() {
return this.id;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package io.smallrye.graphql.test.apps.generics.inheritance.api;

import java.time.LocalDate;

public class Film extends AbstractHasID<TestID> {

private String title;
private Integer episode;
private String director;
private LocalDate releaseDate;

public Film() {

}

public Film(
final TestID id,
final String title,
final int episode,
final String director,
final LocalDate releaseDate) {
this.id = id;
this.title = title;
this.episode = episode;
this.director = director;
this.releaseDate = releaseDate;
}

public String getTitle() {
return this.title;
}

public Integer getEpisode() {
return this.episode;
}

public String getDirector() {
return this.director;
}

public LocalDate getReleaseDate() {
return this.releaseDate;
}

public void setTitle(String title) {
this.title = title;
}

public void setEpisode(Integer episode) {
this.episode = episode;
}

public void setDirector(String director) {
this.director = director;
}

public void setReleaseDate(LocalDate releaseDate) {
this.releaseDate = releaseDate;
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder();

sb.append("Id: ").append(this.id.toString()).append("\n");
sb.append("Title: ").append(this.title).append("\n");
sb.append("Episode: ").append(this.episode).append("\n");
sb.append("Director: ").append(this.director).append("\n");
sb.append("ReleaseDate: ").append(this.releaseDate).append("\n");

return sb.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package io.smallrye.graphql.test.apps.generics.inheritance.api;

import java.util.List;

import jakarta.inject.Inject;

import org.eclipse.microprofile.graphql.GraphQLApi;
import org.eclipse.microprofile.graphql.Mutation;
import org.eclipse.microprofile.graphql.Query;

import io.smallrye.mutiny.Uni;

@GraphQLApi
public class FilmResource {

@Inject
FilmService service;

@Query("allFilms")
public Uni<List<Film>> getFilms() {
return this.service.getFilms();
}

@Mutation
public Uni<Boolean> addFilm(final Film f) {
return this.service.addFilm(f);
}

@Mutation
public Uni<Boolean> deleteFilm(int id) {
return this.service.deleteFilm(id);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package io.smallrye.graphql.test.apps.generics.inheritance.api;

import java.time.LocalDate;
import java.time.Month;
import java.util.ArrayList;
import java.util.List;

import jakarta.enterprise.context.ApplicationScoped;

import io.smallrye.mutiny.Uni;

@ApplicationScoped
public class FilmService {

private List<Film> films = new ArrayList<>();

public FilmService() {
Film aNewHope = new Film(
new TestID("1"), "A New Hope", 4, "George Lucas", LocalDate.of(1977, Month.JANUARY, 25));

Film theEmpireStrikesBack = new Film(
new TestID("2"), "The Empire Strikes Back", 5, "Irvin Kershner", LocalDate.of(1980,
Month.MARCH, 17));

Film returnOfTheJedi = new Film(
new TestID("3"), "Return of the Jedi", 6, "Richard Marquand", LocalDate.of(1983, Month.MAY,
25));

films.add(aNewHope);
films.add(theEmpireStrikesBack);
films.add(returnOfTheJedi);
}

public Uni<List<Film>> getFilms() {
System.out.println("Films get method called");

for (Film f : films) {
System.out.println(f);
}

return Uni.createFrom().item(this.films);
}

public Uni<Boolean> addFilm(Film f) {
return Uni.createFrom().item(this.films.add(f));
}

public Uni<Boolean> deleteFilm(int id) {
try {
this.films.remove(id);
return Uni.createFrom().item(true);
}

catch (IndexOutOfBoundsException | UnsupportedOperationException e) {
return Uni.createFrom().item(false);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package io.smallrye.graphql.test.apps.generics.inheritance.api;

public interface ID<I extends ID<I>> {

// Marker interface.
}
Loading

0 comments on commit 1d83c1d

Please sign in to comment.