Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify annotation position for kotlin #1264

Merged
merged 1 commit into from
Jul 10, 2023
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 @@ -32,7 +32,7 @@ data class ${className}(
<#if field.deprecated?has_content>
@${field.deprecated.annotation}(message = "${field.deprecated.reason}")
</#if>
<#list field.annotations as annotation>@get:${annotation}
<#list field.annotations as annotation>@field:${annotation}
</#list>val ${field.name}: ${field.type}<#if field.defaultValue?has_content> = ${field.defaultValue}</#if><#if field_has_next>,</#if>
</#list>
</#if>
Expand Down
4 changes: 2 additions & 2 deletions src/main/resources/templates/kotlin-lang/type.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ open class ${className}()<#if implements?has_content> : <#list implements as int
@${field.deprecated.annotation}(message = "${field.deprecated.reason}")
</#if><#-- Properties of multiple interfaces should not have duplicate names -->
<#if parentInterfaces?has_content><#list parentInterfaces as parent><#if parent == field.name>override
</#if></#list></#if><#if !immutableModels><#list field.annotations as annotation>@get:${annotation}
</#list>var <#else><#list field.annotations as annotation>@get:${annotation}
</#if></#list></#if><#if !immutableModels><#list field.annotations as annotation>@field:${annotation}
</#list>var <#else><#list field.annotations as annotation>@field:${annotation}
</#list>val </#if>${field.name}: ${field.type}<#if field.defaultValue?has_content> = ${field.defaultValue}<#elseif field.type?ends_with("?") && (initializeNullableTypes == true)> = null</#if><#if field_has_next>,</#if>
</#list>
</#if>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package com.kobylynskyi.graphql.codegen.kotlin;

import com.kobylynskyi.graphql.codegen.MaxQueryTokensExtension;
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 org.junit.jupiter.api.extension.ExtendWith;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Objects;

import static com.kobylynskyi.graphql.codegen.TestUtils.assertFileContainsElements;
import static java.lang.System.lineSeparator;
import static java.util.Collections.singletonList;
import static java.util.Collections.singletonMap;

@ExtendWith(MaxQueryTokensExtension.class)
class GraphQLCodegenAnnotationsTest {

private final File outputBuildDir = new File("build/generated");
private final File outputJavaClassesDir = new File("build/generated/com/kobylynskyi/graphql/test1");

private MappingConfig mappingConfig;

@BeforeEach
void init() {
mappingConfig = new MappingConfig();
mappingConfig.setPackageName("com.kobylynskyi.graphql.test1");
mappingConfig.setGenerateParameterizedFieldsResolvers(false);
mappingConfig.setGeneratedLanguage(GeneratedLanguage.KOTLIN);
}

@AfterEach
void cleanup() {
Utils.deleteDir(outputBuildDir);
}

@Test
void generate_CustomAnnotationMappings() throws Exception {
mappingConfig.setCustomTypesMapping(new HashMap<>(
singletonMap("Event.createdDateTime", "org.joda.time.DateTime")));
mappingConfig.setCustomAnnotationsMapping(new HashMap<>(singletonMap("Event.createdDateTime",
singletonList("@com.fasterxml.jackson.databind.annotation.JsonDeserialize(" +
"using = com.example.json.DateTimeScalarDeserializer.class)"))));

generate("src/test/resources/schemas/test.graphqls");

File[] files = Objects.requireNonNull(outputJavaClassesDir.listFiles());
assertFileContainsElements(files, "Event.kt",
"@field:com.fasterxml.jackson.databind.annotation.JsonDeserialize(",
"using = com.example.json.DateTimeScalarDeserializer.class)",
" val createdDateTime: org.joda.time.DateTime?");
}

@Test
void generate_CustomAnnotationMappings_Type() throws Exception {
mappingConfig.setCustomTypesMapping(new HashMap<>(singletonMap("DateTime", "org.joda.time.DateTime")));
mappingConfig.setCustomAnnotationsMapping(new HashMap<>(singletonMap("DateTime",
singletonList("com.fasterxml.jackson.databind.annotation.JsonDeserialize(" +
"using = com.example.json.DateTimeScalarDeserializer.class)"))));

generate("src/test/resources/schemas/test.graphqls");

File[] files = Objects.requireNonNull(outputJavaClassesDir.listFiles());
assertFileContainsElements(files, "Event.kt",
"@field:com.fasterxml.jackson.databind.annotation.JsonDeserialize(",
"using = com.example.json.DateTimeScalarDeserializer.class)",
" val createdDateTime: org.joda.time.DateTime?");
}

@Test
void generate_CustomAnnotationMappings_Input() throws Exception {
mappingConfig.setCustomAnnotationsMapping(new HashMap<>(singletonMap("ReproInput.reproField",
singletonList("@com.fasterxml.jackson.annotation.JsonProperty(\"reproField\")"))));

generate("src/test/resources/schemas/input.graphqls");

File[] files = Objects.requireNonNull(outputJavaClassesDir.listFiles());
assertFileContainsElements(files, "ReproInput.kt",
" @field:com.fasterxml.jackson.annotation.JsonProperty(\"reproField\")" + lineSeparator() +
" val reproField: List<String>");
}

@Test
void generate_CustomAnnotationMappings_Regexp() throws Exception {
mappingConfig.setCustomTypesMapping(new HashMap<>(singletonMap("DateTime", "org.joda.time.DateTime")));
mappingConfig.setCustomAnnotationsMapping(new HashMap<>(singletonMap("Date.*",
singletonList("com.fasterxml.jackson.databind.annotation.JsonDeserialize(" +
"using = com.example.json.DateTimeScalarDeserializer.class)"))));

generate("src/test/resources/schemas/test.graphqls");

File[] files = Objects.requireNonNull(outputJavaClassesDir.listFiles());
assertFileContainsElements(files, "Event.kt",
"@field:com.fasterxml.jackson.databind.annotation.JsonDeserialize(",
"using = com.example.json.DateTimeScalarDeserializer.class)",
" val createdDateTime: org.joda.time.DateTime?");
}

@Test
void generate_CustomAnnotationMappings_FieldType() throws Exception {
mappingConfig.setCustomTypesMapping(new HashMap<>(singletonMap("DateTime", "org.joda.time.DateTime")));
mappingConfig.setCustomAnnotationsMapping(new HashMap<>(singletonMap("Event.createdDateTime",
singletonList("@com.fasterxml.jackson.databind.annotation.JsonDeserialize(" +
"using = com.example.json.DateTimeScalarDeserializer.class)"))));

generate("src/test/resources/schemas/test.graphqls");

File[] files = Objects.requireNonNull(outputJavaClassesDir.listFiles());
assertFileContainsElements(files, "Event.kt",
"@field:com.fasterxml.jackson.databind.annotation.JsonDeserialize(",
"using = com.example.json.DateTimeScalarDeserializer.class)",
" val createdDateTime: org.joda.time.DateTime?");
}

private void generate(String path) throws IOException {
new KotlinGraphQLCodegen(singletonList(path), outputBuildDir, mappingConfig,
TestUtils.getStaticGeneratedInfo(mappingConfig)).generate();
}

}