Skip to content

Conversation

@Nthalk
Copy link

@Nthalk Nthalk commented Dec 18, 2024

I have been using this library for years, and I use Tagged Unions extensively.
I have some situations where I have many sub-classes, and I would like to make writing code as compact and concise as possible.

Jackson supports different kinds of use=Id variants, however, only NAME, and CLASS are supported.

I originally tried to get MINIMAL_CLASS, and Jacksin 2.18's SIMPLE_NAME to work, however, I had problems with running the existing tests and building due to immutables not being on the compilation annotation path, and the fact that Java12+ isn't supported.

Fixed the immutables error by adding the annotation to the compilation path (this shouldn't affect the end jar).
Made building the project more error-obvious by introducing MavenEnforcer to demand it be built with Java 11.

Now that building and testing is easier, a Kotlin test is introduced (to allow testing for sealed things, as we are not building in Java17+ yet).

This allows us to write more minimal effort definitions like:

@JsonTypeInfo(use = JsonTypeInfo.Id.MINIMAL_CLASS)
    sealed interface MinimalEffort {
        data class A(
            val a: String
        ) : MinimalEffort

        data object B : MinimalEffort
    }

And if we are using Jackson 2.18+, we can use Id.SIMPLE_NAME as well.

It does this by, instead of detecting supported Id types, it detects non-compatible Id types, like CUSTOM, NONE, and DEDUCTION, which is a more forward compatible method.

Without this, one would need to annotate every class with @JsonTypeName(...) in the case of Id.NAME, or be stuck with the full class: Id.CLASS.

Before:
```
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] /Users/nthalk/local/src/nthalk/typescript-generator/typescript-generator-core/src/test/java/cz/habarta/typescript/generator/ImmutablesTest.java:[73,69] package ImmutableRectangle does not exist
[ERROR] /Users/nthalk/local/src/nthalk/typescript-generator/typescript-generator-core/src/test/java/cz/habarta/typescript/generator/ImmutablesTest.java:[82,52] package ImmutableCircle does not exist
[ERROR] /Users/nthalk/local/src/nthalk/typescript-generator/typescript-generator-core/src/test/java/cz/habarta/typescript/generator/ImmutablesTest.java:[62,25] cannot find symbol
  symbol:   class ImmutableRectangle
  location: class cz.habarta.typescript.generator.ImmutablesTest
[ERROR] /Users/nthalk/local/src/nthalk/typescript-generator/typescript-generator-core/src/test/java/cz/habarta/typescript/generator/ImmutablesTest.java:[64,27] cannot find symbol
  symbol:   class ImmutableRectangle
  location: class cz.habarta.typescript.generator.ImmutablesTest
[ERROR] /Users/nthalk/local/src/nthalk/typescript-generator/typescript-generator-core/src/test/java/cz/habarta/typescript/generator/ImmutablesTest.java:[77,25] cannot find symbol
  symbol:   class ImmutableCircle
  location: class cz.habarta.typescript.generator.ImmutablesTest
[ERROR] /Users/nthalk/local/src/nthalk/typescript-generator/typescript-generator-core/src/test/java/cz/habarta/typescript/generator/ImmutablesTest.java:[78,27] cannot find symbol
  symbol:   class ImmutableCircle
  location: class cz.habarta.typescript.generator.ImmutablesTest
```
Before:
```
[ERROR] Tests run: 8, Failures: 0, Errors: 3, Skipped: 0, Time elapsed: 0.040 s <<< FAILURE! -- in cz.habarta.typescript.generator.NamingTest
[ERROR] cz.habarta.typescript.generator.NamingTest.testCombinations -- Time elapsed: 0.032 s <<< ERROR!
java.lang.NoSuchMethodError: 'void sun.misc.Unsafe.ensureClassInitialized(java.lang.Class)'
        at com.oracle.truffle.api.library.LibraryFactory.ensureLibraryInitialized(LibraryFactory.java:384)
```

Now, if you are not running Java 11:
```
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-enforcer-plugin:3.5.0:enforce (enforce-java-version) on project typescript-generator:
[ERROR] Rule 0: org.apache.maven.enforcer.rules.version.RequireJavaVersion failed with message:
[ERROR] Detected JDK version 23.0.1 (JAVA_HOME=/opt/homebrew/Cellar/openjdk/23.0.1/libexec/openjdk.jdk/Contents/Home) is not in the allowed range [11,12).
```

Fixing it by appending the correct `JAVA_HOME`:
```
➤ JAVA_HOME=(/usr/libexec/java_home --version 11) mvn clean install
...
[INFO] BUILD SUCCESS
...
```
Previously, this library only supported `Id.NAME` and `Id.CLASS` for `@JsonTypeInfo`, however, since inception, `Id.MINIMAL_CLASS` and `Id.SIMPLE_NAME` have been introduced, which allows us to write less redundant code, and have a more compact representation.

While `Id.SIMPLE_NAME` is not in Jackson bom 2.15.2 (which is what this project uses), `Id.MINIMAL_CLASS` is.

I use Jackson 2.18+, and would like to use the `SIMPLE_NAME` representation, instead of manually annotating thousands of classes, I would prefer to support the minimal definition for sealed interfaces/classes.

This commit makes a more forward compatible method of determining if a tagged union is being used, and instead of only allowing certain uses, it, instead, disallows known non-functional usages.
@Nthalk
Copy link
Author

Nthalk commented Dec 18, 2024

Is there any way to retry the checks? It got a 502 on the github http call.

@Nthalk Nthalk closed this Dec 18, 2024
@Nthalk Nthalk reopened this Dec 18, 2024
…on of 3.6.0

maven-enforcer-plugin 3.5.0 requires maven >3.6.3, and the CI system uses maven 3.6.0.
The 3.4.1 version supports down to maven 3.2.5: https://github.com/apache/maven-enforcer/blame/d8a21eef9f9fdd9b88c02807f4059e9e7279ad26/pom.xml
@Nthalk Nthalk marked this pull request as draft December 18, 2024 21:23
@Nthalk Nthalk marked this pull request as ready for review December 18, 2024 22:31
@Nthalk Nthalk closed this Dec 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant