Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ public String capitalizeIfRestricted(MappingContext mappingContext, String field
if (SCALA_RESTRICTED_KEYWORDS.contains(fieldName)) {
return wrapString(fieldName, RESTRICTED_WORDS_WRAP_WITH);
}

// Scala case class's Fields cannot also use the names of these methods.
if (SCALA_RESTRICTED_METHOD_NAMES.contains(fieldName)) {
return Utils.capitalize(fieldName);
}

return fieldName;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@ open class ${className}()<#if implements?has_content> : <#list implements as int
joiner.add("${field.originalName}: " + GraphQLRequestSerializer.getEntry(${field.name}))
<#else>
<#if field.type == "String?">
joiner.add("${field.originalName}: \"${field.name}\"")
joiner.add("${field.originalName}: \"" + ${field.name} + "\"");
<#else>
joiner.add(${field.originalName}: ${field.name}")
joiner.add("${field.originalName}: " + ${field.name});
</#if>
</#if>
}
Expand All @@ -102,9 +102,9 @@ open class ${className}()<#if implements?has_content> : <#list implements as int
joiner.add("${field.originalName}: " + GraphQLRequestSerializer.getEntry(${field.name}))
<#else>
<#if field.type == "String">
joiner.add("${field.originalName}: \"${field.name}\"")
joiner.add("${field.originalName}: \"" + ${field.name} + "\"");
<#else>
joiner.add(${field.originalName}: ${field.name}")
joiner.add("${field.originalName}: " + ${field.name});
</#if>
</#if>
</#if>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,25 @@ void generatePrimitiveTypesResponseResolverClasses_nullable_WITH_Prefix_Suffix()
new File("src/test/resources/expected-classes/kt/nullable/QueryResolver_WITH_Prefix_Suffix.kt.txt"),
getFileByName(files, "QueryResolver.kt"));
}
}

