Question about handling nullable Integer with @JSProperty for REST API DTOs #1136
Replies: 1 comment 14 replies
-
Yes, this is an intended behaviour. Whenever you pass/retrieve anything except for String or JSObject to/from JS, you pass/retrieve a Java object. In case you use JSO to declare JS APIs, JSO trusts you, and it's you who's responsible for type consistency. In case you define anything as Passing Java objects to/from JS makes sense if you want, for example, declare a
You can use
No, it's definitely a dirty hack and nobody should ever use JSO this way.
Quote from docs:
So formally this is the answer to your question. I try to keep documentation as brief a possible, i.e. answer the question "what TeaVM does", but not "how would I do a thing X". Otherwise another problem arises: it's hard to navigate in the documentation and find out what you want. I just can't cover all specific developer's cases and find a way to come up with the generalized solution. Also, note that I like the idea of "tests is the code", so I'd encourage you to learn JSO from tests. BTW, TeaVM does not convert Java collections and primitive wrappers. Additionally, TeaVM only performs conversion when type is directly known from method's signature. This means that with generics you won't get expected results, because type arguments from generics only known at compile time. If you want a REST API, you should find a library that does not rely on reflection (e.g. uses annotation processors) or write such. I used to maintain such a library, but then abandoned it: https://github.com/konsoletyper/teavm-flavour. Feel free to fork or use it as an idea. Also, regarding documentation. Feel free to open a PR. But it's recommended to reach me out personally to discuss your PR in advance. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi @konsoletyper
First of all, thank you for all the amazing work done on teavm. I am slowly making my general code base more and more teavm compliant.
I am building annotation processors which work with lombok which generate teavm compliant jsobject interfaces, and other utility methods for serialization/deserialization without reflection in a type safe manner. I hope to be able to use shared DTOs between Java backend. Java desktop client (JavaFx) and JavaScript (both client-side and backend Cloudflare Workers).
I was tackling a use of sending/receiving REST API messages with optional numeric fields. For example:
public class PersonDTO {
private String name; // Required
private Integer age; // Optional - can be null, 0, or any number
}
The challenge: I need to distinguish between:
What I Discovered
When using @JSProperty with Integer:
The issue: The returned Integer object cannot use Java methods directly:
Integer age = person.getAge();
if (age != null) {
System.out.println("Age: " + age); // ❌ Crashes with TypeError
int value = age.intValue(); // ❌ Crashes with TypeError
}
Error: TypeError: Cannot read properties of null (reading '$lastIndexOf0')
It appears @JSProperty returns a raw JavaScript number (not a proper Java Integer object), which lacks Java class metadata.
My Current Solution
I created a helper that converts the raw value to a proper Java Integer:
This successfully:
My Questions
Thank you for any guidance you can provide! 🙏
(PS: I used bit of AI to type this, because I am having some trouble/medical issue in typing)
Beta Was this translation helpful? Give feedback.
All reactions