vojtechhabarta/typescript-generator

Nullable doesn`t work

JonDoeBerlin opened this issue · 2 comments

I have the following dto class:

import java.time.LocalDate;
import javax.validation.constraints.PastOrPresent;
import com.google.gson.annotations.SerializedName;
import foo.Nullable;

public final class MyDTO {
	@SerializedName("Name")
	@Nullable
	private String name;

	@SerializedName("Birthday")
	@PastOrPresent
	@Nullable
	private LocalDate birthday;
}

My own nullable annotation:

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.PARAMETER, ElementType.TYPE_PARAMETER, ElementType.TYPE_USE})
public @interface Nullable {}

myPom:

	<build>
		<sourceDirectory>src/main/java</sourceDirectory>
		<plugins>
			<plugin>
				<artifactId>maven-compiler-plugin</artifactId>
				<configuration>
					<source>11</source>
					<target>11</target>
				</configuration>
			</plugin>
			<plugin>
				<groupId>cz.habarta.typescript-generator</groupId>
				<artifactId>typescript-generator-maven-plugin</artifactId>
				<version>2.37.1128</version>
				<executions>
					<execution>
						<id>generate</id>
						<goals>
							<goal>generate</goal>
						</goals>
						<phase>process-classes</phase>
					</execution>
				</executions>
				<configuration>
					<jsonLibrary>gson</jsonLibrary>
					<classes>
						<class>foo.MyDTO</class>
					</classes>
					<outputKind>module</outputKind>
					<outputFileType>implementationFile</outputFileType>
					<nullabilityDefinition>
						nullInlineUnion
					</nullabilityDefinition>
					<nullableAnnotations>
						foo.Nullable
					</nullableAnnotations>
				</configuration>
			</plugin>
		</plugins>
	</build>

Expected Output:

export interface MyDTO {
    Name: string | null;
    Birthday: Date | null;
}

But the output is:

export interface MyDTO {
    Name: string;
    Birthday: Date;
}

i also have same issue. tried various versions

--edit--

i've added and use 3rd party nullable annotation and it worked with following config.

		<dependency>
			<groupId>org.checkerframework</groupId>
			<artifactId>checker-qual</artifactId>
			<version>3.26.0</version>
		</dependency>

		<nullableAnnotations>
			<nullableAnnotation>org.checkerframework.checker.nullness.qual.Nullable</nullableAnnotation>
		</nullableAnnotations>
		<nullabilityDefinition>nullAndUndefinedInlineUnion</nullabilityDefinition>

@JonDoeBerlin I think correct Maven configuration should be for example:

<nullableAnnotations>
    <annotation>foo.Nullable</annotation> <!-- here should be list of some elements (element name is not important) -->
</nullableAnnotations>

@cgdstnc I think you might have different problem, for example annotation needs to have runtime retention