/**
* Verify Int to override Int?
*/
@Test
void generate_OptionalFieldInInterfaceAndMandatoryInType() throws Exception {
mappingConfig.setGenerateBuilder(true);
mappingConfig.setGenerateToString(true);
mappingConfig.setGenerateEqualsAndHashCode(true);
schemaFinder.setIncludePattern("optional-vs-mandatory-types.graphqls");

new KotlinGraphQLCodegen(schemaFinder.findSchemas(), outputBuildDir, mappingConfig, TestUtils.getStaticGeneratedInfo())
.generate();

File[] files = Objects.requireNonNull(outputJavaClassesDir.listFiles());

assertSameTrimmedContent(new File("src/test/resources/expected-classes/kt/optional/InterfaceWithOptionalField.kt.txt"),
getFileByName(files, "InterfaceWithOptionalField.kt"));
assertSameTrimmedContent(new File("src/test/resources/expected-classes/kt/optional/TypeWithMandatoryField.kt.txt"),
getFileByName(files, "TypeWithMandatoryField.kt"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@

import java.io.File;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import java.util.*;

import static com.kobylynskyi.graphql.codegen.TestUtils.assertSameTrimmedContent;
import static com.kobylynskyi.graphql.codegen.TestUtils.getFileByName;
Expand Down Expand Up @@ -217,4 +214,29 @@ void generatePrimitiveTypesResponseResolverClasses_nullable() throws Exception {
new File("src/test/resources/expected-classes/scala/extend/nullable/QueryResolver.scala.txt"),
getFileByName(files, "QueryResolver.scala"));
}

/**
* Verify Int! to override Int in graphql.
* In scala, Int type cannot be null, So, option Int is Option[Int]. Can not override from Option[Int] to Int.
*/
@Test
void generate_OptionalFieldInInterfaceAndMandatoryInType() throws Exception {
mappingConfig.setGenerateBuilder(true);
mappingConfig.setGenerateToString(true);
mappingConfig.setGenerateEqualsAndHashCode(true);
Map<String, String> maps = new HashMap<>();
maps.put("Int", "Int");// Now, That's the only way to do it.
mappingConfig.setCustomTypesMapping(maps);
schemaFinder.setIncludePattern("optional-vs-mandatory-types.graphqls");

new ScalaGraphQLCodegen(schemaFinder.findSchemas(), outputBuildDir, mappingConfig, TestUtils.getStaticGeneratedInfo())
.generate();

File[] files = Objects.requireNonNull(outputJavaClassesDir.listFiles());

assertSameTrimmedContent(new File("src/test/resources/expected-classes/scala/optional/InterfaceWithOptionalField.scala.txt"),
getFileByName(files, "InterfaceWithOptionalField.scala"));
assertSameTrimmedContent(new File("src/test/resources/expected-classes/scala/optional/TypeWithMandatoryField.scala.txt"),
getFileByName(files, "TypeWithMandatoryField.scala"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,32 @@ void generate_SetGenerateClient_TRUE() throws Exception {
}
}

@Test
void generate_SetGenerateClient_False() throws Exception {
mappingConfig.setPackageName("com.kobylynskyi.graphql.codegen.prot");
mappingConfig.setGenerateEqualsAndHashCode(true);
mappingConfig.setGenerateClient(false);
mappingConfig.setGenerateToString(true);
mappingConfig.setUseObjectMapperForRequestSerialization(singleton("TestEnum"));
mappingConfig.putCustomTypeMappingIfAbsent("DateTime", "java.time.ZonedDateTime");
mappingConfig.setGenerateApis(true);
mappingConfig.setGenerateModelsForRootTypes(true);
mappingConfig.setApiNameSuffix("API");

new ScalaGraphQLCodegen(singletonList("src/test/resources/schemas/scala/restricted-words.graphqls"),
outputBuildDir, mappingConfig, TestUtils.getStaticGeneratedInfo()).generate();

File[] files = Objects.requireNonNull(outputJavaClassesDir.listFiles());
List<String> generatedFileNames = Arrays.stream(files).map(File::getName).filter(f -> Objects.equals("Synchronized.scala", f)).sorted().collect(toList());
assertEquals(singletonList("Synchronized.scala"), generatedFileNames);

for (File file : files) {
if (Objects.equals("Synchronized.scala", file.getName())) {
assertSameTrimmedContent(
new File(String.format("src/test/resources/expected-classes/scala/tostring/%s.txt", "TOSTRING_Synchronized.scala")),
file);
}
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
@javax.annotation.Generated(
value = ["com.kobylynskyi.graphql.codegen.GraphQLCodegen"],
date = "2020-12-31T23:59:59-0500"
)
interface InterfaceWithOptionalField {

val test: Int?

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import java.util.StringJoiner

@javax.annotation.Generated(
value = ["com.kobylynskyi.graphql.codegen.GraphQLCodegen"],
date = "2020-12-31T23:59:59-0500"
)
data class TypeWithMandatoryField(
override
val test: Int
) : InterfaceWithOptionalField {

companion object {
fun builder(): Builder = Builder()
}

// In the future, it maybe change.
override fun toString(): String {
val joiner = StringJoiner(", ", "{ ", " }")
joiner.add("test: " + test);
return joiner.toString()
}

class Builder {

private var test: Int = 0

fun setTest(test: Int): Builder {
this.test = test
return this
}

fun build(): TypeWithMandatoryField {
return TypeWithMandatoryField(test)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
@javax.annotation.Generated(
value = Array("com.kobylynskyi.graphql.codegen.GraphQLCodegen"),
date = "2020-12-31T23:59:59-0500"
)
trait InterfaceWithOptionalField {

val test: Int

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import scala.collection.JavaConverters._

@javax.annotation.Generated(
value = Array("com.kobylynskyi.graphql.codegen.GraphQLCodegen"),
date = "2020-12-31T23:59:59-0500"
)
case class TypeWithMandatoryField(
override val test: Int
) extends InterfaceWithOptionalField {

override def toString(): String = {
Seq(
"test: " + test
).filter(_ != "").mkString("{", ",", "}")
}
}

object TypeWithMandatoryField {

def builder(): TypeWithMandatoryField.Builder = new Builder()

class Builder {

private var test: Int = _

def setTest(test: Int): Builder = {
this.test = test
this
}

def build(): TypeWithMandatoryField = TypeWithMandatoryField(test)

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import TestEnum._
)
case class Synchronized(
void: String,
wait: Char,
Wait: Char,
`this`: Char,
`super`: Char,
`private`: Char,
Expand All @@ -24,7 +24,7 @@ case class Synchronized(
override def toString(): String = {
Seq(
if (void != null) "void: " + GraphQLRequestSerializer.getEntry(void) else "",
"wait: " + GraphQLRequestSerializer.getEntry(wait),
"wait: " + GraphQLRequestSerializer.getEntry(Wait),
"this: " + GraphQLRequestSerializer.getEntry(`this`),
"super: " + GraphQLRequestSerializer.getEntry(`super`),
"private: " + GraphQLRequestSerializer.getEntry(`private`),
Expand All @@ -44,7 +44,7 @@ object Synchronized {
class Builder {

private var void: String = _
private var wait: Char = _
private var Wait: Char = _
private var `this`: Char = _
private var `super`: Char = _
private var `private`: Char = _
Expand All @@ -59,8 +59,8 @@ object Synchronized {
this
}

def setWait(wait: Char): Builder = {
this.wait = wait
def setWait(Wait: Char): Builder = {
this.Wait = Wait
this
}

Expand Down Expand Up @@ -104,7 +104,7 @@ object Synchronized {
this
}

def build(): Synchronized = Synchronized(void, wait, `this`, `super`, `private`, native, that, enum, Synchronized, date)
def build(): Synchronized = Synchronized(void, Wait, `this`, `super`, `private`, native, that, enum, Synchronized, date)

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.kobylynskyi.graphql.codegen.prot

import scala.collection.JavaConverters._
import TestEnum._

@javax.annotation.Generated(
value = Array("com.kobylynskyi.graphql.codegen.GraphQLCodegen"),
date = "2020-12-31T23:59:59-0500"
)
case class Synchronized(
void: String,
Wait: Char,
`this`: Char,
`super`: Char,
`private`: Char,
native: Char,
that: Char,
enum: TestEnum,
Synchronized: Synchronized,
date: java.time.ZonedDateTime
) {

override def toString(): String = {
Seq(
if (void != null) "void: \"void\"" else "",
"wait: " + Wait,
"this: " + `this`,
"super: " + `super`,
"private: " + `private`,
"native: " + native,
"that: " + that,
if (enum != null) "enum: enum" else "",
if (Synchronized != null) "Synchronized: Synchronized" else "",
if (date != null) "date: date" else ""
).filter(_ != "").mkString("{", ",", "}")
}
}