Skip to content

Commit

Permalink
fix(kotlin): Enum names must not contain typesPrefix or typesSuffix (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
tristanlins committed Feb 2, 2024
1 parent 40f3676 commit 926f515
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/curly-brooms-hunt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphql-codegen/kotlin': patch
---

fix(kotlin) Omit typesSuffix in enum names.
1 change: 1 addition & 0 deletions packages/plugins/java/kotlin/src/visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ export class KotlinResolversVisitor extends BaseVisitor<
return indent(
`${this.convertName(node, {
useTypesPrefix: false,
useTypesSuffix: false,
transformUnderscore: true,
})}("${this.getEnumValue(enumName, node.name.value)}")`,
);
Expand Down
46 changes: 46 additions & 0 deletions packages/plugins/java/kotlin/tests/kotlin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ describe('Kotlin', () => {
username: String
email: String
name: String
role: UserRole = USER
sort: ResultSort
metadata: MetadataSearch
}
Expand Down Expand Up @@ -154,6 +155,49 @@ describe('Kotlin', () => {
}
}`);
});

it('Should omit typesPrefix/typesSuffix in enum names if the option is set', async () => {
const result = await plugin(
schema,
[],
{ typesPrefix: 'Graphql', typesSuffix: 'Type' },
{ outputFile: OUTPUT_FILE },
);

// language=kotlin
expect(result).toBeSimilarStringTo(` enum class GraphqlUserRoleType(val label: String) {
Admin("ADMIN"),
User("USER"),
Editor("EDITOR");
companion object {
@JvmStatic
fun valueOfLabel(label: String): GraphqlUserRoleType? {
return values().find { it.label == label }
}
}
}`);

// language=kotlin
expect(result).toBeSimilarStringTo(`data class GraphqlSearchUserTypeInput(
val username: String? = null,
val email: String? = null,
val name: String? = null,
val role: GraphqlUserRoleType? = GraphqlUserRoleType.USER,
val sort: GraphqlResultSortType? = null,
val metadata: GraphqlMetadataSearchTypeInput? = null
) {
@Suppress("UNCHECKED_CAST")
constructor(args: Map<String, Any>) : this(
args["username"] as String?,
args["email"] as String?,
args["name"] as String?,
args["role"] as GraphqlUserRoleType? ?: GraphqlUserRoleType.USER,
args["sort"] as GraphqlResultSortType?,
args["metadata"]?.let { GraphqlMetadataSearchTypeInput(it as Map<String, Any>) }
)
}`);
});
});

describe('Input Types / Arguments', () => {
Expand Down Expand Up @@ -243,6 +287,7 @@ describe('Kotlin', () => {
val username: String? = null,
val email: String? = null,
val name: String? = null,
val role: UserRole? = UserRole.USER,
val sort: ResultSort? = null,
val metadata: MetadataSearchInput? = null
) {
Expand All @@ -251,6 +296,7 @@ describe('Kotlin', () => {
args["username"] as String?,
args["email"] as String?,
args["name"] as String?,
args["role"] as UserRole? ?: UserRole.USER,
args["sort"] as ResultSort?,
args["metadata"]?.let { MetadataSearchInput(it as Map<String, Any>) }
)
Expand Down

0 comments on commit 926f515

Please sign in to comment.