diff --git a/build.gradle b/build.gradle index 244b5d602..c1480bbcc 100644 --- a/build.gradle +++ b/build.gradle @@ -9,7 +9,7 @@ plugins { id "org.sonarqube" version "3.2.0" } -def graphqlCodegenVersion = '5.3.0' // This variable used in the automatic release process +def graphqlCodegenVersion = '5.4.0' // This variable used in the automatic release process group = "io.github.kobylynskyi" version = graphqlCodegenVersion diff --git a/docs/codegen-options.md b/docs/codegen-options.md index ea6c0b481..bc43da2a2 100644 --- a/docs/codegen-options.md +++ b/docs/codegen-options.md @@ -56,7 +56,13 @@ See [DirectiveAnnotationsMapping](#option-directiveannotationsmapping)* | | `responseProjectionMaxDepth` | Integer | 3 | Sets max depth when use `all$()` which for facilitating the construction of projection automatically, the fields on all projections are provided when it be invoked. This is a global configuration, of course, you can use `all$(max)` to set for each method. For self recursive types, too big depth may result in a large number of returned data!| | `generatedLanguage` | Enum | GeneratedLanguage.JAVA | Choose which language you want to generate, Java,Scala,Kotlin were supported. Note that due to language features, there are slight differences in default values between languages.| | `generateModelOpenClasses` | Boolean | false | The class type of the generated model. If true, generate normal classes, else generate data classes. It only support in kotlin(```data class```) and scala(```case class```). Maybe we will consider to support Java ```record``` in the future.| +| `initializeNullableTypes` | Boolean | false | Adds a default null value to nullable arguments. Only supported in Kotlin. +| `generateSealedInterfaces` | Boolean | false | This applies to generated interfaces on unions and interfaces. If true, generate sealed interfaces, else generate normal ones. It is only supported in Kotlin. | | `typesAsInterfaces` | Set(String) | Empty | Types that must generated as interfaces should be defined here in format: `TypeName` or `@directive`. E.g.: `User`, `@asInterface`. | +| `useObjectMapperForRequestSerialization` | Set(String) | Empty | Fields that require serialization using `com.fasterxml.jackson.databind.ObjectMapper#writeValueAsString(Object)`. Values should be defined here in the following format: `GraphqlObjectName.fieldName` or `GraphqlTypeName`. If just type is specified, then all fields of this type will be serialized using ObjectMapper. E.g.: `["Person.createdDateTime", ZonedDateTime"]` | +| `supportUnknownFields` | Boolean | false | Specifies whether api classes should support unknown fields during serialization or deserialization. If `true`, classes will include a property of type [`java.util.Map`](https://docs.oracle.com/javase/8/docs/api/index.html?java/util/Map.html) that will store unknown fields.| +| `unknownFieldsPropertyName` | String | userDefinedFields | Specifies the name of the property to be included in api classes to support unknown fields during serialization or deserialization| + ### Option `graphqlSchemas` diff --git a/plugins/gradle/README.md b/plugins/gradle/README.md index 635c913ea..6b33801c4 100644 --- a/plugins/gradle/README.md +++ b/plugins/gradle/README.md @@ -17,7 +17,7 @@ ```groovy plugins { - id "io.github.kobylynskyi.graphql.codegen" version "5.3.0" + id "io.github.kobylynskyi.graphql.codegen" version "5.4.0" } ``` @@ -31,7 +31,7 @@ buildscript { } } dependencies { - classpath "io.github.kobylynskyi.graphql.codegen:graphql-codegen-gradle-plugin:5.3.0" + classpath "io.github.kobylynskyi.graphql.codegen:graphql-codegen-gradle-plugin:5.4.0" } } diff --git a/plugins/gradle/example-client-kotlin/build.gradle b/plugins/gradle/example-client-kotlin/build.gradle index 0ab0ccce3..d812d21e7 100644 --- a/plugins/gradle/example-client-kotlin/build.gradle +++ b/plugins/gradle/example-client-kotlin/build.gradle @@ -4,10 +4,10 @@ import io.github.kobylynskyi.graphql.codegen.gradle.GraphQLCodegenGradleTask plugins { id 'java' id "org.jetbrains.kotlin.jvm" version "1.3.71" - id "io.github.kobylynskyi.graphql.codegen" version "5.3.0" + id "io.github.kobylynskyi.graphql.codegen" version "5.4.0" } -def graphqlCodegenClientKotlinVersion = '5.3.0' // Variable used in the automatic release process +def graphqlCodegenClientKotlinVersion = '5.4.0' // Variable used in the automatic release process group = 'io.github.dreamylost' version = graphqlCodegenClientKotlinVersion @@ -29,7 +29,7 @@ repositories { dependencies { - implementation "io.github.kobylynskyi:graphql-java-codegen:5.3.0" + implementation "io.github.kobylynskyi:graphql-java-codegen:5.4.0" implementation "javax.validation:validation-api:2.0.1.Final" implementation "com.squareup.okhttp3:okhttp:4.2.2" implementation "com.fasterxml.jackson.core:jackson-core:2.12.0" @@ -57,6 +57,7 @@ task graphqlCodegenKotlinService(type: GraphQLCodegenGradleTask) { apiPackageName = "io.github.dreamylost.api" modelPackageName = "io.github.dreamylost.model" generateModelOpenClasses = true + generateSealedInterfaces = true customAnnotationsMapping = [ "Character": ["@com.fasterxml.jackson.annotation.JsonTypeInfo(use=com.fasterxml.jackson.annotation.JsonTypeInfo.Id.NAME, include=com.fasterxml.jackson.annotation.JsonTypeInfo.As.PROPERTY,property = \"__typename\")", "@com.fasterxml.jackson.annotation.JsonSubTypes(value = arrayOf(" + System.lineSeparator() + @@ -64,4 +65,5 @@ task graphqlCodegenKotlinService(type: GraphQLCodegenGradleTask) { " com.fasterxml.jackson.annotation.JsonSubTypes.Type(value = DroidTO::class, name = \"Droid\")))"], ] modelNameSuffix = "TO" + supportUnknownFields = true } \ No newline at end of file diff --git a/plugins/gradle/example-client/build.gradle b/plugins/gradle/example-client/build.gradle index fa7573f53..75fe7d650 100644 --- a/plugins/gradle/example-client/build.gradle +++ b/plugins/gradle/example-client/build.gradle @@ -7,7 +7,7 @@ plugins { // use the latest available version: // https://plugins.gradle.org/plugin/io.github.kobylynskyi.graphql.codegen - id "io.github.kobylynskyi.graphql.codegen" version "5.3.0" + id "io.github.kobylynskyi.graphql.codegen" version "5.4.0" } mainClassName = "io.github.kobylynskyi.order.Application" @@ -22,7 +22,7 @@ dependencies { // use the latest available version: // https://search.maven.org/artifact/io.github.kobylynskyi/graphql-java-codegen - implementation "io.github.kobylynskyi:graphql-java-codegen:5.3.0" + implementation "io.github.kobylynskyi:graphql-java-codegen:5.4.0" implementation "org.apache.httpcomponents:httpclient:4.5.13" implementation "javax.validation:validation-api:2.0.1.Final" diff --git a/plugins/gradle/example-server/build.gradle b/plugins/gradle/example-server/build.gradle index 34e4492f2..a9c00353d 100644 --- a/plugins/gradle/example-server/build.gradle +++ b/plugins/gradle/example-server/build.gradle @@ -6,7 +6,7 @@ plugins { // // use the latest available version: // https://plugins.gradle.org/plugin/io.github.kobylynskyi.graphql.codegen - id "io.github.kobylynskyi.graphql.codegen" version "5.3.0" + id "io.github.kobylynskyi.graphql.codegen" version "5.4.0" } mainClassName = "io.github.kobylynskyi.product.Application" @@ -48,6 +48,9 @@ graphqlCodegen { } modelNameSuffix = "TO" generateApis = true + supportUnknownFields = true + unknownFieldsPropertyName = "additionalFields" + } repositories { diff --git a/plugins/gradle/example-server/src/main/java/io/github/kobylynskyi/product/Application.java b/plugins/gradle/example-server/src/main/java/io/github/kobylynskyi/product/Application.java index 932bb676d..8f8ec98e0 100644 --- a/plugins/gradle/example-server/src/main/java/io/github/kobylynskyi/product/Application.java +++ b/plugins/gradle/example-server/src/main/java/io/github/kobylynskyi/product/Application.java @@ -6,6 +6,7 @@ @SpringBootApplication public class Application { + public static void main(String[] args) { SpringApplication.run(Application.class, args); } diff --git a/plugins/gradle/graphql-java-codegen-gradle-plugin/build.gradle b/plugins/gradle/graphql-java-codegen-gradle-plugin/build.gradle index 7e04ddbfc..b6d387dda 100644 --- a/plugins/gradle/graphql-java-codegen-gradle-plugin/build.gradle +++ b/plugins/gradle/graphql-java-codegen-gradle-plugin/build.gradle @@ -16,7 +16,7 @@ apply plugin: "java" apply plugin: "idea" apply plugin: "maven-publish" -def graphqlCodegenGradlePluginVersion = '5.3.0' // This variable used in the automatic release process +def graphqlCodegenGradlePluginVersion = '5.4.0' // This variable used in the automatic release process group = "io.github.kobylynskyi" version = graphqlCodegenGradlePluginVersion diff --git a/plugins/gradle/graphql-java-codegen-gradle-plugin/src/main/java/io/github/kobylynskyi/graphql/codegen/gradle/GraphQLCodegenGradleTask.java b/plugins/gradle/graphql-java-codegen-gradle-plugin/src/main/java/io/github/kobylynskyi/graphql/codegen/gradle/GraphQLCodegenGradleTask.java index 3a3289d61..4b032aec0 100644 --- a/plugins/gradle/graphql-java-codegen-gradle-plugin/src/main/java/io/github/kobylynskyi/graphql/codegen/gradle/GraphQLCodegenGradleTask.java +++ b/plugins/gradle/graphql-java-codegen-gradle-plugin/src/main/java/io/github/kobylynskyi/graphql/codegen/gradle/GraphQLCodegenGradleTask.java @@ -103,6 +103,11 @@ public class GraphQLCodegenGradleTask extends DefaultTask implements GraphQLCode private List configurationFiles; private GeneratedLanguage generatedLanguage = MappingConfigConstants.DEFAULT_GENERATED_LANGUAGE; private Boolean generateModelOpenClasses = MappingConfigConstants.DEFAULT_GENERATE_MODEL_OPEN_CLASSES; + private Boolean initializeNullableTypes = MappingConfigConstants.DEFAULT_INITIALIZE_NULLABLE_TYPES; + private Boolean generateSealedInterfaces = MappingConfigConstants.DEFAULT_GENERATE_SEALED_INTERFACES; + + private Boolean supportUnknownFields = MappingConfigConstants.DEFAULT_SUPPORT_UNKNOWN_FIELDS; + private String unknownFieldsPropertyName = MappingConfigConstants.DEFAULT_UNKNOWN_FIELDS_PROPERTY_NAME; public GraphQLCodegenGradleTask() { setGroup("codegen"); @@ -178,6 +183,10 @@ public void generate() throws Exception { mappingConfig.setGeneratedLanguage(generatedLanguage); mappingConfig.setGenerateModelOpenClasses(generateModelOpenClasses); + mappingConfig.setInitializeNullableTypes(initializeNullableTypes); + + mappingConfig.setSupportUnknownFields(isSupportUnknownFields()); + mappingConfig.setUnknownFieldsPropertyName(getUnknownFieldsPropertyName()); instantiateCodegen(mappingConfig).generate(); } @@ -844,4 +853,50 @@ public Boolean isGenerateModelOpenClasses() { public void setGenerateModelOpenClasses(Boolean generateModelOpenClasses) { this.generateModelOpenClasses = generateModelOpenClasses; } + + @Input + @Optional + @Override + public Boolean isInitializeNullableTypes() { + return initializeNullableTypes; + } + + public void setInitializeNullableTypes(Boolean initializeNullableTypes) { + this.initializeNullableTypes = initializeNullableTypes; + } + + @Input + @Optional + @Override + public Boolean isGenerateSealedInterfaces() { + return generateSealedInterfaces; + } + + public void setGenerateSealedInterfaces(Boolean generateSealedInterfaces) { + this.generateSealedInterfaces = generateSealedInterfaces; + } + + @Input + @Optional + @Override + public Boolean isSupportUnknownFields() { + return supportUnknownFields; + } + + public void setSupportUnknownFields(boolean supportUnknownFields) { + this.supportUnknownFields = supportUnknownFields; + } + + @Input + @Optional + @Override + public String getUnknownFieldsPropertyName() { + return unknownFieldsPropertyName; + } + + public void setUnknownFieldsPropertyName(String unknownFieldsPropertyName) { + this.unknownFieldsPropertyName = unknownFieldsPropertyName; + } + + } diff --git a/plugins/maven/README.md b/plugins/maven/README.md index edcd653c8..aa031b287 100644 --- a/plugins/maven/README.md +++ b/plugins/maven/README.md @@ -20,7 +20,7 @@ io.github.kobylynskyi graphql-codegen-maven-plugin - 5.3.0 + 5.4.0 diff --git a/plugins/maven/example-client/pom.xml b/plugins/maven/example-client/pom.xml index d665e5c31..5b43d3af5 100644 --- a/plugins/maven/example-client/pom.xml +++ b/plugins/maven/example-client/pom.xml @@ -4,7 +4,7 @@ io.github.kobylynskyi graphql-codegen-maven-plugin-example-client - 5.3.0 + 5.4.0 graphql-codegen-maven-plugin-example-client diff --git a/plugins/maven/example-server/pom.xml b/plugins/maven/example-server/pom.xml index 6d001ca7c..172346ca7 100644 --- a/plugins/maven/example-server/pom.xml +++ b/plugins/maven/example-server/pom.xml @@ -4,7 +4,7 @@ io.github.kobylynskyi graphql-codegen-maven-plugin-example-server - 5.3.0 + 5.4.0 graphql-codegen-maven-plugin-example-server diff --git a/plugins/maven/graphql-java-codegen-maven-plugin/pom.xml b/plugins/maven/graphql-java-codegen-maven-plugin/pom.xml index 49d885fd7..0e082562b 100644 --- a/plugins/maven/graphql-java-codegen-maven-plugin/pom.xml +++ b/plugins/maven/graphql-java-codegen-maven-plugin/pom.xml @@ -3,7 +3,7 @@ io.github.kobylynskyi graphql-codegen-maven-plugin - 5.3.1-SNAPSHOT + 5.4.0 maven-plugin graphql-codegen-maven-plugin @@ -72,7 +72,7 @@ 3.0.1 3.3.4 - 5.3.0 + 5.4.0 diff --git a/plugins/maven/graphql-java-codegen-maven-plugin/src/main/java/io/github/kobylynskyi/graphql/codegen/GraphQLCodegenMojo.java b/plugins/maven/graphql-java-codegen-maven-plugin/src/main/java/io/github/kobylynskyi/graphql/codegen/GraphQLCodegenMojo.java index 7c00c07b6..e2982fc29 100644 --- a/plugins/maven/graphql-java-codegen-maven-plugin/src/main/java/io/github/kobylynskyi/graphql/codegen/GraphQLCodegenMojo.java +++ b/plugins/maven/graphql-java-codegen-maven-plugin/src/main/java/io/github/kobylynskyi/graphql/codegen/GraphQLCodegenMojo.java @@ -200,6 +200,12 @@ public class GraphQLCodegenMojo extends AbstractMojo implements GraphQLCodegenCo @Parameter private String[] configurationFiles; + @Parameter(defaultValue = MappingConfigConstants.DEFAULT_SUPPORT_UNKNOWN_FIELDS_STRING) + private boolean supportUnknownFields; + + @Parameter(defaultValue = MappingConfigConstants.DEFAULT_UNKNOWN_FIELDS_PROPERTY_NAME) + private String unknownFieldsPropertyName; + /** * The project being built. */ @@ -209,6 +215,12 @@ public class GraphQLCodegenMojo extends AbstractMojo implements GraphQLCodegenCo @Parameter(defaultValue = MappingConfigConstants.DEFAULT_GENERATE_MODEL_OPEN_CLASSES_STRING) private boolean generateModelOpenClasses; + @Parameter(defaultValue = MappingConfigConstants.DEFAULT_INITIALIZE_NULLABLE_TYPES_STRING) + private boolean initializeNullableTypes; + + @Parameter(defaultValue = MappingConfigConstants.DEFAULT_GENERATE_SEALED_INTERFACES_STRING) + private boolean generateSealedInterfaces; + @Override public void execute() throws MojoExecutionException { addCompileSourceRootIfConfigured(); @@ -267,6 +279,11 @@ public void execute() throws MojoExecutionException { mappingConfig.setGeneratedLanguage(generatedLanguage); mappingConfig.setGenerateModelOpenClasses(isGenerateModelOpenClasses()); + mappingConfig.setInitializeNullableTypes(isInitializeNullableTypes()); + mappingConfig.setGenerateSealedInterfaces(isGenerateSealedInterfaces()); + + mappingConfig.setSupportUnknownFields(isSupportUnknownFields()); + mappingConfig.setUnknownFieldsPropertyName(getUnknownFieldsPropertyName()); try { instantiateCodegen(mappingConfig).generate(); @@ -603,6 +620,16 @@ public Boolean isGenerateModelOpenClasses() { return generateModelOpenClasses; } + @Override + public Boolean isInitializeNullableTypes() { + return initializeNullableTypes; + } + + @Override + public Boolean isGenerateSealedInterfaces() { + return generateSealedInterfaces; + } + public ParentInterfacesConfig getParentInterfaces() { return parentInterfaces; } @@ -611,6 +638,24 @@ public String[] getConfigurationFiles() { return configurationFiles; } + @Override + public Boolean isSupportUnknownFields() { + return supportUnknownFields; + } + + public void setSupportUnknownFields(boolean supportUnknownFields) { + this.supportUnknownFields = supportUnknownFields; + } + + @Override + public String getUnknownFieldsPropertyName() { + return unknownFieldsPropertyName; + } + + public void setUnknownFieldsPropertyName(String unknownFieldsPropertyName) { + this.unknownFieldsPropertyName = unknownFieldsPropertyName; + } + private static Map> convertToListsMap(Map sourceMap) { if (sourceMap == null) { return new HashMap<>(); diff --git a/plugins/sbt/graphql-java-codegen-sbt-plugin/src/main/scala/io/github/dreamylost/graphql/codegen/GraphQLCodegenKeys.scala b/plugins/sbt/graphql-java-codegen-sbt-plugin/src/main/scala/io/github/dreamylost/graphql/codegen/GraphQLCodegenKeys.scala index 70420c1b1..a62a58f6d 100644 --- a/plugins/sbt/graphql-java-codegen-sbt-plugin/src/main/scala/io/github/dreamylost/graphql/codegen/GraphQLCodegenKeys.scala +++ b/plugins/sbt/graphql-java-codegen-sbt-plugin/src/main/scala/io/github/dreamylost/graphql/codegen/GraphQLCodegenKeys.scala @@ -124,6 +124,8 @@ trait GraphQLCodegenKeys { val generateModelOpenClasses = settingKey[Boolean]("The class type of the generated model. If true, generate normal classes, else generate case class.") + val generateSealedInterfaces = settingKey[Boolean]("If true, generate sealed interfaces for GraphQL unions and interfaces, else generate normal interfaces.") + val generateJacksonTypeIdResolver = settingKey[Boolean]("Specifies whether generated union interfaces should be annotated with a custom Jackson type id resolver generated in model package") //for version @@ -133,4 +135,7 @@ trait GraphQLCodegenKeys { //some others for sbt val generateCodegenTargetPath = settingKey[File]("Where to store generated files and add the generated code to the classpath, so that they can be referenced.") + val supportUnknownFields = settingKey[Boolean]("supportUnknownFields") + val unknownFieldsPropertyName = settingKey[String]("unknownFieldsPropertyName") + } diff --git a/plugins/sbt/graphql-java-codegen-sbt-plugin/src/main/scala/io/github/dreamylost/graphql/codegen/GraphQLCodegenPlugin.scala b/plugins/sbt/graphql-java-codegen-sbt-plugin/src/main/scala/io/github/dreamylost/graphql/codegen/GraphQLCodegenPlugin.scala index e8e0c37d3..1e2833fa4 100644 --- a/plugins/sbt/graphql-java-codegen-sbt-plugin/src/main/scala/io/github/dreamylost/graphql/codegen/GraphQLCodegenPlugin.scala +++ b/plugins/sbt/graphql-java-codegen-sbt-plugin/src/main/scala/io/github/dreamylost/graphql/codegen/GraphQLCodegenPlugin.scala @@ -71,6 +71,7 @@ class GraphQLCodegenPlugin(configuration: Configuration, private[codegen] val co graphqlJavaCodegenVersion := None, // suffix/prefix/strategies: generateModelOpenClasses := MappingConfigConstants.DEFAULT_GENERATE_MODEL_OPEN_CLASSES, + generateSealedInterfaces := MappingConfigConstants.DEFAULT_GENERATE_SEALED_INTERFACES, generatedLanguage := MappingConfigConstants.DEFAULT_GENERATED_LANGUAGE, apiNamePrefix := None, apiNameSuffix := MappingConfigConstants.DEFAULT_RESOLVER_SUFFIX, @@ -116,7 +117,10 @@ class GraphQLCodegenPlugin(configuration: Configuration, private[codegen] val co // parent interfaces configs: parentInterfaces := parentInterfacesConfig, generateAllMethodInProjection := MappingConfigConstants.DEFAULT_GENERATE_ALL_METHOD, - responseProjectionMaxDepth := MappingConfigConstants.DEFAULT_RESPONSE_PROJECTION_MAX_DEPTH + responseProjectionMaxDepth := MappingConfigConstants.DEFAULT_RESPONSE_PROJECTION_MAX_DEPTH, + + supportUnknownFields := MappingConfigConstants.DEFAULT_SUPPORT_UNKNOWN_FIELDS, + unknownFieldsPropertyName := MappingConfigConstants.DEFAULT_UNKNOWN_FIELDS_PROPERTY_NAME ) private def getMappingConfig(): Def.Initialize[MappingConfig] = Def.setting { @@ -171,6 +175,10 @@ class GraphQLCodegenPlugin(configuration: Configuration, private[codegen] val co mappingConfig.setGeneratedLanguage((generatedLanguage in GraphQLCodegenConfig).value) mappingConfig.setGenerateModelOpenClasses((generateModelOpenClasses in GraphQLCodegenConfig).value) mappingConfig.setGenerateJacksonTypeIdResolver((generateJacksonTypeIdResolver in GraphQLCodegenConfig).value); + + mappingConfig.setSupportUnknownFields((supportUnknownFields in GraphQLCodegenConfig).value) + mappingConfig.setUnknownFieldsPropertyName((unknownFieldsPropertyName in GraphQLCodegenConfig).value) + mappingConfig } diff --git a/plugins/sbt/graphql-java-codegen-sbt-plugin/src/sbt-test/graphql-codegen-sbt-plugin/example-client-scala/project/plugins.sbt b/plugins/sbt/graphql-java-codegen-sbt-plugin/src/sbt-test/graphql-codegen-sbt-plugin/example-client-scala/project/plugins.sbt index 0cf2afa22..d557effa1 100644 --- a/plugins/sbt/graphql-java-codegen-sbt-plugin/src/sbt-test/graphql-codegen-sbt-plugin/example-client-scala/project/plugins.sbt +++ b/plugins/sbt/graphql-java-codegen-sbt-plugin/src/sbt-test/graphql-codegen-sbt-plugin/example-client-scala/project/plugins.sbt @@ -1 +1 @@ -addSbtPlugin("io.github.jxnu-liguobin" % "graphql-codegen-sbt-plugin" % "5.3.0") +addSbtPlugin("io.github.jxnu-liguobin" % "graphql-codegen-sbt-plugin" % "5.4.0") diff --git a/plugins/sbt/graphql-java-codegen-sbt-plugin/src/sbt-test/graphql-codegen-sbt-plugin/example-client-scala/version.sbt b/plugins/sbt/graphql-java-codegen-sbt-plugin/src/sbt-test/graphql-codegen-sbt-plugin/example-client-scala/version.sbt index 385e378a3..52ab284d7 100644 --- a/plugins/sbt/graphql-java-codegen-sbt-plugin/src/sbt-test/graphql-codegen-sbt-plugin/example-client-scala/version.sbt +++ b/plugins/sbt/graphql-java-codegen-sbt-plugin/src/sbt-test/graphql-codegen-sbt-plugin/example-client-scala/version.sbt @@ -1 +1 @@ -version in ThisBuild := "5.3.0" +version in ThisBuild := "5.4.0" diff --git a/plugins/sbt/graphql-java-codegen-sbt-plugin/src/sbt-test/graphql-codegen-sbt-plugin/example-client/project/plugins.sbt b/plugins/sbt/graphql-java-codegen-sbt-plugin/src/sbt-test/graphql-codegen-sbt-plugin/example-client/project/plugins.sbt index 0cf2afa22..d557effa1 100644 --- a/plugins/sbt/graphql-java-codegen-sbt-plugin/src/sbt-test/graphql-codegen-sbt-plugin/example-client/project/plugins.sbt +++ b/plugins/sbt/graphql-java-codegen-sbt-plugin/src/sbt-test/graphql-codegen-sbt-plugin/example-client/project/plugins.sbt @@ -1 +1 @@ -addSbtPlugin("io.github.jxnu-liguobin" % "graphql-codegen-sbt-plugin" % "5.3.0") +addSbtPlugin("io.github.jxnu-liguobin" % "graphql-codegen-sbt-plugin" % "5.4.0") diff --git a/plugins/sbt/graphql-java-codegen-sbt-plugin/src/sbt-test/graphql-codegen-sbt-plugin/example-client/version.sbt b/plugins/sbt/graphql-java-codegen-sbt-plugin/src/sbt-test/graphql-codegen-sbt-plugin/example-client/version.sbt index 385e378a3..52ab284d7 100644 --- a/plugins/sbt/graphql-java-codegen-sbt-plugin/src/sbt-test/graphql-codegen-sbt-plugin/example-client/version.sbt +++ b/plugins/sbt/graphql-java-codegen-sbt-plugin/src/sbt-test/graphql-codegen-sbt-plugin/example-client/version.sbt @@ -1 +1 @@ -version in ThisBuild := "5.3.0" +version in ThisBuild := "5.4.0" diff --git a/plugins/sbt/graphql-java-codegen-sbt-plugin/src/sbt-test/graphql-codegen-sbt-plugin/simple/project/plugins.sbt b/plugins/sbt/graphql-java-codegen-sbt-plugin/src/sbt-test/graphql-codegen-sbt-plugin/simple/project/plugins.sbt index 79367de42..d898283b9 100644 --- a/plugins/sbt/graphql-java-codegen-sbt-plugin/src/sbt-test/graphql-codegen-sbt-plugin/simple/project/plugins.sbt +++ b/plugins/sbt/graphql-java-codegen-sbt-plugin/src/sbt-test/graphql-codegen-sbt-plugin/simple/project/plugins.sbt @@ -1,4 +1,4 @@ -sys.props.get("plugin.version").orElse(Some("5.3.0")) match { +sys.props.get("plugin.version").orElse(Some("5.4.0")) match { case Some(x) => addSbtPlugin("io.github.jxnu-liguobin" % "graphql-codegen-sbt-plugin" % x) case _ => sys.error("""|The system property 'plugin.version' is not defined. |Specify this property using the scriptedLaunchOpts -D.""".stripMargin) diff --git a/plugins/sbt/graphql-java-codegen-sbt-plugin/src/sbt-test/graphql-codegen-sbt-plugin/simple/version.sbt b/plugins/sbt/graphql-java-codegen-sbt-plugin/src/sbt-test/graphql-codegen-sbt-plugin/simple/version.sbt index 385e378a3..52ab284d7 100644 --- a/plugins/sbt/graphql-java-codegen-sbt-plugin/src/sbt-test/graphql-codegen-sbt-plugin/simple/version.sbt +++ b/plugins/sbt/graphql-java-codegen-sbt-plugin/src/sbt-test/graphql-codegen-sbt-plugin/simple/version.sbt @@ -1 +1 @@ -version in ThisBuild := "5.3.0" +version in ThisBuild := "5.4.0" diff --git a/plugins/sbt/graphql-java-codegen-sbt-plugin/version.sbt b/plugins/sbt/graphql-java-codegen-sbt-plugin/version.sbt index 385e378a3..52ab284d7 100644 --- a/plugins/sbt/graphql-java-codegen-sbt-plugin/version.sbt +++ b/plugins/sbt/graphql-java-codegen-sbt-plugin/version.sbt @@ -1 +1 @@ -version in ThisBuild := "5.3.0" +version in ThisBuild := "5.4.0" diff --git a/src/main/java/com/kobylynskyi/graphql/codegen/kotlin/KotlinGraphQLCodegen.java b/src/main/java/com/kobylynskyi/graphql/codegen/kotlin/KotlinGraphQLCodegen.java index 1c515512d..6589f9129 100644 --- a/src/main/java/com/kobylynskyi/graphql/codegen/kotlin/KotlinGraphQLCodegen.java +++ b/src/main/java/com/kobylynskyi/graphql/codegen/kotlin/KotlinGraphQLCodegen.java @@ -49,6 +49,12 @@ protected void initDefaultValues(MappingConfig mappingConfig) { if (mappingConfig.isGenerateModelOpenClasses() == null) { mappingConfig.setGenerateModelOpenClasses(false); } + if (mappingConfig.isInitializeNullableTypes() == null) { + mappingConfig.setInitializeNullableTypes(false); + } + if (mappingConfig.isGenerateSealedInterfaces() == null) { + mappingConfig.setGenerateSealedInterfaces(false); + } if (mappingConfig.getGenerateBuilder() == null) { // functional expression mappingConfig.setGenerateBuilder(false); diff --git a/src/main/java/com/kobylynskyi/graphql/codegen/mapper/DataModelMapperFactory.java b/src/main/java/com/kobylynskyi/graphql/codegen/mapper/DataModelMapperFactory.java index 7fe3a8825..26fe985a3 100644 --- a/src/main/java/com/kobylynskyi/graphql/codegen/mapper/DataModelMapperFactory.java +++ b/src/main/java/com/kobylynskyi/graphql/codegen/mapper/DataModelMapperFactory.java @@ -23,14 +23,14 @@ public class DataModelMapperFactory { public DataModelMapperFactory(MapperFactory mapperFactory) { this.mapperFactory = mapperFactory; - this.fieldDefToParamMapper = new FieldDefinitionToParameterMapper(mapperFactory); + InputValueDefinitionToParameterMapper inputValueDefToParamMapper = new InputValueDefinitionToParameterMapper( + mapperFactory); + this.fieldDefToParamMapper = new FieldDefinitionToParameterMapper(mapperFactory, inputValueDefToParamMapper); this.enumDefToDataModelMapper = new EnumDefinitionToDataModelMapper(mapperFactory); this.unionDefToDataModelMapper = new UnionDefinitionToDataModelMapper(mapperFactory); this.typeDefToDataModelMapper = new TypeDefinitionToDataModelMapper(mapperFactory, fieldDefToParamMapper); this.interfaceDefToDataModelMapper = new InterfaceDefinitionToDataModelMapper( mapperFactory, fieldDefToParamMapper); - InputValueDefinitionToParameterMapper inputValueDefToParamMapper = new InputValueDefinitionToParameterMapper( - mapperFactory); this.inputDefToDataModelMapper = new InputDefinitionToDataModelMapper( mapperFactory, inputValueDefToParamMapper); this.fieldDefsToResolverDataModelMapper = new FieldDefinitionsToResolverDataModelMapper( diff --git a/src/main/java/com/kobylynskyi/graphql/codegen/mapper/FieldDefinitionToParameterMapper.java b/src/main/java/com/kobylynskyi/graphql/codegen/mapper/FieldDefinitionToParameterMapper.java index e9848ffb9..2e3603c1d 100644 --- a/src/main/java/com/kobylynskyi/graphql/codegen/mapper/FieldDefinitionToParameterMapper.java +++ b/src/main/java/com/kobylynskyi/graphql/codegen/mapper/FieldDefinitionToParameterMapper.java @@ -23,11 +23,14 @@ public class FieldDefinitionToParameterMapper { private final GraphQLTypeMapper graphQLTypeMapper; private final DataModelMapper dataModelMapper; private final AnnotationsMapper annotationsMapper; + private final InputValueDefinitionToParameterMapper inputValueDefinitionToParameterMapper; - public FieldDefinitionToParameterMapper(MapperFactory mapperFactory) { + public FieldDefinitionToParameterMapper(MapperFactory mapperFactory, + InputValueDefinitionToParameterMapper inputValueDefToParamMapper) { this.graphQLTypeMapper = mapperFactory.getGraphQLTypeMapper(); this.dataModelMapper = mapperFactory.getDataModelMapper(); this.annotationsMapper = mapperFactory.getAnnotationsMapper(); + this.inputValueDefinitionToParameterMapper = inputValueDefToParamMapper; } /** @@ -137,6 +140,8 @@ private ParameterDefinition mapField(MappingContext mappingContext, ExtendedFiel parameter.setDeprecated(DeprecatedDefinitionBuilder.build(mappingContext, fieldDef)); parameter.setMandatory(namedDefinition.isMandatory()); parameter.setSerializeUsingObjectMapper(namedDefinition.isSerializeUsingObjectMapper()); + parameter.setInputParameters(inputValueDefinitionToParameterMapper.map( + mappingContext, fieldDef.getInputValueDefinitions(), fieldDef.getName())); return parameter; } diff --git a/src/main/java/com/kobylynskyi/graphql/codegen/mapper/FieldDefinitionsToResolverDataModelMapper.java b/src/main/java/com/kobylynskyi/graphql/codegen/mapper/FieldDefinitionsToResolverDataModelMapper.java index ffbe9ca45..971beca11 100644 --- a/src/main/java/com/kobylynskyi/graphql/codegen/mapper/FieldDefinitionsToResolverDataModelMapper.java +++ b/src/main/java/com/kobylynskyi/graphql/codegen/mapper/FieldDefinitionsToResolverDataModelMapper.java @@ -24,6 +24,7 @@ import java.util.Set; import java.util.stream.Collectors; +import static com.kobylynskyi.graphql.codegen.model.DataModelFields.INITIALIZE_NULLABLE_TYPES; import static com.kobylynskyi.graphql.codegen.model.DataModelFields.CLASS_NAME; import static com.kobylynskyi.graphql.codegen.model.DataModelFields.ENUM_IMPORT_IT_SELF_IN_SCALA; import static com.kobylynskyi.graphql.codegen.model.DataModelFields.GENERATED_ANNOTATION; @@ -162,6 +163,7 @@ private Map mapToResolverModel(MappingContext mappingContext, St dataModel.put(GENERATED_INFO, mappingContext.getGeneratedInformation()); dataModel.put(ENUM_IMPORT_IT_SELF_IN_SCALA, mappingContext.getEnumImportItSelfInScala()); dataModel.put(GENERATE_MODEL_OPEN_CLASSES, mappingContext.isGenerateModelOpenClasses()); + dataModel.put(INITIALIZE_NULLABLE_TYPES, mappingContext.isInitializeNullableTypes()); return dataModel; } diff --git a/src/main/java/com/kobylynskyi/graphql/codegen/mapper/InputDefinitionToDataModelMapper.java b/src/main/java/com/kobylynskyi/graphql/codegen/mapper/InputDefinitionToDataModelMapper.java index 3df7a4749..ffe6f5819 100644 --- a/src/main/java/com/kobylynskyi/graphql/codegen/mapper/InputDefinitionToDataModelMapper.java +++ b/src/main/java/com/kobylynskyi/graphql/codegen/mapper/InputDefinitionToDataModelMapper.java @@ -1,10 +1,12 @@ package com.kobylynskyi.graphql.codegen.mapper; import com.kobylynskyi.graphql.codegen.model.MappingContext; +import com.kobylynskyi.graphql.codegen.model.ParameterDefinition; import com.kobylynskyi.graphql.codegen.model.builders.JavaDocBuilder; import com.kobylynskyi.graphql.codegen.model.definitions.ExtendedInputObjectTypeDefinition; import java.util.HashMap; +import java.util.List; import java.util.Map; import static com.kobylynskyi.graphql.codegen.model.DataModelFields.ANNOTATIONS; @@ -17,18 +19,21 @@ import static com.kobylynskyi.graphql.codegen.model.DataModelFields.GENERATED_INFO; import static com.kobylynskyi.graphql.codegen.model.DataModelFields.GENERATE_MODEL_OPEN_CLASSES; import static com.kobylynskyi.graphql.codegen.model.DataModelFields.IMMUTABLE_MODELS; +import static com.kobylynskyi.graphql.codegen.model.DataModelFields.INITIALIZE_NULLABLE_TYPES; import static com.kobylynskyi.graphql.codegen.model.DataModelFields.JAVA_DOC; import static com.kobylynskyi.graphql.codegen.model.DataModelFields.NAME; import static com.kobylynskyi.graphql.codegen.model.DataModelFields.PACKAGE; +import static com.kobylynskyi.graphql.codegen.model.DataModelFields.SUPPORT_UNKNOWN_FIELDS; import static com.kobylynskyi.graphql.codegen.model.DataModelFields.TO_STRING; import static com.kobylynskyi.graphql.codegen.model.DataModelFields.TO_STRING_FOR_REQUEST; +import static com.kobylynskyi.graphql.codegen.model.DataModelFields.UNKNOWN_FIELDS_PROPERTY_NAME; /** * Map input type definition to a Freemarker data model * * @author kobylynskyi */ -public class InputDefinitionToDataModelMapper { +public class InputDefinitionToDataModelMapper implements UnknownFieldsSupport { private final AnnotationsMapper annotationsMapper; private final DataModelMapper dataModelMapper; @@ -49,14 +54,17 @@ public InputDefinitionToDataModelMapper(MapperFactory mapperFactory, * @return Freemarker data model of the GraphQL type */ public Map map(MappingContext mappingContext, ExtendedInputObjectTypeDefinition definition) { + List fields = inputValueDefinitionToParameterMapper + .map(mappingContext, definition.getValueDefinitions(), definition.getName()); + createUnknownFields(mappingContext).ifPresent(fields::add); + Map dataModel = new HashMap<>(); // type/enum/input/interface/union classes do not require any imports dataModel.put(PACKAGE, DataModelMapper.getModelPackageName(mappingContext)); dataModel.put(CLASS_NAME, dataModelMapper.getModelClassNameWithPrefixAndSuffix(mappingContext, definition)); dataModel.put(JAVA_DOC, JavaDocBuilder.build(definition)); dataModel.put(NAME, definition.getName()); - dataModel.put(FIELDS, inputValueDefinitionToParameterMapper - .map(mappingContext, definition.getValueDefinitions(), definition.getName())); + dataModel.put(FIELDS, fields); dataModel.put(ANNOTATIONS, annotationsMapper.getAnnotations(mappingContext, definition)); dataModel.put(BUILDER, mappingContext.getGenerateBuilder()); dataModel.put(EQUALS_AND_HASH_CODE, mappingContext.getGenerateEqualsAndHashCode()); @@ -67,6 +75,9 @@ public Map map(MappingContext mappingContext, ExtendedInputObjec dataModel.put(GENERATED_INFO, mappingContext.getGeneratedInformation()); dataModel.put(ENUM_IMPORT_IT_SELF_IN_SCALA, mappingContext.getEnumImportItSelfInScala()); dataModel.put(GENERATE_MODEL_OPEN_CLASSES, mappingContext.isGenerateModelOpenClasses()); + dataModel.put(INITIALIZE_NULLABLE_TYPES, mappingContext.isInitializeNullableTypes()); + dataModel.put(SUPPORT_UNKNOWN_FIELDS, mappingContext.isSupportUnknownFields()); + dataModel.put(UNKNOWN_FIELDS_PROPERTY_NAME, mappingContext.getUnknownFieldsPropertyName()); return dataModel; } diff --git a/src/main/java/com/kobylynskyi/graphql/codegen/mapper/InterfaceDefinitionToDataModelMapper.java b/src/main/java/com/kobylynskyi/graphql/codegen/mapper/InterfaceDefinitionToDataModelMapper.java index e25e6cdc4..b21d18965 100644 --- a/src/main/java/com/kobylynskyi/graphql/codegen/mapper/InterfaceDefinitionToDataModelMapper.java +++ b/src/main/java/com/kobylynskyi/graphql/codegen/mapper/InterfaceDefinitionToDataModelMapper.java @@ -15,6 +15,7 @@ import static com.kobylynskyi.graphql.codegen.model.DataModelFields.FIELDS; import static com.kobylynskyi.graphql.codegen.model.DataModelFields.GENERATED_ANNOTATION; import static com.kobylynskyi.graphql.codegen.model.DataModelFields.GENERATED_INFO; +import static com.kobylynskyi.graphql.codegen.model.DataModelFields.GENERATE_SEALED_INTERFACES; import static com.kobylynskyi.graphql.codegen.model.DataModelFields.IMMUTABLE_MODELS; import static com.kobylynskyi.graphql.codegen.model.DataModelFields.IMPLEMENTS; import static com.kobylynskyi.graphql.codegen.model.DataModelFields.JAVA_DOC; @@ -63,6 +64,7 @@ public Map map(MappingContext mappingContext, ExtendedInterfaceT dataModel.put(ENUM_IMPORT_IT_SELF_IN_SCALA, mappingContext.getEnumImportItSelfInScala()); dataModel.put(IMMUTABLE_MODELS, mappingContext.getGenerateImmutableModels()); dataModel.put(PARENT_INTERFACE_PROPERTIES, mappingContext.getParentInterfaceProperties()); + dataModel.put(GENERATE_SEALED_INTERFACES, mappingContext.isGenerateSealedInterfaces()); return dataModel; } diff --git a/src/main/java/com/kobylynskyi/graphql/codegen/mapper/RequestResponseDefinitionToDataModelMapper.java b/src/main/java/com/kobylynskyi/graphql/codegen/mapper/RequestResponseDefinitionToDataModelMapper.java index 39bf24ff7..ade4b4f0e 100644 --- a/src/main/java/com/kobylynskyi/graphql/codegen/mapper/RequestResponseDefinitionToDataModelMapper.java +++ b/src/main/java/com/kobylynskyi/graphql/codegen/mapper/RequestResponseDefinitionToDataModelMapper.java @@ -29,6 +29,7 @@ import static com.kobylynskyi.graphql.codegen.model.DataModelFields.GENERATED_ANNOTATION; import static com.kobylynskyi.graphql.codegen.model.DataModelFields.GENERATED_INFO; import static com.kobylynskyi.graphql.codegen.model.DataModelFields.GENERATE_ALL_METHOD_IN_PROJECTION; +import static com.kobylynskyi.graphql.codegen.model.DataModelFields.GENERATE_SEALED_INTERFACES; import static com.kobylynskyi.graphql.codegen.model.DataModelFields.JAVA_DOC; import static com.kobylynskyi.graphql.codegen.model.DataModelFields.METHOD_NAME; import static com.kobylynskyi.graphql.codegen.model.DataModelFields.OPERATION_NAME; diff --git a/src/main/java/com/kobylynskyi/graphql/codegen/mapper/TypeDefinitionToDataModelMapper.java b/src/main/java/com/kobylynskyi/graphql/codegen/mapper/TypeDefinitionToDataModelMapper.java index 5760ad4eb..8936d5f30 100644 --- a/src/main/java/com/kobylynskyi/graphql/codegen/mapper/TypeDefinitionToDataModelMapper.java +++ b/src/main/java/com/kobylynskyi/graphql/codegen/mapper/TypeDefinitionToDataModelMapper.java @@ -26,20 +26,24 @@ import static com.kobylynskyi.graphql.codegen.model.DataModelFields.GENERATED_ANNOTATION; import static com.kobylynskyi.graphql.codegen.model.DataModelFields.GENERATED_INFO; import static com.kobylynskyi.graphql.codegen.model.DataModelFields.GENERATE_MODEL_OPEN_CLASSES; +import static com.kobylynskyi.graphql.codegen.model.DataModelFields.GENERATE_SEALED_INTERFACES; import static com.kobylynskyi.graphql.codegen.model.DataModelFields.IMMUTABLE_MODELS; import static com.kobylynskyi.graphql.codegen.model.DataModelFields.IMPLEMENTS; +import static com.kobylynskyi.graphql.codegen.model.DataModelFields.INITIALIZE_NULLABLE_TYPES; import static com.kobylynskyi.graphql.codegen.model.DataModelFields.JAVA_DOC; import static com.kobylynskyi.graphql.codegen.model.DataModelFields.PACKAGE; import static com.kobylynskyi.graphql.codegen.model.DataModelFields.PARENT_INTERFACE_PROPERTIES; +import static com.kobylynskyi.graphql.codegen.model.DataModelFields.SUPPORT_UNKNOWN_FIELDS; import static com.kobylynskyi.graphql.codegen.model.DataModelFields.TO_STRING; import static com.kobylynskyi.graphql.codegen.model.DataModelFields.TO_STRING_FOR_REQUEST; +import static com.kobylynskyi.graphql.codegen.model.DataModelFields.UNKNOWN_FIELDS_PROPERTY_NAME; /** * Map type definition to a Freemarker data model * * @author kobylynskyi */ -public class TypeDefinitionToDataModelMapper { +public class TypeDefinitionToDataModelMapper implements UnknownFieldsSupport { private final GraphQLTypeMapper graphQLTypeMapper; private final AnnotationsMapper annotationsMapper; @@ -102,6 +106,10 @@ public Map map(MappingContext mappingContext, dataModel.put(ENUM_IMPORT_IT_SELF_IN_SCALA, mappingContext.getEnumImportItSelfInScala()); dataModel.put(PARENT_INTERFACE_PROPERTIES, mappingContext.getParentInterfaceProperties()); dataModel.put(GENERATE_MODEL_OPEN_CLASSES, mappingContext.isGenerateModelOpenClasses()); + dataModel.put(INITIALIZE_NULLABLE_TYPES, mappingContext.isInitializeNullableTypes()); + dataModel.put(GENERATE_SEALED_INTERFACES, mappingContext.isGenerateSealedInterfaces()); + dataModel.put(SUPPORT_UNKNOWN_FIELDS, mappingContext.isSupportUnknownFields()); + dataModel.put(UNKNOWN_FIELDS_PROPERTY_NAME, mappingContext.getUnknownFieldsPropertyName()); return dataModel; } @@ -128,6 +136,12 @@ private Collection getFields(MappingContext mappingContext, .flatMap(Collection::stream) .forEach(paramDef -> allParameters .merge(paramDef.getName(), paramDef, TypeDefinitionToDataModelMapper::merge)); + + + createUnknownFields(mappingContext).ifPresent( + unknownFields -> allParameters.put(mappingContext.getUnknownFieldsPropertyName(), unknownFields) + ); + return allParameters.values(); } diff --git a/src/main/java/com/kobylynskyi/graphql/codegen/mapper/UnionDefinitionToDataModelMapper.java b/src/main/java/com/kobylynskyi/graphql/codegen/mapper/UnionDefinitionToDataModelMapper.java index a0ea2c4f4..5b94a632a 100644 --- a/src/main/java/com/kobylynskyi/graphql/codegen/mapper/UnionDefinitionToDataModelMapper.java +++ b/src/main/java/com/kobylynskyi/graphql/codegen/mapper/UnionDefinitionToDataModelMapper.java @@ -12,6 +12,7 @@ import static com.kobylynskyi.graphql.codegen.model.DataModelFields.ENUM_IMPORT_IT_SELF_IN_SCALA; import static com.kobylynskyi.graphql.codegen.model.DataModelFields.GENERATED_ANNOTATION; import static com.kobylynskyi.graphql.codegen.model.DataModelFields.GENERATED_INFO; +import static com.kobylynskyi.graphql.codegen.model.DataModelFields.GENERATE_SEALED_INTERFACES; import static com.kobylynskyi.graphql.codegen.model.DataModelFields.JAVA_DOC; import static com.kobylynskyi.graphql.codegen.model.DataModelFields.PACKAGE; @@ -47,6 +48,7 @@ public Map map(MappingContext mappingContext, ExtendedUnionTypeD dataModel.put(GENERATED_ANNOTATION, mappingContext.getAddGeneratedAnnotation()); dataModel.put(GENERATED_INFO, mappingContext.getGeneratedInformation()); dataModel.put(ENUM_IMPORT_IT_SELF_IN_SCALA, mappingContext.getEnumImportItSelfInScala()); + dataModel.put(GENERATE_SEALED_INTERFACES, mappingContext.isGenerateSealedInterfaces()); return dataModel; } diff --git a/src/main/java/com/kobylynskyi/graphql/codegen/mapper/UnknownFieldsSupport.java b/src/main/java/com/kobylynskyi/graphql/codegen/mapper/UnknownFieldsSupport.java new file mode 100644 index 000000000..9618ec645 --- /dev/null +++ b/src/main/java/com/kobylynskyi/graphql/codegen/mapper/UnknownFieldsSupport.java @@ -0,0 +1,40 @@ +package com.kobylynskyi.graphql.codegen.mapper; + +import com.kobylynskyi.graphql.codegen.model.MappingContext; +import com.kobylynskyi.graphql.codegen.model.ParameterDefinition; + +import java.util.Arrays; +import java.util.Optional; + +/** + * Utility interface that provides convenience methods to handle unknown fields during the marshaling + * and unmarshalling of a JSON document + * + * @author aldib + */ +public interface UnknownFieldsSupport { + + /** + * Creates an instance of {@link ParameterDefinition} that can be used to generate + * a field of type {@link java.util.Map} to store unknown fields during the marshaling + * and unmarshalling of a JSON document + * + * @param mappingContext The context of the mapping process. + * @return If {@link MappingContext#isSupportUnknownFields()} is true, it returns a monad containing + * the instance of {@link ParameterDefinition}. {@link Optional#empty()} otherwise. + */ + default Optional createUnknownFields(MappingContext mappingContext) { + if (mappingContext.isSupportUnknownFields()) { + ParameterDefinition unknownFields = new ParameterDefinition(); + unknownFields.setName(mappingContext.getUnknownFieldsPropertyName()); + unknownFields.setOriginalName(mappingContext.getUnknownFieldsPropertyName()); + unknownFields.setType("java.util.Map"); + unknownFields.setAnnotations(Arrays.asList( + "com.fasterxml.jackson.annotation.JsonAnyGetter", + "com.fasterxml.jackson.annotation.JsonAnySetter" + )); + return Optional.of(unknownFields); + } + return Optional.empty(); + } +} diff --git a/src/main/java/com/kobylynskyi/graphql/codegen/model/DataModelFields.java b/src/main/java/com/kobylynskyi/graphql/codegen/model/DataModelFields.java index df670b27b..bfe2f95f9 100644 --- a/src/main/java/com/kobylynskyi/graphql/codegen/model/DataModelFields.java +++ b/src/main/java/com/kobylynskyi/graphql/codegen/model/DataModelFields.java @@ -36,6 +36,10 @@ public final class DataModelFields { public static final String PARENT_INTERFACE_PROPERTIES = "parentInterfaceProperties"; public static final String SERIALIZATION_LIBRARY = "serializationLibrary"; public static final String GENERATE_MODEL_OPEN_CLASSES = "generateModelOpenClasses"; + public static final String INITIALIZE_NULLABLE_TYPES = "initializeNullableTypes"; + public static final String GENERATE_SEALED_INTERFACES = "generateSealedInterfaces"; + public static final String SUPPORT_UNKNOWN_FIELDS = "supportUnknownFields"; + public static final String UNKNOWN_FIELDS_PROPERTY_NAME = "unknownFieldsPropertyName"; private DataModelFields() { } diff --git a/src/main/java/com/kobylynskyi/graphql/codegen/model/GraphQLCodegenConfiguration.java b/src/main/java/com/kobylynskyi/graphql/codegen/model/GraphQLCodegenConfiguration.java index 81ca953de..efc0c4a5f 100644 --- a/src/main/java/com/kobylynskyi/graphql/codegen/model/GraphQLCodegenConfiguration.java +++ b/src/main/java/com/kobylynskyi/graphql/codegen/model/GraphQLCodegenConfiguration.java @@ -446,4 +446,37 @@ public interface GraphQLCodegenConfiguration { */ Boolean isGenerateModelOpenClasses(); + /** + * Specifies whether classes should be generated with constructors setting the + * default value for nullable fields to null. + * + * @return true if nullable fields should be defaulted to null. + */ + Boolean isInitializeNullableTypes(); + + /** + * Specifies whether generate sealed interfaces. + * + * @return false generate sealed interfaces class in kotlin + */ + Boolean isGenerateSealedInterfaces(); + + /* + * Specifies whether api classes should support unknown fields during serialization or deserialization. + * + * @return true classes will include a property of type {@link java.util.Map} that will store unknown fields. + * @see com.fasterxml.jackson.annotation.JsonAnyGetter + * @see com.fasterxml.jackson.annotation.JsonAnySetter + */ + Boolean isSupportUnknownFields(); + + /** + * Specifies the name of the property to be included in api classes to support unknown + * fields during serialization or deserialization + * + * @return The name of the property to store unknown fields + */ + String getUnknownFieldsPropertyName(); + + } diff --git a/src/main/java/com/kobylynskyi/graphql/codegen/model/MappingConfig.java b/src/main/java/com/kobylynskyi/graphql/codegen/model/MappingConfig.java index 0a337d299..128901e52 100644 --- a/src/main/java/com/kobylynskyi/graphql/codegen/model/MappingConfig.java +++ b/src/main/java/com/kobylynskyi/graphql/codegen/model/MappingConfig.java @@ -39,6 +39,7 @@ public class MappingConfig implements GraphQLCodegenConfiguration, Combinable fieldsWithResolvers = new HashSet<>(); @@ -78,6 +80,8 @@ public class MappingConfig implements GraphQLCodegenConfiguration, Combinable typesAsInterfaces = new HashSet<>(); private boolean generateModelOpenClasses; + private boolean initializeNullableTypes; + private boolean generateSealedInterfaces; private GeneratedLanguage generatedLanguage; @@ -185,6 +189,14 @@ public void combine(MappingConfig source) { generatedLanguage = getValueOrDefaultToThis(source, GraphQLCodegenConfiguration::getGeneratedLanguage); generateModelOpenClasses = getValueOrDefaultToThis(source, GraphQLCodegenConfiguration::isGenerateModelOpenClasses); + initializeNullableTypes = getValueOrDefaultToThis(source, + GraphQLCodegenConfiguration::isInitializeNullableTypes); + generateSealedInterfaces = getValueOrDefaultToThis(source, + GraphQLCodegenConfiguration::isGenerateSealedInterfaces); + supportUnknownFields = getValueOrDefaultToThis(source, + GraphQLCodegenConfiguration::isSupportUnknownFields); + unknownFieldsPropertyName = getValueOrDefaultToThis(source, + GraphQLCodegenConfiguration::getUnknownFieldsPropertyName); } private T getValueOrDefaultToThis(MappingConfig source, Function getValueFunction) { @@ -643,11 +655,28 @@ public GeneratedLanguage getGeneratedLanguage() { return generatedLanguage; } + @Override + public String getUnknownFieldsPropertyName() { + return unknownFieldsPropertyName; + } + + public void setUnknownFieldsPropertyName(String unknownFieldsPropertyName) { + this.unknownFieldsPropertyName = unknownFieldsPropertyName; + } + + @Override + public Boolean isSupportUnknownFields() { + return supportUnknownFields; + } + + public void setSupportUnknownFields(Boolean supportUnknownFields) { + this.supportUnknownFields = supportUnknownFields; + } + public void setGeneratedLanguage(GeneratedLanguage generatedLanguage) { this.generatedLanguage = generatedLanguage; } - public Boolean isGenerateModelOpenClasses() { return generateModelOpenClasses; } @@ -656,4 +685,20 @@ public void setGenerateModelOpenClasses(boolean generateModelOpenClasses) { this.generateModelOpenClasses = generateModelOpenClasses; } + public Boolean isInitializeNullableTypes() { + return initializeNullableTypes; + } + + public void setInitializeNullableTypes(boolean initializeNullableTypes) { + this.initializeNullableTypes = initializeNullableTypes; + } + + public Boolean isGenerateSealedInterfaces() { + return generateSealedInterfaces; + } + + public void setGenerateSealedInterfaces(boolean generateSealedInterfaces) { + this.generateSealedInterfaces = generateSealedInterfaces; + } + } diff --git a/src/main/java/com/kobylynskyi/graphql/codegen/model/MappingConfigConstants.java b/src/main/java/com/kobylynskyi/graphql/codegen/model/MappingConfigConstants.java index 68197d007..df3499cde 100644 --- a/src/main/java/com/kobylynskyi/graphql/codegen/model/MappingConfigConstants.java +++ b/src/main/java/com/kobylynskyi/graphql/codegen/model/MappingConfigConstants.java @@ -68,6 +68,16 @@ public class MappingConfigConstants { public static final boolean DEFAULT_GENERATE_MODEL_OPEN_CLASSES = false; public static final String DEFAULT_GENERATE_MODEL_OPEN_CLASSES_STRING = "false"; + //Only supported in kotlin. + public static final boolean DEFAULT_INITIALIZE_NULLABLE_TYPES = false; + public static final String DEFAULT_INITIALIZE_NULLABLE_TYPES_STRING = "false"; + // Only supported in Kotlin + public static final boolean DEFAULT_GENERATE_SEALED_INTERFACES = false; + public static final String DEFAULT_GENERATE_SEALED_INTERFACES_STRING = "false"; + + public static final boolean DEFAULT_SUPPORT_UNKNOWN_FIELDS = false; + public static final String DEFAULT_SUPPORT_UNKNOWN_FIELDS_STRING = "false"; + public static final String DEFAULT_UNKNOWN_FIELDS_PROPERTY_NAME = "userDefinedFields"; private MappingConfigConstants() { } diff --git a/src/main/java/com/kobylynskyi/graphql/codegen/model/MappingConfigDefaultValuesInitializer.java b/src/main/java/com/kobylynskyi/graphql/codegen/model/MappingConfigDefaultValuesInitializer.java index 1aa446a02..57f11b675 100644 --- a/src/main/java/com/kobylynskyi/graphql/codegen/model/MappingConfigDefaultValuesInitializer.java +++ b/src/main/java/com/kobylynskyi/graphql/codegen/model/MappingConfigDefaultValuesInitializer.java @@ -105,6 +105,15 @@ public static void initDefaultValues(MappingConfig mappingConfig) { if (mappingConfig.getGenerateAllMethodInProjection() == null) { mappingConfig.setGenerateAllMethodInProjection(MappingConfigConstants.DEFAULT_GENERATE_ALL_METHOD); } + if (mappingConfig.isGenerateSealedInterfaces() == null) { + mappingConfig.setGenerateSealedInterfaces(MappingConfigConstants.DEFAULT_GENERATE_SEALED_INTERFACES); + } + if (mappingConfig.isSupportUnknownFields() == null) { + mappingConfig.setSupportUnknownFields(MappingConfigConstants.DEFAULT_SUPPORT_UNKNOWN_FIELDS); + } + if (mappingConfig.getUnknownFieldsPropertyName() == null) { + mappingConfig.setUnknownFieldsPropertyName(MappingConfigConstants.DEFAULT_UNKNOWN_FIELDS_PROPERTY_NAME); + } } } diff --git a/src/main/java/com/kobylynskyi/graphql/codegen/model/MappingContext.java b/src/main/java/com/kobylynskyi/graphql/codegen/model/MappingContext.java index 10d452918..8b780d4d5 100644 --- a/src/main/java/com/kobylynskyi/graphql/codegen/model/MappingContext.java +++ b/src/main/java/com/kobylynskyi/graphql/codegen/model/MappingContext.java @@ -57,6 +57,18 @@ public Boolean isGenerateModelOpenClasses() { return config.isGenerateModelOpenClasses(); } + @Override + public Boolean isInitializeNullableTypes() { + return config.isInitializeNullableTypes(); + } + + + + @Override + public Boolean isGenerateSealedInterfaces() { + return config.isGenerateSealedInterfaces(); + } + @Override public Map getCustomTypesMapping() { return config.getCustomTypesMapping(); @@ -297,6 +309,16 @@ public Set getTypesAsInterfaces() { return config.getTypesAsInterfaces(); } + @Override + public Boolean isSupportUnknownFields() { + return config.isSupportUnknownFields(); + } + + @Override + public String getUnknownFieldsPropertyName() { + return config.getUnknownFieldsPropertyName(); + } + public ExtendedDocument getDocument() { return document; } diff --git a/src/main/java/com/kobylynskyi/graphql/codegen/model/ParameterDefinition.java b/src/main/java/com/kobylynskyi/graphql/codegen/model/ParameterDefinition.java index 98d4a5290..435111613 100644 --- a/src/main/java/com/kobylynskyi/graphql/codegen/model/ParameterDefinition.java +++ b/src/main/java/com/kobylynskyi/graphql/codegen/model/ParameterDefinition.java @@ -31,6 +31,10 @@ public class ParameterDefinition { private List javaDoc = new ArrayList<>(); private DeprecatedDefinition deprecated; private boolean serializeUsingObjectMapper; + /** + * If the type is parametrized then input parameters will be defined here + */ + private List inputParameters; /** * Definition of the same type, but defined in the parent */ @@ -124,4 +128,12 @@ public ParameterDefinition getDefinitionInParentType() { public void setDefinitionInParentType(ParameterDefinition definitionInParentType) { this.definitionInParentType = definitionInParentType; } + + public List getInputParameters() { + return inputParameters; + } + + public void setInputParameters(List inputParameters) { + this.inputParameters = inputParameters; + } } diff --git a/src/main/resources/templates/java-lang/interface.ftl b/src/main/resources/templates/java-lang/interface.ftl index 4d55a9a2e..f3957e0b0 100644 --- a/src/main/resources/templates/java-lang/interface.ftl +++ b/src/main/resources/templates/java-lang/interface.ftl @@ -41,7 +41,7 @@ public interface ${className} <#if implements?has_content>extends <#list impleme <#list field.annotations as annotation> @${annotation} - ${field.type} get${field.name?cap_first}(); + ${field.type} get${field.name?cap_first}(<#list field.inputParameters as param><#list param.annotations as paramAnnotation>@${paramAnnotation}<#if param.annotations?has_content> ${param.type} ${param.name}<#if param_has_next>, ); diff --git a/src/main/resources/templates/java-lang/type.ftl b/src/main/resources/templates/java-lang/type.ftl index 242764d01..8b3e56187 100644 --- a/src/main/resources/templates/java-lang/type.ftl +++ b/src/main/resources/templates/java-lang/type.ftl @@ -36,6 +36,8 @@ import java.util.StringJoiner; public class ${className} implements java.io.Serializable<#if implements?has_content><#list implements as interface>, ${interface}<#if interface_has_next> { + private static final long serialVersionUID = 1L; + <#if fields?has_content> <#list fields as field> <#if field.deprecated?has_content> diff --git a/src/main/resources/templates/kotlin-lang/interface.ftl b/src/main/resources/templates/kotlin-lang/interface.ftl index 4c512e152..a5f54201e 100755 --- a/src/main/resources/templates/kotlin-lang/interface.ftl +++ b/src/main/resources/templates/kotlin-lang/interface.ftl @@ -46,7 +46,7 @@ import ${import}.* <#list annotations as annotation> @${annotation} -interface ${className}<#if implements?has_content> : <#list implements as interface>${interface}<#if interface_has_next>, { +<#if generateSealedInterfaces>sealed interface ${className}<#if implements?has_content> : <#list implements as interface>${interface}<#if interface_has_next>, { <#if fields?has_content> <#list fields as field> diff --git a/src/main/resources/templates/kotlin-lang/request.ftl b/src/main/resources/templates/kotlin-lang/request.ftl index 7354edda1..99b0f5ea5 100755 --- a/src/main/resources/templates/kotlin-lang/request.ftl +++ b/src/main/resources/templates/kotlin-lang/request.ftl @@ -107,7 +107,7 @@ open class ${className}(private val alias: String?) : GraphQLOperationRequest { <#else> <#if MapperUtil.isKotlinPrimitive(field.type)> <#assign default = MapperUtil.defaultValueKotlinPrimitive(field.type)/> - private var ${field.name}: ${field.type} = default + private var ${field.name}: ${field.type} = ${default} <#else> private lateinit var ${field.name}: ${field.type} diff --git a/src/main/resources/templates/kotlin-lang/type.ftl b/src/main/resources/templates/kotlin-lang/type.ftl index bf4c000a2..4ef1f4c8d 100755 --- a/src/main/resources/templates/kotlin-lang/type.ftl +++ b/src/main/resources/templates/kotlin-lang/type.ftl @@ -68,7 +68,7 @@ open class ${className}()<#if implements?has_content> : <#list implements as int <#if parentInterfaces?has_content><#list parentInterfaces as parent><#if parent == field.name>override <#if !immutableModels><#list field.annotations as annotation>@get:${annotation} var <#else><#list field.annotations as annotation>@get:${annotation} - val ${field.name}: ${field.type}<#if field.defaultValue?has_content> = ${field.defaultValue}<#if field_has_next>, + val ${field.name}: ${field.type}<#if field.defaultValue?has_content> = ${field.defaultValue}<#elseif field.type?ends_with("?") && (initializeNullableTypes == true)> = null<#if field_has_next>, )<#if implements?has_content> : <#list implements as interface>${interface}<#if interface_has_next>, { diff --git a/src/main/resources/templates/kotlin-lang/union.ftl b/src/main/resources/templates/kotlin-lang/union.ftl index c10c72156..c4a9ea594 100755 --- a/src/main/resources/templates/kotlin-lang/union.ftl +++ b/src/main/resources/templates/kotlin-lang/union.ftl @@ -19,6 +19,6 @@ package ${package} <#list annotations as annotation> @${annotation} -interface ${className} { +<#if generateSealedInterfaces>sealed interface ${className} { } diff --git a/src/test/java/com/kobylynskyi/graphql/codegen/GraphQLCodegenDefaultsTest.java b/src/test/java/com/kobylynskyi/graphql/codegen/GraphQLCodegenDefaultsTest.java index d64c706ac..900e8aad4 100644 --- a/src/test/java/com/kobylynskyi/graphql/codegen/GraphQLCodegenDefaultsTest.java +++ b/src/test/java/com/kobylynskyi/graphql/codegen/GraphQLCodegenDefaultsTest.java @@ -53,6 +53,25 @@ void generate_CheckFiles() throws Exception { } } + @Test + void generate_UnkonwnFields() throws Exception { + mappingConfig.setSupportUnknownFields(true); + mappingConfig.setUnknownFieldsPropertyName("userDefinedFields"); + + new JavaGraphQLCodegen(singletonList("src/test/resources/schemas/defaults.graphqls"), + outputBuildDir, mappingConfig, TestUtils.getStaticGeneratedInfo()).generate(); + + File[] files = Objects.requireNonNull(outputJavaClassesDir.listFiles()); + List generatedFileNames = Arrays.stream(files).map(File::getName).sorted().collect(toList()); + assertEquals(asList("InputWithDefaults.java", "MyEnum.java", "SomeObject.java"), generatedFileNames); + + for (File file : files) { + assertSameTrimmedContent(new File(String.format("src/test/resources/expected-classes/unknown-fields/%s.txt", + file.getName())), + file); + } + } + @Test void generate_CheckFiles_WithPrefixSuffix() throws Exception { mappingConfig.setModelNameSuffix("TO"); diff --git a/src/test/java/com/kobylynskyi/graphql/codegen/GraphQLCodegenTypesAsInterfacesTest.java b/src/test/java/com/kobylynskyi/graphql/codegen/GraphQLCodegenTypesAsInterfacesTest.java index 91d063ec6..5eeff536d 100644 --- a/src/test/java/com/kobylynskyi/graphql/codegen/GraphQLCodegenTypesAsInterfacesTest.java +++ b/src/test/java/com/kobylynskyi/graphql/codegen/GraphQLCodegenTypesAsInterfacesTest.java @@ -8,13 +8,13 @@ import org.junit.jupiter.api.Test; import java.io.File; -import java.util.Collections; import java.util.HashSet; import java.util.Objects; import static com.kobylynskyi.graphql.codegen.TestUtils.assertSameTrimmedContent; import static com.kobylynskyi.graphql.codegen.TestUtils.getFileByName; import static java.util.Arrays.asList; +import static java.util.Collections.singleton; import static java.util.Collections.singletonList; class GraphQLCodegenTypesAsInterfacesTest { @@ -26,7 +26,9 @@ class GraphQLCodegenTypesAsInterfacesTest { @BeforeEach void init() { mappingConfig.setPackageName("com.github.graphql"); - mappingConfig.setFieldsWithResolvers(Collections.singleton("@customResolver")); + mappingConfig.setFieldsWithResolvers(singleton("@customResolver")); + mappingConfig.setFieldsWithoutResolvers(singleton("@noResolver")); + mappingConfig.setTypesAsInterfaces(new HashSet<>(singleton("@asInterface"))); } @AfterEach @@ -36,7 +38,7 @@ void cleanup() { @Test void generate_typesAsInterfaces() throws Exception { - mappingConfig.setTypesAsInterfaces(new HashSet<>(asList("@asInterface", "Order"))); + mappingConfig.getTypesAsInterfaces().add("Order"); new JavaGraphQLCodegen(singletonList("src/test/resources/schemas/types-as-interfaces.graphqls"), outputBuildDir, mappingConfig, TestUtils.getStaticGeneratedInfo()).generate(); @@ -68,16 +70,31 @@ void generate_typesAsInterfacesExtendsInterface() throws Exception { assertSameTrimmedContent(new File("src/test/resources/expected-classes/" + "types-as-interfaces-extends-interface/Node.java.txt"), getFileByName(files, "Node.java")); assertSameTrimmedContent(new File("src/test/resources/expected-classes/" + - "types-as-interfaces-extends-interface/Profile.java.txt"), + "types-as-interfaces-extends-interface/Profile.java.txt"), getFileByName(files, "Profile.java")); assertSameTrimmedContent(new File("src/test/resources/expected-classes/" + - "types-as-interfaces-extends-interface/QueryResolver.java.txt"), + "types-as-interfaces-extends-interface/QueryResolver.java.txt"), getFileByName(files, "QueryResolver.java")); assertSameTrimmedContent(new File("src/test/resources/expected-classes/" + "types-as-interfaces-extends-interface/User.java.txt"), getFileByName(files, "User.java")); assertSameTrimmedContent(new File("src/test/resources/expected-classes/" + - "types-as-interfaces-extends-interface/UserCurrentQueryResolver.java.txt"), + "types-as-interfaces-extends-interface/UserCurrentQueryResolver.java.txt"), getFileByName(files, "UserCurrentQueryResolver.java")); } + @Test + void generate_typeAsInterfaceParametrized() throws Exception { + mappingConfig.setGenerateParameterizedFieldsResolvers(true); + + new JavaGraphQLCodegen(singletonList("src/test/resources/schemas/" + + "types-as-interfaces-parametrized.graphqls"), + outputBuildDir, mappingConfig, TestUtils.getStaticGeneratedInfo()).generate(); + + File[] files = Objects.requireNonNull(outputJavaClassesDir.listFiles()); + + assertSameTrimmedContent(new File( + "src/test/resources/expected-classes/types-as-interfaces-parametrized/Foo.java.txt"), + getFileByName(files, "Foo.java")); + } + } diff --git a/src/test/java/com/kobylynskyi/graphql/codegen/kotlin/GraphQLCodegenGitHubTest.java b/src/test/java/com/kobylynskyi/graphql/codegen/kotlin/GraphQLCodegenGitHubTest.java index 9a3672ab0..3d3040c50 100644 --- a/src/test/java/com/kobylynskyi/graphql/codegen/kotlin/GraphQLCodegenGitHubTest.java +++ b/src/test/java/com/kobylynskyi/graphql/codegen/kotlin/GraphQLCodegenGitHubTest.java @@ -19,6 +19,7 @@ import static java.util.Collections.singletonList; import static org.hamcrest.MatcherAssert.assertThat; + class GraphQLCodegenGitHubTest { private final File outputBuildDir = new File("build/generated"); @@ -187,4 +188,16 @@ void generate_CustomFieldsResolvers() throws Exception { getFileByName(files, "AcceptTopicSuggestionPayloadResolver.kt")); } + @Test + void generate_RequestWithDefaultValue() throws Exception { + mappingConfig.setGenerateBuilder(true); + mappingConfig.setGenerateClient(true); + new KotlinGraphQLCodegen(singletonList("src/test/resources/schemas/kt/default.graphqls"), + outputBuildDir, mappingConfig, TestUtils.getStaticGeneratedInfo()).generate(); + File[] files = Objects.requireNonNull(outputktClassesDir.listFiles()); + assertSameTrimmedContent(new File("src/test/resources/expected-classes/kt/default/" + + "FriendsQueryRequest.kt.txt"), + getFileByName(files, "FriendsQueryRequest.kt")); + } + } \ No newline at end of file diff --git a/src/test/java/com/kobylynskyi/graphql/codegen/kotlin/GraphQLCodegenInitializeNullableTypesTest.java b/src/test/java/com/kobylynskyi/graphql/codegen/kotlin/GraphQLCodegenInitializeNullableTypesTest.java new file mode 100644 index 000000000..ee4460dc6 --- /dev/null +++ b/src/test/java/com/kobylynskyi/graphql/codegen/kotlin/GraphQLCodegenInitializeNullableTypesTest.java @@ -0,0 +1,53 @@ +package com.kobylynskyi.graphql.codegen.kotlin; + +import com.kobylynskyi.graphql.codegen.TestUtils; +import com.kobylynskyi.graphql.codegen.model.GeneratedLanguage; +import com.kobylynskyi.graphql.codegen.model.MappingConfig; +import com.kobylynskyi.graphql.codegen.utils.Utils; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.io.File; +import java.util.Objects; + +import static com.kobylynskyi.graphql.codegen.TestUtils.assertSameTrimmedContent; +import static com.kobylynskyi.graphql.codegen.TestUtils.getFileByName; +import static java.util.Collections.singletonList; + +class GraphQLCodegenInitializeNullableTypesTest { + + private final File outputBuildDir = new File("build/generated"); + private final File outputScalaClassesDir = new File("build/generated/com/github/graphql"); + private final MappingConfig mappingConfig = new MappingConfig(); + + @BeforeEach + void init() { + mappingConfig.setGenerateParameterizedFieldsResolvers(false); + mappingConfig.setPackageName("com.github.graphql"); + mappingConfig.setGeneratedLanguage(GeneratedLanguage.KOTLIN); + mappingConfig.setGenerateToString(true); + mappingConfig.setGenerateApis(true); + mappingConfig.setGenerateClient(true); + mappingConfig.setGenerateEqualsAndHashCode(true); + mappingConfig.setInitializeNullableTypes(true); + mappingConfig.setGenerateBuilder(true); + } + + @AfterEach + void cleanup() { + Utils.deleteDir(outputBuildDir); + } + + @Test + void generate_InitializeNullableTypes() throws Exception { + new KotlinGraphQLCodegen(singletonList("src/test/resources/schemas/github.graphqls"), + outputBuildDir, mappingConfig, TestUtils.getStaticGeneratedInfo()).generate(); + File[] files = Objects.requireNonNull(outputScalaClassesDir.listFiles()); + + assertSameTrimmedContent( + new File("src/test/resources/expected-classes/kt/Commit_initialize_nullable_types.kt.txt"), + getFileByName(files, "Commit.kt")); + } + +} diff --git a/src/test/java/com/kobylynskyi/graphql/codegen/kotlin/GraphQLCodegenSealedInterfacesTest.java b/src/test/java/com/kobylynskyi/graphql/codegen/kotlin/GraphQLCodegenSealedInterfacesTest.java new file mode 100644 index 000000000..0fb0f4b03 --- /dev/null +++ b/src/test/java/com/kobylynskyi/graphql/codegen/kotlin/GraphQLCodegenSealedInterfacesTest.java @@ -0,0 +1,50 @@ +package com.kobylynskyi.graphql.codegen.kotlin; + +import com.kobylynskyi.graphql.codegen.TestUtils; +import com.kobylynskyi.graphql.codegen.model.GeneratedLanguage; +import com.kobylynskyi.graphql.codegen.model.MappingConfig; +import com.kobylynskyi.graphql.codegen.utils.Utils; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.io.File; +import java.util.Objects; + +import static com.kobylynskyi.graphql.codegen.TestUtils.assertSameTrimmedContent; +import static com.kobylynskyi.graphql.codegen.TestUtils.getFileByName; +import static java.util.Collections.singletonList; + +class GraphQLCodegenSealedInterfacesTest { + private final File outputBuildDir = new File("build/generated"); + private final File outputScalaClassesDir = new File("build/generated/com/github/graphql"); + private final MappingConfig mappingConfig = new MappingConfig(); + + @BeforeEach + void init() { + mappingConfig.setGenerateParameterizedFieldsResolvers(false); + mappingConfig.setPackageName("com.github.graphql"); + mappingConfig.setGeneratedLanguage(GeneratedLanguage.KOTLIN); + mappingConfig.setGenerateToString(true); + mappingConfig.setGenerateApis(true); + mappingConfig.setGenerateClient(true); + mappingConfig.setGenerateEqualsAndHashCode(true); + mappingConfig.setGenerateBuilder(true); + mappingConfig.setGenerateSealedInterfaces(true); + } + + @AfterEach + void cleanup() { + Utils.deleteDir(outputBuildDir); + } + + @Test + void generate_MultipleInterfacesPerType() throws Exception { + new KotlinGraphQLCodegen(singletonList("src/test/resources/schemas/github.graphqls"), + outputBuildDir, mappingConfig, TestUtils.getStaticGeneratedInfo()).generate(); + File[] files = Objects.requireNonNull(outputScalaClassesDir.listFiles()); + + assertSameTrimmedContent(new File("src/test/resources/expected-classes/kt/Comment_sealed_interfaces.kt.txt"), + getFileByName(files, "Comment.kt")); + } +} diff --git a/src/test/resources/expected-classes/CommentDeletedEvent.java.txt b/src/test/resources/expected-classes/CommentDeletedEvent.java.txt index 788076193..f24977a86 100644 --- a/src/test/resources/expected-classes/CommentDeletedEvent.java.txt +++ b/src/test/resources/expected-classes/CommentDeletedEvent.java.txt @@ -7,6 +7,8 @@ package com.github.graphql; ) public class CommentDeletedEvent implements java.io.Serializable, IssueTimelineItems, PullRequestTimelineItems, Node { + private static final long serialVersionUID = 1L; + @javax.validation.constraints.NotNull private String id; @@ -48,4 +50,4 @@ public class CommentDeletedEvent implements java.io.Serializable, IssueTimelineI } } -} \ No newline at end of file +} diff --git a/src/test/resources/expected-classes/Commit.java.txt b/src/test/resources/expected-classes/Commit.java.txt index 627b2bd0d..2cfc3ea36 100644 --- a/src/test/resources/expected-classes/Commit.java.txt +++ b/src/test/resources/expected-classes/Commit.java.txt @@ -7,6 +7,8 @@ package com.github.graphql; ) public class Commit implements java.io.Serializable, Closer, IssueTimelineItem, PullRequestTimelineItem, Subscribable, Node, GitObject, UniformResourceLocatable { + private static final long serialVersionUID = 1L; + @javax.validation.constraints.NotNull private String abbreviatedOid; private int additions; @@ -626,4 +628,4 @@ public class Commit implements java.io.Serializable, Closer, IssueTimelineItem, } } -} \ No newline at end of file +} diff --git a/src/test/resources/expected-classes/Commit_noParametrizedFields.java.txt b/src/test/resources/expected-classes/Commit_noParametrizedFields.java.txt index 16d2a2927..cd09b1dc6 100644 --- a/src/test/resources/expected-classes/Commit_noParametrizedFields.java.txt +++ b/src/test/resources/expected-classes/Commit_noParametrizedFields.java.txt @@ -7,6 +7,8 @@ package com.github.graphql; ) public class Commit implements java.io.Serializable, Closer, IssueTimelineItem, PullRequestTimelineItem, Subscribable, Node, GitObject, UniformResourceLocatable { + private static final long serialVersionUID = 1L; + @javax.validation.constraints.NotNull private String abbreviatedOid; private int additions; @@ -532,4 +534,4 @@ public class Commit implements java.io.Serializable, Closer, IssueTimelineItem, } } -} \ No newline at end of file +} diff --git a/src/test/resources/expected-classes/Commit_noValidationAnnotation.java.txt b/src/test/resources/expected-classes/Commit_noValidationAnnotation.java.txt index d9a7f550a..d572b6e05 100644 --- a/src/test/resources/expected-classes/Commit_noValidationAnnotation.java.txt +++ b/src/test/resources/expected-classes/Commit_noValidationAnnotation.java.txt @@ -7,6 +7,8 @@ package com.github.graphql; ) public class Commit implements java.io.Serializable, Closer, IssueTimelineItem, PullRequestTimelineItem, Subscribable, Node, GitObject, UniformResourceLocatable { + private static final long serialVersionUID = 1L; + private String abbreviatedOid; private int additions; private PullRequestConnection associatedPullRequests; @@ -602,4 +604,4 @@ public class Commit implements java.io.Serializable, Closer, IssueTimelineItem, } } -} \ No newline at end of file +} diff --git a/src/test/resources/expected-classes/Commit_withoutPrimitives.java.txt b/src/test/resources/expected-classes/Commit_withoutPrimitives.java.txt index 443487912..91a88e054 100644 --- a/src/test/resources/expected-classes/Commit_withoutPrimitives.java.txt +++ b/src/test/resources/expected-classes/Commit_withoutPrimitives.java.txt @@ -7,6 +7,8 @@ package com.github.graphql; ) public class Commit implements java.io.Serializable, Closer, IssueTimelineItem, PullRequestTimelineItem, Subscribable, Node, GitObject, UniformResourceLocatable { + private static final long serialVersionUID = 1L; + @javax.validation.constraints.NotNull private String abbreviatedOid; @javax.validation.constraints.NotNull @@ -632,4 +634,4 @@ public class Commit implements java.io.Serializable, Closer, IssueTimelineItem, } } -} \ No newline at end of file +} diff --git a/src/test/resources/expected-classes/Event.java.txt b/src/test/resources/expected-classes/Event.java.txt index b9cd645b0..68416933d 100644 --- a/src/test/resources/expected-classes/Event.java.txt +++ b/src/test/resources/expected-classes/Event.java.txt @@ -10,6 +10,8 @@ package com.kobylynskyi.graphql.test1; ) public class Event implements java.io.Serializable { + private static final long serialVersionUID = 1L; + private String id; private String categoryId; private java.util.List properties; @@ -155,4 +157,4 @@ public class Event implements java.io.Serializable { } } -} \ No newline at end of file +} diff --git a/src/test/resources/expected-classes/EventProperty.java.txt b/src/test/resources/expected-classes/EventProperty.java.txt index aa9c6ff21..4664c762c 100644 --- a/src/test/resources/expected-classes/EventProperty.java.txt +++ b/src/test/resources/expected-classes/EventProperty.java.txt @@ -10,6 +10,8 @@ package com.kobylynskyi.graphql.test1; ) public class EventProperty implements java.io.Serializable { + private static final long serialVersionUID = 1L; + private Double floatVal; private Boolean booleanVal; private int intVal; @@ -188,4 +190,4 @@ public class EventProperty implements java.io.Serializable { } } -} \ No newline at end of file +} diff --git a/src/test/resources/expected-classes/EventPropertyTO_toString.java.txt b/src/test/resources/expected-classes/EventPropertyTO_toString.java.txt index ca7cc374c..2fd460a42 100644 --- a/src/test/resources/expected-classes/EventPropertyTO_toString.java.txt +++ b/src/test/resources/expected-classes/EventPropertyTO_toString.java.txt @@ -11,6 +11,8 @@ import java.util.StringJoiner; ) public class EventPropertyTO implements java.io.Serializable { + private static final long serialVersionUID = 1L; + private Double floatVal; private Boolean booleanVal; private int intVal; @@ -213,4 +215,4 @@ public class EventPropertyTO implements java.io.Serializable { } } -} \ No newline at end of file +} diff --git a/src/test/resources/expected-classes/EventPropertyTO_withEqualsAndHashCode.java.txt b/src/test/resources/expected-classes/EventPropertyTO_withEqualsAndHashCode.java.txt index cafb7d5d8..62eb0e4cc 100644 --- a/src/test/resources/expected-classes/EventPropertyTO_withEqualsAndHashCode.java.txt +++ b/src/test/resources/expected-classes/EventPropertyTO_withEqualsAndHashCode.java.txt @@ -11,6 +11,8 @@ import java.util.Objects; ) public class EventPropertyTO implements java.io.Serializable { + private static final long serialVersionUID = 1L; + private Double floatVal; private Boolean booleanVal; private int intVal; @@ -211,4 +213,4 @@ public class EventPropertyTO implements java.io.Serializable { } } -} \ No newline at end of file +} diff --git a/src/test/resources/expected-classes/EventPropertyTO_withoutGeneratedAnnotation.java.txt b/src/test/resources/expected-classes/EventPropertyTO_withoutGeneratedAnnotation.java.txt index 109d91bed..125d19b7a 100644 --- a/src/test/resources/expected-classes/EventPropertyTO_withoutGeneratedAnnotation.java.txt +++ b/src/test/resources/expected-classes/EventPropertyTO_withoutGeneratedAnnotation.java.txt @@ -6,6 +6,8 @@ package com.kobylynskyi.graphql.test1; */ public class EventPropertyTO implements java.io.Serializable { + private static final long serialVersionUID = 1L; + private Double floatVal; private Boolean booleanVal; private int intVal; diff --git a/src/test/resources/expected-classes/Event_noBuilder.java.txt b/src/test/resources/expected-classes/Event_noBuilder.java.txt index 8e2dd4088..14ff02763 100644 --- a/src/test/resources/expected-classes/Event_noBuilder.java.txt +++ b/src/test/resources/expected-classes/Event_noBuilder.java.txt @@ -10,6 +10,8 @@ package com.kobylynskyi.graphql.test1; ) public class Event implements java.io.Serializable { + private static final long serialVersionUID = 1L; + private String id; private String categoryId; private java.util.List properties; @@ -91,4 +93,4 @@ public class Event implements java.io.Serializable { -} \ No newline at end of file +} diff --git a/src/test/resources/expected-classes/GithubAcceptTopicSuggestionInputTO.java.txt b/src/test/resources/expected-classes/GithubAcceptTopicSuggestionInputTO.java.txt index 5281447b8..f86c24c18 100644 --- a/src/test/resources/expected-classes/GithubAcceptTopicSuggestionInputTO.java.txt +++ b/src/test/resources/expected-classes/GithubAcceptTopicSuggestionInputTO.java.txt @@ -7,6 +7,8 @@ package com.github.graphql; ) public class GithubAcceptTopicSuggestionInputTO implements java.io.Serializable { + private static final long serialVersionUID = 1L; + private String clientMutationId; @javax.validation.constraints.NotNull private String name; @@ -79,4 +81,4 @@ public class GithubAcceptTopicSuggestionInputTO implements java.io.Serializable } } -} \ No newline at end of file +} diff --git a/src/test/resources/expected-classes/GithubAcceptTopicSuggestionPayloadTO.java.txt b/src/test/resources/expected-classes/GithubAcceptTopicSuggestionPayloadTO.java.txt index bf5ab23fc..aabd7984b 100644 --- a/src/test/resources/expected-classes/GithubAcceptTopicSuggestionPayloadTO.java.txt +++ b/src/test/resources/expected-classes/GithubAcceptTopicSuggestionPayloadTO.java.txt @@ -7,6 +7,8 @@ package com.github.graphql; ) public class GithubAcceptTopicSuggestionPayloadTO implements java.io.Serializable { + private static final long serialVersionUID = 1L; + private String clientMutationId; public GithubAcceptTopicSuggestionPayloadTO() { @@ -47,4 +49,4 @@ public class GithubAcceptTopicSuggestionPayloadTO implements java.io.Serializabl } } -} \ No newline at end of file +} diff --git a/src/test/resources/expected-classes/GithubCommitTO.java.txt b/src/test/resources/expected-classes/GithubCommitTO.java.txt index 3cfef2926..dda379ef9 100644 --- a/src/test/resources/expected-classes/GithubCommitTO.java.txt +++ b/src/test/resources/expected-classes/GithubCommitTO.java.txt @@ -7,6 +7,8 @@ package com.github.graphql; ) public class GithubCommitTO implements java.io.Serializable, GithubCloserTO, GithubIssueTimelineItemTO, GithubPullRequestTimelineItemTO, GithubGitObjectTO, GithubNodeTO, GithubSubscribableTO, GithubUniformResourceLocatableTO { + private static final long serialVersionUID = 1L; + @javax.validation.constraints.NotNull private String abbreviatedOid; private int additions; @@ -626,4 +628,4 @@ public class GithubCommitTO implements java.io.Serializable, GithubCloserTO, Git } } -} \ No newline at end of file +} diff --git a/src/test/resources/expected-classes/Person.java.txt b/src/test/resources/expected-classes/Person.java.txt index c3b6f602d..e8e9582e5 100644 --- a/src/test/resources/expected-classes/Person.java.txt +++ b/src/test/resources/expected-classes/Person.java.txt @@ -7,6 +7,8 @@ package com.kobylynskyi.graphql.test1; ) public class Person implements java.io.Serializable, NamedEntity { + private static final long serialVersionUID = 1L; + private String name; private Integer age; diff --git a/src/test/resources/expected-classes/Person1.java.txt b/src/test/resources/expected-classes/Person1.java.txt index 705249908..89ae390c1 100644 --- a/src/test/resources/expected-classes/Person1.java.txt +++ b/src/test/resources/expected-classes/Person1.java.txt @@ -7,6 +7,8 @@ package com.kobylynskyi.graphql.test1; ) public class Person implements java.io.Serializable, NamedEntity { + private static final long serialVersionUID = 1L; + @Deprecated private String name; private Integer age; @@ -84,4 +86,4 @@ public class Person implements java.io.Serializable, NamedEntity { } } -} \ No newline at end of file +} diff --git a/src/test/resources/expected-classes/ProfileOwner.java.txt b/src/test/resources/expected-classes/ProfileOwner.java.txt index aecfc03a2..0cb9bf1d6 100644 --- a/src/test/resources/expected-classes/ProfileOwner.java.txt +++ b/src/test/resources/expected-classes/ProfileOwner.java.txt @@ -7,7 +7,7 @@ package com.github.graphql; ) public interface ProfileOwner { - boolean getAnyPinnableItems(); + boolean getAnyPinnableItems(PinnableItemType type); String getEmail(); @@ -25,10 +25,10 @@ public interface ProfileOwner { String getName(); @javax.validation.constraints.NotNull - PinnableItemConnection getPinnableItems(); + PinnableItemConnection getPinnableItems(String after, String before, Integer first, Integer last, @javax.validation.constraints.NotNull java.util.List types); @javax.validation.constraints.NotNull - PinnableItemConnection getPinnedItems(); + PinnableItemConnection getPinnedItems(String after, String before, Integer first, Integer last, @javax.validation.constraints.NotNull java.util.List types); int getPinnedItemsRemaining(); diff --git a/src/test/resources/expected-classes/UnionMember1.java.txt b/src/test/resources/expected-classes/UnionMember1.java.txt index bfc48ca6b..ed5861b45 100644 --- a/src/test/resources/expected-classes/UnionMember1.java.txt +++ b/src/test/resources/expected-classes/UnionMember1.java.txt @@ -7,6 +7,8 @@ package com.kobylynskyi.graphql.multifiles; ) public class UnionMember1 implements java.io.Serializable, MyUnion { + private static final long serialVersionUID = 1L; + private Integer someField; public UnionMember1() { @@ -47,4 +49,4 @@ public class UnionMember1 implements java.io.Serializable, MyUnion { } } -} \ No newline at end of file +} diff --git a/src/test/resources/expected-classes/UnionMember2.java.txt b/src/test/resources/expected-classes/UnionMember2.java.txt index f17773cfb..001659317 100644 --- a/src/test/resources/expected-classes/UnionMember2.java.txt +++ b/src/test/resources/expected-classes/UnionMember2.java.txt @@ -7,6 +7,8 @@ package com.kobylynskyi.graphql.multifiles; ) public class UnionMember2 implements java.io.Serializable, MyUnion { + private static final long serialVersionUID = 1L; + private String someField; public UnionMember2() { @@ -47,4 +49,4 @@ public class UnionMember2 implements java.io.Serializable, MyUnion { } } -} \ No newline at end of file +} diff --git a/src/test/resources/expected-classes/User.java.txt b/src/test/resources/expected-classes/User.java.txt index 2c76824b2..3962e4311 100644 --- a/src/test/resources/expected-classes/User.java.txt +++ b/src/test/resources/expected-classes/User.java.txt @@ -10,6 +10,8 @@ package com.kobylynskyi.graphql.test1; ) public class User implements java.io.Serializable { + private static final long serialVersionUID = 1L; + private String name; private java.util.List friends; diff --git a/src/test/resources/expected-classes/annotation/EventProperty.java.txt b/src/test/resources/expected-classes/annotation/EventProperty.java.txt index 014aedb06..12bbcb787 100644 --- a/src/test/resources/expected-classes/annotation/EventProperty.java.txt +++ b/src/test/resources/expected-classes/annotation/EventProperty.java.txt @@ -11,6 +11,8 @@ package com.kobylynskyi.graphql.test1; @com.example.CustomAnnotation public class EventProperty implements java.io.Serializable { + private static final long serialVersionUID = 1L; + private Double floatVal; private Boolean booleanVal; private int intVal; @@ -189,4 +191,4 @@ public class EventProperty implements java.io.Serializable { } } -} \ No newline at end of file +} diff --git a/src/test/resources/expected-classes/annotation/User.java.txt b/src/test/resources/expected-classes/annotation/User.java.txt index 2ef170f20..4dd342bb4 100644 --- a/src/test/resources/expected-classes/annotation/User.java.txt +++ b/src/test/resources/expected-classes/annotation/User.java.txt @@ -10,6 +10,8 @@ package com.kobylynskyi.graphql.test1; ) public class User implements java.io.Serializable { + private static final long serialVersionUID = 1L; + private String name; @com.example.Relationship(type = "FRIEND_WITH", direction = OUT) private java.util.List friends; diff --git a/src/test/resources/expected-classes/custom-type/ResponseContainingDate.java.txt b/src/test/resources/expected-classes/custom-type/ResponseContainingDate.java.txt index 5aec90729..a0023bb01 100644 --- a/src/test/resources/expected-classes/custom-type/ResponseContainingDate.java.txt +++ b/src/test/resources/expected-classes/custom-type/ResponseContainingDate.java.txt @@ -9,6 +9,8 @@ import java.util.StringJoiner; ) public class ResponseContainingDate implements java.io.Serializable { + private static final long serialVersionUID = 1L; + private java.time.ZonedDateTime a; public ResponseContainingDate() { @@ -57,4 +59,4 @@ public class ResponseContainingDate implements java.io.Serializable { } } -} \ No newline at end of file +} diff --git a/src/test/resources/expected-classes/defaults/InputWithDefaults.java.txt b/src/test/resources/expected-classes/defaults/InputWithDefaults.java.txt index 5a906e4e9..91bd5111e 100644 --- a/src/test/resources/expected-classes/defaults/InputWithDefaults.java.txt +++ b/src/test/resources/expected-classes/defaults/InputWithDefaults.java.txt @@ -10,6 +10,8 @@ package com.kobylynskyi.graphql.testdefaults; ) public class InputWithDefaults implements java.io.Serializable { + private static final long serialVersionUID = 1L; + private Double floatVal = 1.23; private Boolean booleanVal = false; private Integer intVal = 42; @@ -202,4 +204,4 @@ public class InputWithDefaults implements java.io.Serializable { } } -} \ No newline at end of file +} diff --git a/src/test/resources/expected-classes/defaults/InputWithDefaultsDTO.java.txt b/src/test/resources/expected-classes/defaults/InputWithDefaultsDTO.java.txt index 8d92b9718..3f20b4cac 100644 --- a/src/test/resources/expected-classes/defaults/InputWithDefaultsDTO.java.txt +++ b/src/test/resources/expected-classes/defaults/InputWithDefaultsDTO.java.txt @@ -10,6 +10,8 @@ package com.kobylynskyi.graphql.testdefaults; ) public class InputWithDefaultsDTO implements java.io.Serializable { + private static final long serialVersionUID = 1L; + private Double floatVal = 1.23; private Boolean booleanVal = false; private Integer intVal = 42; diff --git a/src/test/resources/expected-classes/defaults/InputWithDefaultsTO.java.txt b/src/test/resources/expected-classes/defaults/InputWithDefaultsTO.java.txt index 1777c0864..1505e9ee9 100644 --- a/src/test/resources/expected-classes/defaults/InputWithDefaultsTO.java.txt +++ b/src/test/resources/expected-classes/defaults/InputWithDefaultsTO.java.txt @@ -10,6 +10,8 @@ package com.kobylynskyi.graphql.testdefaults; ) public class InputWithDefaultsTO implements java.io.Serializable { + private static final long serialVersionUID = 1L; + private Double floatVal = 1.23; private Boolean booleanVal = false; private Integer intVal = 42; @@ -202,4 +204,4 @@ public class InputWithDefaultsTO implements java.io.Serializable { } } -} \ No newline at end of file +} diff --git a/src/test/resources/expected-classes/defaults/SomeObject.java.txt b/src/test/resources/expected-classes/defaults/SomeObject.java.txt index 380b17f37..eb21593b3 100644 --- a/src/test/resources/expected-classes/defaults/SomeObject.java.txt +++ b/src/test/resources/expected-classes/defaults/SomeObject.java.txt @@ -7,6 +7,8 @@ package com.kobylynskyi.graphql.testdefaults; ) public class SomeObject implements java.io.Serializable { + private static final long serialVersionUID = 1L; + @javax.validation.constraints.NotNull private String name; @@ -48,4 +50,4 @@ public class SomeObject implements java.io.Serializable { } } -} \ No newline at end of file +} diff --git a/src/test/resources/expected-classes/defaults/SomeObjectDTO.java.txt b/src/test/resources/expected-classes/defaults/SomeObjectDTO.java.txt index 6acb509f6..3ed8cd5bd 100644 --- a/src/test/resources/expected-classes/defaults/SomeObjectDTO.java.txt +++ b/src/test/resources/expected-classes/defaults/SomeObjectDTO.java.txt @@ -7,6 +7,8 @@ package com.kobylynskyi.graphql.testdefaults; ) public class SomeObjectDTO implements java.io.Serializable { + private static final long serialVersionUID = 1L; + @javax.validation.constraints.NotNull private String name; diff --git a/src/test/resources/expected-classes/defaults/SomeObjectTO.java.txt b/src/test/resources/expected-classes/defaults/SomeObjectTO.java.txt index 1f3cc1783..6976c23b3 100644 --- a/src/test/resources/expected-classes/defaults/SomeObjectTO.java.txt +++ b/src/test/resources/expected-classes/defaults/SomeObjectTO.java.txt @@ -7,6 +7,8 @@ package com.kobylynskyi.graphql.testdefaults; ) public class SomeObjectTO implements java.io.Serializable { + private static final long serialVersionUID = 1L; + @javax.validation.constraints.NotNull private String name; @@ -48,4 +50,4 @@ public class SomeObjectTO implements java.io.Serializable { } } -} \ No newline at end of file +} diff --git a/src/test/resources/expected-classes/deprecated/Event.java.txt b/src/test/resources/expected-classes/deprecated/Event.java.txt index db54f5879..f8da8351c 100644 --- a/src/test/resources/expected-classes/deprecated/Event.java.txt +++ b/src/test/resources/expected-classes/deprecated/Event.java.txt @@ -7,6 +7,8 @@ package com.kobylynskyi.graphql.test1; ) public class Event implements java.io.Serializable, PinnableItem, Node { + private static final long serialVersionUID = 1L; + @Deprecated @javax.validation.constraints.NotNull private Status status; @@ -92,4 +94,4 @@ public class Event implements java.io.Serializable, PinnableItem, Node { } } -} \ No newline at end of file +} diff --git a/src/test/resources/expected-classes/deprecated/EventInput.java.txt b/src/test/resources/expected-classes/deprecated/EventInput.java.txt index c901f3d15..89e47d0a2 100644 --- a/src/test/resources/expected-classes/deprecated/EventInput.java.txt +++ b/src/test/resources/expected-classes/deprecated/EventInput.java.txt @@ -7,6 +7,8 @@ package com.kobylynskyi.graphql.test1; ) public class EventInput implements java.io.Serializable { + private static final long serialVersionUID = 1L; + @Deprecated @javax.validation.constraints.NotNull private Status status; @@ -52,4 +54,4 @@ public class EventInput implements java.io.Serializable { } } -} \ No newline at end of file +} diff --git a/src/test/resources/expected-classes/empty/Event.java.txt b/src/test/resources/expected-classes/empty/Event.java.txt index bb1c08135..578790df8 100644 --- a/src/test/resources/expected-classes/empty/Event.java.txt +++ b/src/test/resources/expected-classes/empty/Event.java.txt @@ -4,6 +4,8 @@ ) public class Event implements java.io.Serializable { + private static final long serialVersionUID = 1L; + public Event() { } @@ -27,4 +29,4 @@ public class Event implements java.io.Serializable { } } -} \ No newline at end of file +} diff --git a/src/test/resources/expected-classes/empty/EventInput.java.txt b/src/test/resources/expected-classes/empty/EventInput.java.txt index ee22ff655..c5aff43c5 100644 --- a/src/test/resources/expected-classes/empty/EventInput.java.txt +++ b/src/test/resources/expected-classes/empty/EventInput.java.txt @@ -4,6 +4,8 @@ ) public class EventInput implements java.io.Serializable { + private static final long serialVersionUID = 1L; + public EventInput() { } @@ -27,4 +29,4 @@ public class EventInput implements java.io.Serializable { } } -} \ No newline at end of file +} diff --git a/src/test/resources/expected-classes/extend-with-resolvers/Asset.java.txt b/src/test/resources/expected-classes/extend-with-resolvers/Asset.java.txt index 7ef0a174f..f69c36721 100644 --- a/src/test/resources/expected-classes/extend-with-resolvers/Asset.java.txt +++ b/src/test/resources/expected-classes/extend-with-resolvers/Asset.java.txt @@ -4,6 +4,8 @@ ) public class Asset implements java.io.Serializable, PinnableItem, Node { + private static final long serialVersionUID = 1L; + @javax.validation.constraints.NotNull private String name; @javax.validation.constraints.NotNull @@ -77,4 +79,4 @@ public class Asset implements java.io.Serializable, PinnableItem, Node { } } -} \ No newline at end of file +} diff --git a/src/test/resources/expected-classes/extend-with-resolvers/AssetInput.java.txt b/src/test/resources/expected-classes/extend-with-resolvers/AssetInput.java.txt index 1528e114d..5a41c1c66 100644 --- a/src/test/resources/expected-classes/extend-with-resolvers/AssetInput.java.txt +++ b/src/test/resources/expected-classes/extend-with-resolvers/AssetInput.java.txt @@ -4,6 +4,8 @@ ) public class AssetInput implements java.io.Serializable { + private static final long serialVersionUID = 1L; + @javax.validation.constraints.NotNull private String name; diff --git a/src/test/resources/expected-classes/extend-with-resolvers/Event.java.txt b/src/test/resources/expected-classes/extend-with-resolvers/Event.java.txt index 8e2f71c0f..624ecde90 100644 --- a/src/test/resources/expected-classes/extend-with-resolvers/Event.java.txt +++ b/src/test/resources/expected-classes/extend-with-resolvers/Event.java.txt @@ -4,6 +4,8 @@ ) public class Event implements java.io.Serializable, PinnableItem, Node { + private static final long serialVersionUID = 1L; + @javax.validation.constraints.NotNull private Status status; @javax.validation.constraints.NotNull @@ -77,4 +79,4 @@ public class Event implements java.io.Serializable, PinnableItem, Node { } } -} \ No newline at end of file +} diff --git a/src/test/resources/expected-classes/extend-with-resolvers/EventInput.java.txt b/src/test/resources/expected-classes/extend-with-resolvers/EventInput.java.txt index 201223773..56b4d5269 100644 --- a/src/test/resources/expected-classes/extend-with-resolvers/EventInput.java.txt +++ b/src/test/resources/expected-classes/extend-with-resolvers/EventInput.java.txt @@ -4,6 +4,8 @@ ) public class EventInput implements java.io.Serializable { + private static final long serialVersionUID = 1L; + @javax.validation.constraints.NotNull private Status status; @javax.validation.constraints.NotNull diff --git a/src/test/resources/expected-classes/extend/Asset.java.txt b/src/test/resources/expected-classes/extend/Asset.java.txt index bfa05b522..705faca56 100644 --- a/src/test/resources/expected-classes/extend/Asset.java.txt +++ b/src/test/resources/expected-classes/extend/Asset.java.txt @@ -4,6 +4,8 @@ ) public class Asset implements java.io.Serializable, PinnableItem, Node { + private static final long serialVersionUID = 1L; + @javax.validation.constraints.NotNull private String name; @javax.validation.constraints.NotNull @@ -92,4 +94,4 @@ public class Asset implements java.io.Serializable, PinnableItem, Node { } } -} \ No newline at end of file +} diff --git a/src/test/resources/expected-classes/extend/AssetInput.java.txt b/src/test/resources/expected-classes/extend/AssetInput.java.txt index 1528e114d..5a41c1c66 100644 --- a/src/test/resources/expected-classes/extend/AssetInput.java.txt +++ b/src/test/resources/expected-classes/extend/AssetInput.java.txt @@ -4,6 +4,8 @@ ) public class AssetInput implements java.io.Serializable { + private static final long serialVersionUID = 1L; + @javax.validation.constraints.NotNull private String name; diff --git a/src/test/resources/expected-classes/extend/Event.java.txt b/src/test/resources/expected-classes/extend/Event.java.txt index a7d2763be..fd440bd82 100644 --- a/src/test/resources/expected-classes/extend/Event.java.txt +++ b/src/test/resources/expected-classes/extend/Event.java.txt @@ -4,6 +4,8 @@ ) public class Event implements java.io.Serializable, PinnableItem, Node { + private static final long serialVersionUID = 1L; + @javax.validation.constraints.NotNull private Status status; @javax.validation.constraints.NotNull @@ -117,4 +119,4 @@ public class Event implements java.io.Serializable, PinnableItem, Node { } } -} \ No newline at end of file +} diff --git a/src/test/resources/expected-classes/extend/EventInput.java.txt b/src/test/resources/expected-classes/extend/EventInput.java.txt index 201223773..56b4d5269 100644 --- a/src/test/resources/expected-classes/extend/EventInput.java.txt +++ b/src/test/resources/expected-classes/extend/EventInput.java.txt @@ -4,6 +4,8 @@ ) public class EventInput implements java.io.Serializable { + private static final long serialVersionUID = 1L; + @javax.validation.constraints.NotNull private Status status; @javax.validation.constraints.NotNull diff --git a/src/test/resources/expected-classes/from-introspection-result/Product.java.txt b/src/test/resources/expected-classes/from-introspection-result/Product.java.txt index e9b569d8d..a07cf1d21 100644 --- a/src/test/resources/expected-classes/from-introspection-result/Product.java.txt +++ b/src/test/resources/expected-classes/from-introspection-result/Product.java.txt @@ -9,6 +9,8 @@ import java.util.StringJoiner; ) public class Product implements java.io.Serializable { + private static final long serialVersionUID = 1L; + @javax.validation.constraints.NotNull private String id; @javax.validation.constraints.NotNull diff --git a/src/test/resources/expected-classes/from-introspection-result/ProductInput.java.txt b/src/test/resources/expected-classes/from-introspection-result/ProductInput.java.txt index a010d56bc..f48aacbc8 100644 --- a/src/test/resources/expected-classes/from-introspection-result/ProductInput.java.txt +++ b/src/test/resources/expected-classes/from-introspection-result/ProductInput.java.txt @@ -9,6 +9,8 @@ import java.util.StringJoiner; ) public class ProductInput implements java.io.Serializable { + private static final long serialVersionUID = 1L; + @javax.validation.constraints.NotNull private String title; private String description; diff --git a/src/test/resources/expected-classes/immutable/Event.java.txt b/src/test/resources/expected-classes/immutable/Event.java.txt index f84b9dcf1..3533cb780 100644 --- a/src/test/resources/expected-classes/immutable/Event.java.txt +++ b/src/test/resources/expected-classes/immutable/Event.java.txt @@ -10,6 +10,8 @@ package com.kobylynskyi.graphql.immutable; ) public class Event implements java.io.Serializable { + private static final long serialVersionUID = 1L; + private String id; private String categoryId; private java.util.List properties; @@ -131,4 +133,4 @@ public class Event implements java.io.Serializable { } } -} \ No newline at end of file +} diff --git a/src/test/resources/expected-classes/interfaces/Bar1.java.txt b/src/test/resources/expected-classes/interfaces/Bar1.java.txt index 3e8a10a3b..2f1445cf3 100644 --- a/src/test/resources/expected-classes/interfaces/Bar1.java.txt +++ b/src/test/resources/expected-classes/interfaces/Bar1.java.txt @@ -7,6 +7,8 @@ package com.kobylynskyi.graphql.interfaces; ) public class Bar1 implements java.io.Serializable, Bar { + private static final long serialVersionUID = 1L; + @javax.validation.constraints.NotNull private String id; @@ -48,4 +50,4 @@ public class Bar1 implements java.io.Serializable, Bar { } } -} \ No newline at end of file +} diff --git a/src/test/resources/expected-classes/interfaces/Foo1.java.txt b/src/test/resources/expected-classes/interfaces/Foo1.java.txt index ecbfc5379..84c1ae002 100644 --- a/src/test/resources/expected-classes/interfaces/Foo1.java.txt +++ b/src/test/resources/expected-classes/interfaces/Foo1.java.txt @@ -7,6 +7,8 @@ package com.kobylynskyi.graphql.interfaces; ) public class Foo1 implements java.io.Serializable, Foo { + private static final long serialVersionUID = 1L; + @javax.validation.constraints.NotNull private String id; private java.util.List bars; @@ -63,4 +65,4 @@ public class Foo1 implements java.io.Serializable, Foo { } } -} \ No newline at end of file +} diff --git a/src/test/resources/expected-classes/jackson-resolver-union/UnionMemberA.java.txt b/src/test/resources/expected-classes/jackson-resolver-union/UnionMemberA.java.txt index efabf09e2..e63cf709f 100644 --- a/src/test/resources/expected-classes/jackson-resolver-union/UnionMemberA.java.txt +++ b/src/test/resources/expected-classes/jackson-resolver-union/UnionMemberA.java.txt @@ -7,6 +7,8 @@ package com.kobylynskyi.graphql.unionresolver; ) public class UnionMemberA implements java.io.Serializable, UnionToResolve { + private static final long serialVersionUID = 1L; + private Integer someField; public UnionMemberA() { @@ -47,4 +49,4 @@ public class UnionMemberA implements java.io.Serializable, UnionToResolve { } } -} \ No newline at end of file +} diff --git a/src/test/resources/expected-classes/jackson-resolver-union/UnionMemberB.java.txt b/src/test/resources/expected-classes/jackson-resolver-union/UnionMemberB.java.txt index 1cd23b14a..203ee4e6d 100644 --- a/src/test/resources/expected-classes/jackson-resolver-union/UnionMemberB.java.txt +++ b/src/test/resources/expected-classes/jackson-resolver-union/UnionMemberB.java.txt @@ -7,6 +7,8 @@ package com.kobylynskyi.graphql.unionresolver; ) public class UnionMemberB implements java.io.Serializable, UnionToResolve { + private static final long serialVersionUID = 1L; + private String someField; public UnionMemberB() { @@ -47,4 +49,4 @@ public class UnionMemberB implements java.io.Serializable, UnionToResolve { } } -} \ No newline at end of file +} diff --git a/src/test/resources/expected-classes/jackson-resolver-union/without-model-package/MyUnionMemberASuffix.java.txt b/src/test/resources/expected-classes/jackson-resolver-union/without-model-package/MyUnionMemberASuffix.java.txt index ec09ffd50..cd8b10758 100644 --- a/src/test/resources/expected-classes/jackson-resolver-union/without-model-package/MyUnionMemberASuffix.java.txt +++ b/src/test/resources/expected-classes/jackson-resolver-union/without-model-package/MyUnionMemberASuffix.java.txt @@ -4,6 +4,8 @@ ) public class MyUnionMemberASuffix implements java.io.Serializable, MyUnionToResolveSuffix { + private static final long serialVersionUID = 1L; + private Integer someField; public MyUnionMemberASuffix() { @@ -44,4 +46,4 @@ public class MyUnionMemberASuffix implements java.io.Serializable, MyUnionToReso } } -} \ No newline at end of file +} diff --git a/src/test/resources/expected-classes/jackson-resolver-union/without-model-package/MyUnionMemberBSuffix.java.txt b/src/test/resources/expected-classes/jackson-resolver-union/without-model-package/MyUnionMemberBSuffix.java.txt index 867d41baf..80488d274 100644 --- a/src/test/resources/expected-classes/jackson-resolver-union/without-model-package/MyUnionMemberBSuffix.java.txt +++ b/src/test/resources/expected-classes/jackson-resolver-union/without-model-package/MyUnionMemberBSuffix.java.txt @@ -4,6 +4,8 @@ ) public class MyUnionMemberBSuffix implements java.io.Serializable, MyUnionToResolveSuffix { + private static final long serialVersionUID = 1L; + private String someField; public MyUnionMemberBSuffix() { @@ -44,4 +46,4 @@ public class MyUnionMemberBSuffix implements java.io.Serializable, MyUnionToReso } } -} \ No newline at end of file +} diff --git a/src/test/resources/expected-classes/kt/Comment_sealed_interfaces.kt.txt b/src/test/resources/expected-classes/kt/Comment_sealed_interfaces.kt.txt new file mode 100644 index 000000000..97d2a15c1 --- /dev/null +++ b/src/test/resources/expected-classes/kt/Comment_sealed_interfaces.kt.txt @@ -0,0 +1,40 @@ +package com.github.graphql + + +@javax.annotation.Generated( + value = ["com.kobylynskyi.graphql.codegen.GraphQLCodegen"], + date = "2020-12-31T23:59:59-0500" +) +sealed interface Comment { + + val author: Actor? + + val authorAssociation: CommentAuthorAssociation + + val body: String + + val bodyHTML: String + + val bodyText: String + + val createdAt: String + + val createdViaEmail: Boolean + + val editor: Actor? + + val id: String + + val includesCreatedEdit: Boolean + + val lastEditedAt: String? + + val publishedAt: String? + + val updatedAt: String + + val userContentEdits: UserContentEditConnection? + + val viewerDidAuthor: Boolean + +} diff --git a/src/test/resources/expected-classes/kt/Commit_initialize_nullable_types.kt.txt b/src/test/resources/expected-classes/kt/Commit_initialize_nullable_types.kt.txt new file mode 100644 index 000000000..1ec67aeec --- /dev/null +++ b/src/test/resources/expected-classes/kt/Commit_initialize_nullable_types.kt.txt @@ -0,0 +1,360 @@ +package com.github.graphql + +import com.kobylynskyi.graphql.codegen.model.graphql.GraphQLRequestSerializer +import java.util.StringJoiner + +@javax.annotation.Generated( + value = ["com.kobylynskyi.graphql.codegen.GraphQLCodegen"], + date = "2020-12-31T23:59:59-0500" +) +data class Commit( + override + val abbreviatedOid: String, + val additions: Int, + val associatedPullRequests: PullRequestConnection? = null, + val author: GitActor? = null, + val authoredByCommitter: Boolean, + val authoredDate: String, + val blame: Blame, + val changedFiles: Int, + val comments: CommitCommentConnection, + override + val commitResourcePath: String, + override + val commitUrl: String, + val committedDate: String, + val committedViaWeb: Boolean, + val committer: GitActor? = null, + val deletions: Int, + val deployments: DeploymentConnection? = null, + val history: CommitHistoryConnection, + override + val id: String, + val message: String, + val messageBody: String, + val messageBodyHTML: String, + val messageHeadline: String, + val messageHeadlineHTML: String, + override + val oid: String, + val parents: CommitConnection, + val pushedDate: String? = null, + override + val repository: Repository, + override + val resourcePath: String, + val signature: GitSignature? = null, + val status: Status? = null, + val tarballUrl: String, + val tree: Tree, + val treeResourcePath: String, + val treeUrl: String, + override + val url: String, + override + val viewerCanSubscribe: Boolean, + override + val viewerSubscription: SubscriptionState? = null, + val zipballUrl: String +) : Closer, IssueTimelineItem, PullRequestTimelineItem, Subscribable, Node, GitObject, UniformResourceLocatable { + + companion object { + @JvmStatic fun builder(): Builder = Builder() + } + + // In the future, it maybe change. + override fun toString(): String { + val joiner = StringJoiner(", ", "{ ", " }") + joiner.add("abbreviatedOid: " + GraphQLRequestSerializer.getEntry(abbreviatedOid)) + joiner.add("additions: " + GraphQLRequestSerializer.getEntry(additions)) + if (associatedPullRequests != null) { + joiner.add("associatedPullRequests: " + GraphQLRequestSerializer.getEntry(associatedPullRequests)) + } + if (author != null) { + joiner.add("author: " + GraphQLRequestSerializer.getEntry(author)) + } + joiner.add("authoredByCommitter: " + GraphQLRequestSerializer.getEntry(authoredByCommitter)) + joiner.add("authoredDate: " + GraphQLRequestSerializer.getEntry(authoredDate)) + joiner.add("blame: " + GraphQLRequestSerializer.getEntry(blame)) + joiner.add("changedFiles: " + GraphQLRequestSerializer.getEntry(changedFiles)) + joiner.add("comments: " + GraphQLRequestSerializer.getEntry(comments)) + joiner.add("commitResourcePath: " + GraphQLRequestSerializer.getEntry(commitResourcePath)) + joiner.add("commitUrl: " + GraphQLRequestSerializer.getEntry(commitUrl)) + joiner.add("committedDate: " + GraphQLRequestSerializer.getEntry(committedDate)) + joiner.add("committedViaWeb: " + GraphQLRequestSerializer.getEntry(committedViaWeb)) + if (committer != null) { + joiner.add("committer: " + GraphQLRequestSerializer.getEntry(committer)) + } + joiner.add("deletions: " + GraphQLRequestSerializer.getEntry(deletions)) + if (deployments != null) { + joiner.add("deployments: " + GraphQLRequestSerializer.getEntry(deployments)) + } + joiner.add("history: " + GraphQLRequestSerializer.getEntry(history)) + joiner.add("id: " + GraphQLRequestSerializer.getEntry(id)) + joiner.add("message: " + GraphQLRequestSerializer.getEntry(message)) + joiner.add("messageBody: " + GraphQLRequestSerializer.getEntry(messageBody)) + joiner.add("messageBodyHTML: " + GraphQLRequestSerializer.getEntry(messageBodyHTML)) + joiner.add("messageHeadline: " + GraphQLRequestSerializer.getEntry(messageHeadline)) + joiner.add("messageHeadlineHTML: " + GraphQLRequestSerializer.getEntry(messageHeadlineHTML)) + joiner.add("oid: " + GraphQLRequestSerializer.getEntry(oid)) + joiner.add("parents: " + GraphQLRequestSerializer.getEntry(parents)) + if (pushedDate != null) { + joiner.add("pushedDate: " + GraphQLRequestSerializer.getEntry(pushedDate)) + } + joiner.add("repository: " + GraphQLRequestSerializer.getEntry(repository)) + joiner.add("resourcePath: " + GraphQLRequestSerializer.getEntry(resourcePath)) + if (signature != null) { + joiner.add("signature: " + GraphQLRequestSerializer.getEntry(signature)) + } + if (status != null) { + joiner.add("status: " + GraphQLRequestSerializer.getEntry(status)) + } + joiner.add("tarballUrl: " + GraphQLRequestSerializer.getEntry(tarballUrl)) + joiner.add("tree: " + GraphQLRequestSerializer.getEntry(tree)) + joiner.add("treeResourcePath: " + GraphQLRequestSerializer.getEntry(treeResourcePath)) + joiner.add("treeUrl: " + GraphQLRequestSerializer.getEntry(treeUrl)) + joiner.add("url: " + GraphQLRequestSerializer.getEntry(url)) + joiner.add("viewerCanSubscribe: " + GraphQLRequestSerializer.getEntry(viewerCanSubscribe)) + if (viewerSubscription != null) { + joiner.add("viewerSubscription: " + GraphQLRequestSerializer.getEntry(viewerSubscription)) + } + joiner.add("zipballUrl: " + GraphQLRequestSerializer.getEntry(zipballUrl)) + return joiner.toString() + } + + class Builder { + + private lateinit var abbreviatedOid: String + private var additions: Int = 0 + private var associatedPullRequests: PullRequestConnection? = null + private var author: GitActor? = null + private var authoredByCommitter: Boolean = false + private lateinit var authoredDate: String + private lateinit var blame: Blame + private var changedFiles: Int = 0 + private lateinit var comments: CommitCommentConnection + private lateinit var commitResourcePath: String + private lateinit var commitUrl: String + private lateinit var committedDate: String + private var committedViaWeb: Boolean = false + private var committer: GitActor? = null + private var deletions: Int = 0 + private var deployments: DeploymentConnection? = null + private lateinit var history: CommitHistoryConnection + private lateinit var id: String + private lateinit var message: String + private lateinit var messageBody: String + private lateinit var messageBodyHTML: String + private lateinit var messageHeadline: String + private lateinit var messageHeadlineHTML: String + private lateinit var oid: String + private lateinit var parents: CommitConnection + private var pushedDate: String? = null + private lateinit var repository: Repository + private lateinit var resourcePath: String + private var signature: GitSignature? = null + private var status: Status? = null + private lateinit var tarballUrl: String + private lateinit var tree: Tree + private lateinit var treeResourcePath: String + private lateinit var treeUrl: String + private lateinit var url: String + private var viewerCanSubscribe: Boolean = false + private var viewerSubscription: SubscriptionState? = null + private lateinit var zipballUrl: String + + fun setAbbreviatedOid(abbreviatedOid: String): Builder { + this.abbreviatedOid = abbreviatedOid + return this + } + + fun setAdditions(additions: Int): Builder { + this.additions = additions + return this + } + + fun setAssociatedPullRequests(associatedPullRequests: PullRequestConnection?): Builder { + this.associatedPullRequests = associatedPullRequests + return this + } + + fun setAuthor(author: GitActor?): Builder { + this.author = author + return this + } + + fun setAuthoredByCommitter(authoredByCommitter: Boolean): Builder { + this.authoredByCommitter = authoredByCommitter + return this + } + + fun setAuthoredDate(authoredDate: String): Builder { + this.authoredDate = authoredDate + return this + } + + fun setBlame(blame: Blame): Builder { + this.blame = blame + return this + } + + fun setChangedFiles(changedFiles: Int): Builder { + this.changedFiles = changedFiles + return this + } + + fun setComments(comments: CommitCommentConnection): Builder { + this.comments = comments + return this + } + + fun setCommitResourcePath(commitResourcePath: String): Builder { + this.commitResourcePath = commitResourcePath + return this + } + + fun setCommitUrl(commitUrl: String): Builder { + this.commitUrl = commitUrl + return this + } + + fun setCommittedDate(committedDate: String): Builder { + this.committedDate = committedDate + return this + } + + fun setCommittedViaWeb(committedViaWeb: Boolean): Builder { + this.committedViaWeb = committedViaWeb + return this + } + + fun setCommitter(committer: GitActor?): Builder { + this.committer = committer + return this + } + + fun setDeletions(deletions: Int): Builder { + this.deletions = deletions + return this + } + + fun setDeployments(deployments: DeploymentConnection?): Builder { + this.deployments = deployments + return this + } + + fun setHistory(history: CommitHistoryConnection): Builder { + this.history = history + return this + } + + fun setId(id: String): Builder { + this.id = id + return this + } + + fun setMessage(message: String): Builder { + this.message = message + return this + } + + fun setMessageBody(messageBody: String): Builder { + this.messageBody = messageBody + return this + } + + fun setMessageBodyHTML(messageBodyHTML: String): Builder { + this.messageBodyHTML = messageBodyHTML + return this + } + + fun setMessageHeadline(messageHeadline: String): Builder { + this.messageHeadline = messageHeadline + return this + } + + fun setMessageHeadlineHTML(messageHeadlineHTML: String): Builder { + this.messageHeadlineHTML = messageHeadlineHTML + return this + } + + fun setOid(oid: String): Builder { + this.oid = oid + return this + } + + fun setParents(parents: CommitConnection): Builder { + this.parents = parents + return this + } + + fun setPushedDate(pushedDate: String?): Builder { + this.pushedDate = pushedDate + return this + } + + fun setRepository(repository: Repository): Builder { + this.repository = repository + return this + } + + fun setResourcePath(resourcePath: String): Builder { + this.resourcePath = resourcePath + return this + } + + fun setSignature(signature: GitSignature?): Builder { + this.signature = signature + return this + } + + fun setStatus(status: Status?): Builder { + this.status = status + return this + } + + fun setTarballUrl(tarballUrl: String): Builder { + this.tarballUrl = tarballUrl + return this + } + + fun setTree(tree: Tree): Builder { + this.tree = tree + return this + } + + fun setTreeResourcePath(treeResourcePath: String): Builder { + this.treeResourcePath = treeResourcePath + return this + } + + fun setTreeUrl(treeUrl: String): Builder { + this.treeUrl = treeUrl + return this + } + + fun setUrl(url: String): Builder { + this.url = url + return this + } + + fun setViewerCanSubscribe(viewerCanSubscribe: Boolean): Builder { + this.viewerCanSubscribe = viewerCanSubscribe + return this + } + + fun setViewerSubscription(viewerSubscription: SubscriptionState?): Builder { + this.viewerSubscription = viewerSubscription + return this + } + + fun setZipballUrl(zipballUrl: String): Builder { + this.zipballUrl = zipballUrl + return this + } + + fun build(): Commit { + return Commit(abbreviatedOid, additions, associatedPullRequests, author, authoredByCommitter, authoredDate, blame, changedFiles, comments, commitResourcePath, commitUrl, committedDate, committedViaWeb, committer, deletions, deployments, history, id, message, messageBody, messageBodyHTML, messageHeadline, messageHeadlineHTML, oid, parents, pushedDate, repository, resourcePath, signature, status, tarballUrl, tree, treeResourcePath, treeUrl, url, viewerCanSubscribe, viewerSubscription, zipballUrl) + } + } +} \ No newline at end of file diff --git a/src/test/resources/expected-classes/kt/default/FriendsQueryRequest.kt.txt b/src/test/resources/expected-classes/kt/default/FriendsQueryRequest.kt.txt new file mode 100644 index 000000000..394f0f585 --- /dev/null +++ b/src/test/resources/expected-classes/kt/default/FriendsQueryRequest.kt.txt @@ -0,0 +1,78 @@ +package com.github.graphql + +import com.kobylynskyi.graphql.codegen.model.graphql.GraphQLOperation +import com.kobylynskyi.graphql.codegen.model.graphql.GraphQLOperationRequest +import java.util.Objects + +@javax.annotation.Generated( + value = ["com.kobylynskyi.graphql.codegen.GraphQLCodegen"], + date = "2020-12-31T23:59:59-0500" +) +open class FriendsQueryRequest(private val alias: String?) : GraphQLOperationRequest { + + companion object { + const val OPERATION_NAME: String = "friends" + val OPERATION_TYPE: GraphQLOperation = GraphQLOperation.QUERY + + @JvmStatic fun builder(): Builder = Builder() + } + + private val input: MutableMap = LinkedHashMap() + private val useObjectMapperForInputSerialization: MutableSet = HashSet() + + constructor(): this(null) + + fun setNum(num: Int) { + this.input["num"] = num + } + + override fun getOperationType(): GraphQLOperation = OPERATION_TYPE + + override fun getOperationName(): String = OPERATION_NAME + + override fun getAlias(): String? = alias ?: OPERATION_NAME + + override fun getInput(): MutableMap = input + + override fun getUseObjectMapperForInputSerialization(): MutableSet = useObjectMapperForInputSerialization + + override fun equals(other: Any?): Boolean { + if (this === other) { + return true + } + if (other == null || javaClass != other.javaClass) { + return false + } + val that = other as FriendsQueryRequest + return Objects.equals(operationType, that.operationType) && + Objects.equals(operationName, that.operationName) && + Objects.equals(input, that.input) + } + + override fun hashCode(): Int = Objects.hash(operationType, operationName, input) + + override fun toString(): String = Objects.toString(input) + + class Builder { + + private var `$alias`: String? = null + private var num: Int = 0 + + fun alias(alias: String?): Builder { + this.`$alias` = alias + return this + } + + fun setNum(num: Int): Builder { + this.num = num + return this + } + + fun build(): FriendsQueryRequest { + val obj = FriendsQueryRequest(`$alias`) + obj.setNum(num) + return obj + } + + } +} \ No newline at end of file diff --git a/src/test/resources/expected-classes/optional/TypeWithMandatoryField.java.txt b/src/test/resources/expected-classes/optional/TypeWithMandatoryField.java.txt index e58a01207..7d4f4d0cd 100644 --- a/src/test/resources/expected-classes/optional/TypeWithMandatoryField.java.txt +++ b/src/test/resources/expected-classes/optional/TypeWithMandatoryField.java.txt @@ -7,6 +7,8 @@ import java.util.StringJoiner; ) public class TypeWithMandatoryField implements java.io.Serializable, InterfaceWithOptionalField { + private static final long serialVersionUID = 1L; + private int test; public TypeWithMandatoryField() { @@ -69,4 +71,4 @@ public class TypeWithMandatoryField implements java.io.Serializable, InterfaceWi } } -} \ No newline at end of file +} diff --git a/src/test/resources/expected-classes/relay/Organization.java.txt b/src/test/resources/expected-classes/relay/Organization.java.txt index 5d49011e3..51cf25c3b 100644 --- a/src/test/resources/expected-classes/relay/Organization.java.txt +++ b/src/test/resources/expected-classes/relay/Organization.java.txt @@ -5,6 +5,8 @@ ) public class Organization implements java.io.Serializable { + private static final long serialVersionUID = 1L; + @javax.validation.constraints.NotNull private String id; diff --git a/src/test/resources/expected-classes/relay/User.java.txt b/src/test/resources/expected-classes/relay/User.java.txt index f8718ef2c..25b0bcb14 100644 --- a/src/test/resources/expected-classes/relay/User.java.txt +++ b/src/test/resources/expected-classes/relay/User.java.txt @@ -5,6 +5,8 @@ ) public class User implements java.io.Serializable { + private static final long serialVersionUID = 1L; + @javax.validation.constraints.NotNull private String id; private String name; diff --git a/src/test/resources/expected-classes/request/AcceptTopicSuggestionInput.java.txt b/src/test/resources/expected-classes/request/AcceptTopicSuggestionInput.java.txt index cb56d84b5..079e2cea6 100644 --- a/src/test/resources/expected-classes/request/AcceptTopicSuggestionInput.java.txt +++ b/src/test/resources/expected-classes/request/AcceptTopicSuggestionInput.java.txt @@ -10,6 +10,8 @@ import java.util.StringJoiner; ) public class AcceptTopicSuggestionInput implements java.io.Serializable { + private static final long serialVersionUID = 1L; + private String clientMutationId; @javax.validation.constraints.NotNull private String name; @@ -114,4 +116,4 @@ public class AcceptTopicSuggestionInput implements java.io.Serializable { } } -} \ No newline at end of file +} diff --git a/src/test/resources/expected-classes/request/Event_useObjectMapperForRequestSerialization.java.txt b/src/test/resources/expected-classes/request/Event_useObjectMapperForRequestSerialization.java.txt index f53397b05..37a0ae031 100644 --- a/src/test/resources/expected-classes/request/Event_useObjectMapperForRequestSerialization.java.txt +++ b/src/test/resources/expected-classes/request/Event_useObjectMapperForRequestSerialization.java.txt @@ -13,6 +13,8 @@ import java.util.StringJoiner; ) public class Event implements java.io.Serializable { + private static final long serialVersionUID = 1L; + private String id; private String categoryId; private java.util.List properties; @@ -210,4 +212,4 @@ public class Event implements java.io.Serializable { } } -} \ No newline at end of file +} diff --git a/src/test/resources/expected-classes/resolvers/Event.java.txt b/src/test/resources/expected-classes/resolvers/Event.java.txt index fe0169a7c..35781becd 100644 --- a/src/test/resources/expected-classes/resolvers/Event.java.txt +++ b/src/test/resources/expected-classes/resolvers/Event.java.txt @@ -10,6 +10,8 @@ package com.github.graphql; ) public class Event implements java.io.Serializable { + private static final long serialVersionUID = 1L; + private String id; private String categoryId; private EventStatus status; diff --git a/src/test/resources/expected-classes/resolvers/EventProperty.java.txt b/src/test/resources/expected-classes/resolvers/EventProperty.java.txt index 38d6e199b..131431b50 100644 --- a/src/test/resources/expected-classes/resolvers/EventProperty.java.txt +++ b/src/test/resources/expected-classes/resolvers/EventProperty.java.txt @@ -10,6 +10,8 @@ package com.github.graphql; ) public class EventProperty implements java.io.Serializable { + private static final long serialVersionUID = 1L; + private Double floatVal; public EventProperty() { @@ -62,4 +64,4 @@ public class EventProperty implements java.io.Serializable { } } -} \ No newline at end of file +} diff --git a/src/test/resources/expected-classes/restricted-words/Query.java.txt b/src/test/resources/expected-classes/restricted-words/Query.java.txt index 4aef5e501..3edd0cf5d 100644 --- a/src/test/resources/expected-classes/restricted-words/Query.java.txt +++ b/src/test/resources/expected-classes/restricted-words/Query.java.txt @@ -10,6 +10,8 @@ import java.util.StringJoiner; ) public class Query implements java.io.Serializable { + private static final long serialVersionUID = 1L; + private String Native; public Query() { @@ -74,4 +76,4 @@ public class Query implements java.io.Serializable { } } -} \ No newline at end of file +} diff --git a/src/test/resources/expected-classes/restricted-words/Synchronized.java.txt b/src/test/resources/expected-classes/restricted-words/Synchronized.java.txt index 0df3320c5..14206a901 100644 --- a/src/test/resources/expected-classes/restricted-words/Synchronized.java.txt +++ b/src/test/resources/expected-classes/restricted-words/Synchronized.java.txt @@ -10,6 +10,8 @@ import java.util.StringJoiner; ) public class Synchronized implements java.io.Serializable { + private static final long serialVersionUID = 1L; + private String Void; private Char wait; @@ -93,4 +95,4 @@ public class Synchronized implements java.io.Serializable { } } -} \ No newline at end of file +} diff --git a/src/test/resources/expected-classes/types-as-interfaces-extends-interface/Profile.java.txt b/src/test/resources/expected-classes/types-as-interfaces-extends-interface/Profile.java.txt index 8aae6800d..59ba1109e 100644 --- a/src/test/resources/expected-classes/types-as-interfaces-extends-interface/Profile.java.txt +++ b/src/test/resources/expected-classes/types-as-interfaces-extends-interface/Profile.java.txt @@ -7,6 +7,8 @@ package com.github.graphql; ) public class Profile implements java.io.Serializable { + private static final long serialVersionUID = 1L; + @javax.validation.constraints.NotNull private String firstName; @javax.validation.constraints.NotNull diff --git a/src/test/resources/expected-classes/types-as-interfaces-parametrized/Foo.java.txt b/src/test/resources/expected-classes/types-as-interfaces-parametrized/Foo.java.txt new file mode 100644 index 000000000..554307732 --- /dev/null +++ b/src/test/resources/expected-classes/types-as-interfaces-parametrized/Foo.java.txt @@ -0,0 +1,16 @@ +package com.github.graphql; + + +@javax.annotation.Generated( + value = "com.kobylynskyi.graphql.codegen.GraphQLCodegen", + date = "2020-12-31T23:59:59-0500" +) +public interface Foo { + + @javax.validation.constraints.NotNull + String getSimpleField(); + + @javax.validation.constraints.NotNull + String getParameterizedField(int count); + +} \ No newline at end of file diff --git a/src/test/resources/expected-classes/unknown-fields/InputWithDefaults.java.txt b/src/test/resources/expected-classes/unknown-fields/InputWithDefaults.java.txt new file mode 100644 index 000000000..0629ea8a0 --- /dev/null +++ b/src/test/resources/expected-classes/unknown-fields/InputWithDefaults.java.txt @@ -0,0 +1,224 @@ +package com.kobylynskyi.graphql.testdefaults; + + +/** + * This input has all possible types + */ +@javax.annotation.Generated( + value = "com.kobylynskyi.graphql.codegen.GraphQLCodegen", + date = "2020-12-31T23:59:59-0500" +) +public class InputWithDefaults implements java.io.Serializable { + + private static final long serialVersionUID = 1L; + + private Double floatVal = 1.23; + private Boolean booleanVal = false; + private Integer intVal = 42; + private String stringVal = "my-default"; + private MyEnum enumVal = MyEnum.ONE; + @javax.validation.constraints.NotNull + private MyEnum nonNullEnumVal = MyEnum.TWO; + private SomeObject objectWithNullDefault = null; + private SomeObject objectWithNonNullDefault; + private java.util.List intList = java.util.Arrays.asList(1, 2, 3); + private java.util.List intListEmptyDefault = java.util.Collections.emptyList(); + @javax.validation.constraints.NotNull + private java.util.List objectListEmptyDefault = java.util.Collections.emptyList(); + @com.fasterxml.jackson.annotation.JsonAnyGetter + @com.fasterxml.jackson.annotation.JsonAnySetter + private java.util.Map userDefinedFields; + + public InputWithDefaults() { + } + + public InputWithDefaults(Double floatVal, Boolean booleanVal, Integer intVal, String stringVal, MyEnum enumVal, MyEnum nonNullEnumVal, SomeObject objectWithNullDefault, SomeObject objectWithNonNullDefault, java.util.List intList, java.util.List intListEmptyDefault, java.util.List objectListEmptyDefault, java.util.Map userDefinedFields) { + this.floatVal = floatVal; + this.booleanVal = booleanVal; + this.intVal = intVal; + this.stringVal = stringVal; + this.enumVal = enumVal; + this.nonNullEnumVal = nonNullEnumVal; + this.objectWithNullDefault = objectWithNullDefault; + this.objectWithNonNullDefault = objectWithNonNullDefault; + this.intList = intList; + this.intListEmptyDefault = intListEmptyDefault; + this.objectListEmptyDefault = objectListEmptyDefault; + this.userDefinedFields = userDefinedFields; + } + + public Double getFloatVal() { + return floatVal; + } + public void setFloatVal(Double floatVal) { + this.floatVal = floatVal; + } + + public Boolean getBooleanVal() { + return booleanVal; + } + public void setBooleanVal(Boolean booleanVal) { + this.booleanVal = booleanVal; + } + + public Integer getIntVal() { + return intVal; + } + public void setIntVal(Integer intVal) { + this.intVal = intVal; + } + + public String getStringVal() { + return stringVal; + } + public void setStringVal(String stringVal) { + this.stringVal = stringVal; + } + + public MyEnum getEnumVal() { + return enumVal; + } + public void setEnumVal(MyEnum enumVal) { + this.enumVal = enumVal; + } + + public MyEnum getNonNullEnumVal() { + return nonNullEnumVal; + } + public void setNonNullEnumVal(MyEnum nonNullEnumVal) { + this.nonNullEnumVal = nonNullEnumVal; + } + + public SomeObject getObjectWithNullDefault() { + return objectWithNullDefault; + } + public void setObjectWithNullDefault(SomeObject objectWithNullDefault) { + this.objectWithNullDefault = objectWithNullDefault; + } + + public SomeObject getObjectWithNonNullDefault() { + return objectWithNonNullDefault; + } + public void setObjectWithNonNullDefault(SomeObject objectWithNonNullDefault) { + this.objectWithNonNullDefault = objectWithNonNullDefault; + } + + public java.util.List getIntList() { + return intList; + } + public void setIntList(java.util.List intList) { + this.intList = intList; + } + + public java.util.List getIntListEmptyDefault() { + return intListEmptyDefault; + } + public void setIntListEmptyDefault(java.util.List intListEmptyDefault) { + this.intListEmptyDefault = intListEmptyDefault; + } + + public java.util.List getObjectListEmptyDefault() { + return objectListEmptyDefault; + } + public void setObjectListEmptyDefault(java.util.List objectListEmptyDefault) { + this.objectListEmptyDefault = objectListEmptyDefault; + } + + public java.util.Map getUserDefinedFields() { + return userDefinedFields; + } + public void setUserDefinedFields(java.util.Map userDefinedFields) { + this.userDefinedFields = userDefinedFields; + } + + + + public static InputWithDefaults.Builder builder() { + return new InputWithDefaults.Builder(); + } + + public static class Builder { + + private Double floatVal = 1.23; + private Boolean booleanVal = false; + private Integer intVal = 42; + private String stringVal = "my-default"; + private MyEnum enumVal = MyEnum.ONE; + private MyEnum nonNullEnumVal = MyEnum.TWO; + private SomeObject objectWithNullDefault = null; + private SomeObject objectWithNonNullDefault; + private java.util.List intList = java.util.Arrays.asList(1, 2, 3); + private java.util.List intListEmptyDefault = java.util.Collections.emptyList(); + private java.util.List objectListEmptyDefault = java.util.Collections.emptyList(); + private java.util.Map userDefinedFields; + + public Builder() { + } + + public Builder setFloatVal(Double floatVal) { + this.floatVal = floatVal; + return this; + } + + public Builder setBooleanVal(Boolean booleanVal) { + this.booleanVal = booleanVal; + return this; + } + + public Builder setIntVal(Integer intVal) { + this.intVal = intVal; + return this; + } + + public Builder setStringVal(String stringVal) { + this.stringVal = stringVal; + return this; + } + + public Builder setEnumVal(MyEnum enumVal) { + this.enumVal = enumVal; + return this; + } + + public Builder setNonNullEnumVal(MyEnum nonNullEnumVal) { + this.nonNullEnumVal = nonNullEnumVal; + return this; + } + + public Builder setObjectWithNullDefault(SomeObject objectWithNullDefault) { + this.objectWithNullDefault = objectWithNullDefault; + return this; + } + + public Builder setObjectWithNonNullDefault(SomeObject objectWithNonNullDefault) { + this.objectWithNonNullDefault = objectWithNonNullDefault; + return this; + } + + public Builder setIntList(java.util.List intList) { + this.intList = intList; + return this; + } + + public Builder setIntListEmptyDefault(java.util.List intListEmptyDefault) { + this.intListEmptyDefault = intListEmptyDefault; + return this; + } + + public Builder setObjectListEmptyDefault(java.util.List objectListEmptyDefault) { + this.objectListEmptyDefault = objectListEmptyDefault; + return this; + } + + public Builder setUserDefinedFields(java.util.Map userDefinedFields) { + this.userDefinedFields = userDefinedFields; + return this; + } + + + public InputWithDefaults build() { + return new InputWithDefaults(floatVal, booleanVal, intVal, stringVal, enumVal, nonNullEnumVal, objectWithNullDefault, objectWithNonNullDefault, intList, intListEmptyDefault, objectListEmptyDefault, userDefinedFields); + } + + } +} \ No newline at end of file diff --git a/src/test/resources/expected-classes/unknown-fields/MyEnum.java.txt b/src/test/resources/expected-classes/unknown-fields/MyEnum.java.txt new file mode 100644 index 000000000..489176b8f --- /dev/null +++ b/src/test/resources/expected-classes/unknown-fields/MyEnum.java.txt @@ -0,0 +1,24 @@ +package com.kobylynskyi.graphql.testdefaults; + +@javax.annotation.Generated( + value = "com.kobylynskyi.graphql.codegen.GraphQLCodegen", + date = "2020-12-31T23:59:59-0500" +) +public enum MyEnum { + + ONE("ONE"), + TWO("TWO"), + THREE("THREE"); + + private final String graphqlName; + + private MyEnum(String graphqlName) { + this.graphqlName = graphqlName; + } + + @Override + public String toString() { + return this.graphqlName; + } + +} \ No newline at end of file diff --git a/src/test/resources/expected-classes/unknown-fields/SomeObject.java.txt b/src/test/resources/expected-classes/unknown-fields/SomeObject.java.txt new file mode 100644 index 000000000..0b6ecfb65 --- /dev/null +++ b/src/test/resources/expected-classes/unknown-fields/SomeObject.java.txt @@ -0,0 +1,70 @@ +package com.kobylynskyi.graphql.testdefaults; + + +@javax.annotation.Generated( + value = "com.kobylynskyi.graphql.codegen.GraphQLCodegen", + date = "2020-12-31T23:59:59-0500" +) +public class SomeObject implements java.io.Serializable { + + private static final long serialVersionUID = 1L; + + @javax.validation.constraints.NotNull + private String name; + @com.fasterxml.jackson.annotation.JsonAnyGetter + @com.fasterxml.jackson.annotation.JsonAnySetter + private java.util.Map userDefinedFields; + + public SomeObject() { + } + + public SomeObject(String name, java.util.Map userDefinedFields) { + this.name = name; + this.userDefinedFields = userDefinedFields; + } + + public String getName() { + return name; + } + public void setName(String name) { + this.name = name; + } + + public java.util.Map getUserDefinedFields() { + return userDefinedFields; + } + public void setUserDefinedFields(java.util.Map userDefinedFields) { + this.userDefinedFields = userDefinedFields; + } + + + + public static SomeObject.Builder builder() { + return new SomeObject.Builder(); + } + + public static class Builder { + + private String name; + private java.util.Map userDefinedFields; + + public Builder() { + } + + public Builder setName(String name) { + this.name = name; + return this; + } + + public Builder setUserDefinedFields(java.util.Map userDefinedFields) { + this.userDefinedFields = userDefinedFields; + return this; + } + + + public SomeObject build() { + return new SomeObject(name, userDefinedFields); + } + + } +} \ No newline at end of file diff --git a/src/test/resources/schemas/kt/default.graphqls b/src/test/resources/schemas/kt/default.graphqls new file mode 100644 index 000000000..95019f4c6 --- /dev/null +++ b/src/test/resources/schemas/kt/default.graphqls @@ -0,0 +1,7 @@ +type Query { + friends(num: Int!): [Friend] +} + +type Friend { + name: String +} \ No newline at end of file diff --git a/src/test/resources/schemas/types-as-interfaces-parametrized.graphqls b/src/test/resources/schemas/types-as-interfaces-parametrized.graphqls new file mode 100644 index 000000000..67d7ba604 --- /dev/null +++ b/src/test/resources/schemas/types-as-interfaces-parametrized.graphqls @@ -0,0 +1,14 @@ +""" +used with typesAsInterfaces config +""" +directive @asInterface on OBJECT + +""" +used with fieldsWithResolvers config +""" +directive @noResolver on FIELD_DEFINITION + +type Foo @asInterface { + simpleField: String! + parameterizedField(count: Int!): String! @noResolver +} \ No newline at end of file