Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
babyfish-ct committed Dec 2, 2024
1 parent a641c25 commit 197d3e1
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 43 deletions.
2 changes: 1 addition & 1 deletion project/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
group=org.babyfish.jimmer
version=0.9.23
version=0.9.24
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.TypeElement;
import java.util.HashSet;
import java.util.Set;
import java.util.*;

public class InputBuilderGenerator {

private static final String JACKSON_ANNO_PREFIX = "com.fasterxml.jackson.databind.annotation.";
private static final String[] JACKSON_ANNO_PREFIXIES = new String[] {
"com.fasterxml.jackson.databind.annotation.",
"com.fasterxml.jackson.annotation."
};

private final DtoGenerator parentGenerator;

Expand Down Expand Up @@ -145,8 +147,7 @@ private void addJacksonAnnotations(
) {
Set<String> typeNames = new HashSet<>();
for (Anno anno : prop.getAnnotations()) {
if (anno.getQualifiedName().startsWith(JACKSON_ANNO_PREFIX) &&
typeNames.add(anno.getQualifiedName())) {
if (isJacksonQualifiedName(anno.getQualifiedName()) && typeNames.add(anno.getQualifiedName())) {
builder.addAnnotation(DtoGenerator.annotationOf(anno));
}
}
Expand All @@ -157,8 +158,7 @@ private void addJacksonAnnotations(
.asElement())
.getQualifiedName()
.toString();
if (qualifiedName.startsWith(JACKSON_ANNO_PREFIX) &&
typeNames.add(qualifiedName)) {
if (isJacksonQualifiedName(qualifiedName) && typeNames.add(qualifiedName)) {
builder.addAnnotation(AnnotationSpec.get(mirror));
}
}
Expand Down Expand Up @@ -247,4 +247,13 @@ private static boolean isFieldNullable(AbstractProp prop) {
}
return true;
}

private static boolean isJacksonQualifiedName(String qualifiedName) {
for (String prefix : JACKSON_ANNO_PREFIXIES) {
if (qualifiedName.startsWith(prefix)) {
return true;
}
}
return false;
}
}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.9.23
0.9.24
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,10 @@ class InputBuilderGenerator(

companion object {

private val JACKSON_ANNO_PREFIX = "com.fasterxml.jackson.databind.annotation."
private val JACKSON_ANNO_PREFIXIES = arrayOf(
"com.fasterxml.jackson.databind.annotation.",
"com.fasterxml.jackson.annotation."
)

private fun isFieldNullable(prop: AbstractProp): Boolean =
prop !is DtoProp<*, *> || (prop.funcName != "null" && prop.funcName != "notNull")
Expand All @@ -204,18 +207,19 @@ class InputBuilderGenerator(
private fun FunSpec.Builder.addJacksonAnnotations(prop: AbstractProp) {
val typeNames = mutableSetOf<String>()
for (anno in prop.annotations) {
if (anno.qualifiedName.startsWith(JACKSON_ANNO_PREFIX) &&
if (JACKSON_ANNO_PREFIXIES.any { anno.qualifiedName.startsWith(it) } &&
typeNames.add(anno.qualifiedName)) {
addAnnotation(DtoGenerator.annotationOf(anno))
}
}
if (prop is DtoProp<*, *>) {
val dtoProp = prop as DtoProp<*, ImmutableProp>
for (anno in dtoProp.toTailProp().baseProp.annotations {
it.fullName.startsWith(JACKSON_ANNO_PREFIX) &&
typeNames.add(it.fullName)
for (anno in dtoProp.toTailProp().baseProp.annotations { an ->
JACKSON_ANNO_PREFIXIES.any { an.fullName.startsWith(it) }
}) {
addAnnotation(anno.toAnnotationSpec())
if (typeNames.add(anno.fullName)) {
addAnnotation(anno.toAnnotationSpec())
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ private static Dialect getDialectOrNullForDriver(@NotNull DatabaseDriver driver)
return new SqlServerDialect();
case H2:
return new H2Dialect();
case SQLITE:
return new SQLiteDialect();
default:
return null;
}
Expand Down
12 changes: 12 additions & 0 deletions project/jimmer-sql-kotlin/src/test/dto/Administrator.dto
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
export org.babyfish.jimmer.sql.kt.model.inheritance.Administrator
-> package org.babyfish.jimmer.sql.kt.model.inheritance.dto

import com.fasterxml.jackson.annotation.JsonFormat

static input AdministratorInputForIssue684 {
fuzzy metadata {
name
}
}

input AdministratorInputForIssue819 {
#allScalars

@JsonFormat(pattern = "yyyy/MM/dd HH-mm-ss")
createdTime

@JsonFormat(pattern = "yyyy/MM/dd HH-mm-ss")
modifiedTime
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package org.babyfish.jimmer.sql.kt.dto

import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule
import org.babyfish.jimmer.sql.kt.common.assertContent
import org.babyfish.jimmer.sql.kt.model.inheritance.dto.AdministratorInputForIssue684
import org.babyfish.jimmer.sql.kt.model.inheritance.dto.AdministratorInputForIssue819
import org.junit.Test
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
import kotlin.test.expect

class AdministratorInputTest {

@Test
fun testNullForIssue684() {
expect(
"{}"
) {
AdministratorInputForIssue684().toEntity().toString()
}
}

@Test
fun testNonNullForIssue684() {
expect(
"{\"metadata\":{\"name\":\"Metadata\"}}"
) {
AdministratorInputForIssue684(
metadata = AdministratorInputForIssue684.TargetOf_metadata("Metadata")
).toEntity().toString()
}
}

@Test
fun testIssueFor819() {
val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
val mapper = ObjectMapper().registerModule(JavaTimeModule())
val input = AdministratorInputForIssue819(
id = 10L,
name = "SuperAdmin",
createdTime = LocalDateTime.parse("2024-12-02 13:07:24", formatter),
modifiedTime = LocalDateTime.parse("2024-12-03 02:00:14", formatter),
)
val json = mapper.writeValueAsString(input)
assertContent(
"""{"
|--->id":10,
|--->"name":"SuperAdmin",
|--->"createdTime":"2024/12/02 13-07-24",
|--->"modifiedTime":"2024/12/03 02-00-14"
|}""".trimMargin(),
json
)
val input2 = mapper.readValue(json, AdministratorInputForIssue819::class.java)
assertContent(
"""AdministratorInputForIssue819(
|--->id=10,
|--->name=SuperAdmin,
|--->createdTime=2024-12-02T13:07:24,
|--->modifiedTime=2024-12-03T02:00:14)""".trimMargin(),
input2
)
}
}

0 comments on commit 197d3e1

Please sign in to comment.