From 206b703df2dbee7b33abae9a37ebbbb9bb6fc163 Mon Sep 17 00:00:00 2001 From: Rama Palaniappan Date: Sat, 10 Feb 2024 06:38:02 -0800 Subject: [PATCH] Issue-129 Read typeMapping from properties file in classpath (#130) * Issue-129 Read GraphQL type to Java/Kotlin class mapping from typeMapping properties file in compile-classpath * Issue-129 Update version as per review comment Update ReadMe * Issue-129 Update version as per review comment * Remove CustomUrlClassLoader.java and read it through jarFile * Issue-129 Remove debug logs --- README.md | 18 ++++++ pom.xml | 2 +- .../deweyjose/graphqlcodegen/Codegen.java | 63 ++++++++++++++++++- 3 files changed, 81 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 57b9982..46c9c45 100644 --- a/README.md +++ b/README.md @@ -172,6 +172,24 @@ Example ``` +## typeMappingPropertiesFiles +Provide typeMapping as properties file(s) that is accessible as a compile-time-classpath resource +Key-Value pairs in the properties file will be added to `typeMapping` Map when it is not already present in it + +When a same GraphQL type is present in both `typeMapping` and also in `typeMappingPropertiesFiles`, value in `typeMapping` will be used (and the value from `typeMappingPropertiesFiles` *will* be ignored) + +- Type: Array +- Required: false + +Example + +```xml + + commontypes-typeMapping.properties + someother-commontypes-typeMapping.properties + +``` + ## subPackageNameClient - Type: string diff --git a/pom.xml b/pom.xml index 52378ce..884f17e 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ io.github.deweyjose graphqlcodegen-maven-plugin maven-plugin - 1.51.3 + 1.60.0 GraphQL Code Generator Maven port of the Netflix DGS GraphQL Codegen gradle build plugin diff --git a/src/main/java/io/github/deweyjose/graphqlcodegen/Codegen.java b/src/main/java/io/github/deweyjose/graphqlcodegen/Codegen.java index 49d589d..d7db4cb 100644 --- a/src/main/java/io/github/deweyjose/graphqlcodegen/Codegen.java +++ b/src/main/java/io/github/deweyjose/graphqlcodegen/Codegen.java @@ -3,26 +3,34 @@ import com.netflix.graphql.dgs.codegen.CodeGen; import com.netflix.graphql.dgs.codegen.CodeGenConfig; import com.netflix.graphql.dgs.codegen.Language; +import org.apache.maven.artifact.Artifact; import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugins.annotations.LifecyclePhase; import org.apache.maven.plugins.annotations.Mojo; import org.apache.maven.plugins.annotations.Parameter; +import org.apache.maven.plugins.annotations.ResolutionScope; import org.apache.maven.project.MavenProject; import java.io.File; +import java.io.IOException; +import java.io.InputStream; import java.nio.file.Paths; import java.util.Arrays; import java.util.HashSet; +import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; import java.util.Set; +import java.util.jar.JarFile; +import java.util.zip.ZipEntry; import static java.lang.String.format; import static java.util.Arrays.stream; import static java.util.Collections.emptySet; import static java.util.stream.Collectors.*; -@Mojo(name = "generate", defaultPhase = LifecyclePhase.GENERATE_SOURCES) +@Mojo(name = "generate", defaultPhase = LifecyclePhase.GENERATE_SOURCES, + requiresDependencyResolution = ResolutionScope.COMPILE) public class Codegen extends AbstractMojo { @Parameter(defaultValue = "${project}") @@ -55,6 +63,15 @@ public class Codegen extends AbstractMojo { @Parameter(property = "typeMapping") private Map typeMapping; + /** + * Provide the typeMapping as properties file(s) + * that is accessible as a compile-time-classpath resource + * Values in the properties file will be added to `typeMapping` Map + * when it is not already present + */ + @Parameter(property = "typeMappingPropertiesFiles") + private String [] typeMappingPropertiesFiles; + @Parameter(property = "generateBoxedTypes", defaultValue = "false") private boolean generateBoxedTypes; @@ -209,6 +226,25 @@ public void execute() { return; } + if (typeMappingPropertiesFiles!=null && typeMappingPropertiesFiles.length > 0) { + Set dependencies = project.getArtifacts(); + java.util.Properties typeMappingProperties = new java.util.Properties(); + for (Artifact dependency : dependencies) { + File artifactFile = dependency.getFile(); + if (artifactFile != null && artifactFile.isFile()) { + loadPropertiesFile(typeMappingProperties, artifactFile, typeMappingPropertiesFiles); + } + } + //Set key-value from this properties object to typeMapping Map + //only when it is not already present in the Map + if (typeMapping == null) { + typeMapping = new HashMap<>(); + } + typeMappingProperties.forEach((k, v) -> { + typeMapping.putIfAbsent(String.valueOf(k), String.valueOf(v)); + }); + } + final CodeGenConfig config = new CodeGenConfig( emptySet(), schemaPaths, @@ -268,4 +304,29 @@ public void execute() { } } } + + /** + * + * @param typeMappingProperties: Java Properties where typeMapping will be loaded into + * @param artifactFile: Artifact file + * @param typeMappingPropertiesFiles: Input: Classpath location of typeMapping properties file + * @return + */ + private void loadPropertiesFile(java.util.Properties typeMappingProperties, + File artifactFile, String [] typeMappingPropertiesFiles) { + try (JarFile jarFile = new JarFile(artifactFile)) { + for (String file : typeMappingPropertiesFiles) { + ZipEntry entry = jarFile.getEntry(file); + if (entry!=null) { + try (InputStream inputStream = jarFile.getInputStream(entry)) { + // load the data into the typeMappingProperties + typeMappingProperties.load(inputStream); + } + } + } + } catch (IOException e) { + getLog().error(e); + } + } + }