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

Record with a field of Map<String,String> produces invalid TS definitions #712

Open
recrsn opened this issue Jan 2, 2023 · 3 comments
Open
Labels
bug Something isn't working hilla Issues related to Hilla Impact: Low Severity: Minor

Comments

@recrsn
Copy link

recrsn commented Jan 2, 2023

Description of the bug

Using a Java Record with a Map<String,String field as the input/output of an endpoint, produces invalid TypeScript definitions.

Minimal reproducible example

public record EventSummary(String key, Map<String, String> data, Map<String, String> tags) {}

Expected behavior

Expected TS code

export default interface EventSummary {
  key?: string;
  data?: Record<string, string | undefined>;
  tags?: Record<string, string | undefined>;
}

Since it's a JVM Record, no need to create another empty interface Record and extend it.

Actual behaviour

Generated TS code

import Record from '../../../../java/lang/Record';

export default interface EventSummary extends Record {
  key?: string;
  data?: Record<string, string | undefined>;
  tags?: Record<string, string | undefined>;
}

Hilla (correctly) tries to map Map<String,String> to TS Record<string,string> however since the class being mapped here is a Record, it tries to extend it and causes name clashes with TS Record.

Versions:

- Vaadin / Hilla version: Hilla 1.3.2
- Node version: v18.10.0
- Java version: 17
- OS version: macOS 13.1
- Browser version (if applicable):
- Application Server (if applicable):
- IDE/Editor (if applicable): IntelliJ IDEA
@recrsn recrsn added bug Something isn't working hilla Issues related to Hilla labels Jan 2, 2023
@VerebG
Copy link

VerebG commented Jan 6, 2023

I met a similiar problem where I started using Hilla. Try to following the official docs: https://hilla.dev/docs/tutorials/quickstart#step-3-create-a-typed-server-endpoint

If you're extend your code with @nonnull annotation, may you solving this problem.

public record EventSummary(@Nonnull String key, @Nonnull Map<@Nonnull String, @Nonnull String> data,@Nonnull Map<@Nonnull String, @Nonnull String> tags) {}

@recrsn
Copy link
Author

recrsn commented Jan 7, 2023

@VerebG, thanks for your suggestion. But my problem is not related to not-null and undefined.

Hilla generates TS interfaces for every superclass to preserve inheritance semantics. In case my case, since every record is a subclass of java.lang.Record Hilla generates a Record TS interface. The name Record interferes with the built-in TS type Record<any,any>1 which is used to denote a Map-like structure.

@VerebG
Copy link

VerebG commented Jan 7, 2023

Understand. Probably I saw same things between my and your problem, but now I recognised this is not same :)
For my case is connecting to Map<> but in the funcation return:

public @Nonnull Map<@Nonnull String, @Nonnull List<@Nonnull LogEntryDto>> getAllEntry() {}

If I not putting @nonnull annotation every keyword, Hilla generator putting '| undifined' to endpoint while the funcation return value never should be null.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working hilla Issues related to Hilla Impact: Low Severity: Minor
Projects
None yet
Development

No branches or pull requests

3 participants