Comments (3)
The fix for this should be incorporated into v0.4.1 of jtd-codegen
. :)
from json-typedef-codegen.
Hi, sorry for the delayed response. To answer your final concern: this is a bug in JTD-Codegen. JTD-Codegen should always output either working code or else give you a clear error as to why working code can't be generated. I am going to keep this ticket open until we fix this. I can't give you a precise estimate on how long this will take to fix, but I do think this can be fixed reasonably easily. What follows is a bit of notes from me looking into the root cause.
I've been looking into this, and it seems this problem permeates in a few different cases. You can also get broken output with this schema:
{
"definitions": {
"": { "type": "string" },
"$foo": { "type": "string" },
"_foo": { "type": "string" },
"0foo": { "type": "string" },
"foo0bar": { "type": "string" },
"foo bar": { "type": "string" },
"foo\nbar": { "type": "string" },
"foo\uFDFDbar": { "type": "string" }
}
}
Which generates:
// Code generated by jtd-codegen for TypeScript v0.2.0
export type Root = any;
export type = string;
export type Foo = string;
export type 0foo = string;
export type Foo0 = string;
export type FooBar = string;
export type FooBar0 = string;
export type Foo0bar = string;
export type FooBar1 = string;
And this schema breaks every target except for TypeScript:
{
"properties": {
"": { "type": "string" },
"$foo": { "type": "string" },
"_foo": { "type": "string" },
"0foo": { "type": "string" },
"foo0bar": { "type": "string" },
"foo bar": { "type": "string" },
"foo\nbar": { "type": "string" },
"foo\uFDFDbar": { "type": "string" }
}
}
For instance, in Ruby:
module XXX
class Root
attr_accessor :
attr_accessor :foo
attr_accessor :0foo
attr_accessor :foo0
attr_accessor :foo_bar
attr_accessor :foo_bar0
attr_accessor :foo0bar
attr_accessor :foo_bar1
# ...
end
end
All of these cases should be made to work for all targets, except for the specific case of empty string and Java+Jackson (because of limitations in Jackson's ability to distinguish @JsonProperty
from @JsonProperty("")
). Even in that case, jtd-codegen
should return an error, rather than silently produce non-working code.
Thankfully, the fix can be done at the "core" of JTD-Codegen. The AST phase of jtd-codegen, which assigns desired names for all classes/structs/etc. it emits, should be updated to never assign names that are usually invalid in most languages -- in other words, it should never give out names that don't match [a-zA-Z][a-zA-Z0-9]*
, and instead fall back to some default dummy name (e.g. Default
or something) if the desired name is irreparably bad, such as if it's ""
, as in your example.
Thank you for opening this ticket, and sorry you're hitting this limitation. I'm grateful for you taking the time to report this issue, and I'm going to work on fixing it when time permits.
from json-typedef-codegen.
Thanks for the detailed response, @ucarion! We were able to work around the issue in our application (by modifying the schema), but it's good to hear that the "right" fix will be forthcoming as well!
from json-typedef-codegen.
Related Issues (20)
- Java: Invalid NON_NULL annotation?
- Feature Request/Discussion: Golang string types for discriminator
- Is it possible to use it as a crate from the build.rs?
- codegen for c or c++
- Incorrect singularization of "drives" HOT 1
- Rust: tests broken by shifting dependency versions
- Arm64 support for jtd-codegen
- cant generate types from this jsonld schema HOT 2
- Add nullability annotations in generated Java code HOT 3
- Discriminator variant disapears when type name is overridden
- Go: Use private interfaces instead of generating structs for discriminating types HOT 1
- Python datetime serialisation/deserialisation not working correctly without timezone
- Permit specifying/overriding field names HOT 1
- Go: support for explicit embedded structs
- Remote resource support
- TypeScript: `any` vs `unknown` HOT 1
- How to generate classes part of build.gradle?
- Old url in repo settings
- Name JSON key when erroring
- Certain type names causes errors for Python codegen
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from json-typedef-codegen.