Comments (7)
@rekram1-node Yes I think that would be the code.
Yes I was just about to post the flag description myself.
I'd argue the opposite since b
is in a required
list (albeit in a oneOf) it should not count as "non-required".
But I think we've exhausted this topic, it boils down to a difference of interpretation.
from quicktype.
@vbqz could you elaborate on your expectations of what should be done? Seems to me that this would be expected. You have two events, both of which will have a non nil "kind", both of these events can have a "B" but only one of them has it being required. Thereby making it optional. To me this makes sense but perhaps I misunderstand you or perhaps it is I who is mistaken.
from quicktype.
I think you have the right idea of the schema, B is (conditionally) optional. The problem lies in the generated Go code.
If I try to create a valid event like:
Event {
Kind: One,
B: nil, // OK since bar can be "string" or "null".
}
then the omitempty
tag on B
will exclude my B: nil
from the marshalled payload, it is simply not there. And then this payload does not fulfil any of the oneOf
variants. In other words
we get -> { "kind": "one" }
we want -> { "kind": "one", "b": null }
from quicktype.
@vbqz Okay then why use the omit-empty flag if you don't want it? Couldn't you just run:
npx quicktype --src-lang schema -o ./event.go --just-types-and-package def.json
from quicktype.
@rekram1-node Sure, that is what we're doing right now. However I opened this issue since it seems like using --omit-empty generated incorrect code.
The upside of --omit-empty would be that truly optional fields could be omitted. Avoiding null value noise for example seeing all fields of variants A or B when the items is actually variant C.
from quicktype.
@vbqz I see...
I assume your expected generated code would be the following correct?:
package main
type Event struct {
B *string `json:"b"`
Kind Kind `json:"kind"`
}
type Kind string
const (
One Kind = "one"
Two Kind = "two"
)
from quicktype.
@vbqz After looking at the omit empty flag description:
omit-empty If set, all non-required objects will be tagged with
",omitempty" (off by default)
And given that B is not required in all cases, I think that the behavior you are experiencing is the expected outcome at least in the current iteration. Perhaps it is too opinionated but given that description this would fall under that behavior.
from quicktype.
Related Issues (20)
- Circular dependency
- [BUG]: Using --lang typescript-zod, the generated code ignores regex fields
- [BUG]: Certain schema attributes are not generated into code
- [BUG]: C# codegen does not generate all fields when using dependecies with oneOf
- [BUG]: http error is unhandled and empty content is returned
- [FEATURE]: C++: add equality operators to classes
- Request support for RapidJson HOT 1
- Null check error in Generated toJson methods in dart having nullable DateTime values
- Escaped slashes in keys are not understood
- C++ output with "https://app.quicktype.io/" is very different from the one generated locally with master repo HOT 1
- [BUG]: no quicktype binary for 23.0.159 release
- [BUG]: quicktype version 23.0.161 command not found HOT 3
- [FEATURE]: Swift - Sendable JSONAny HOT 1
- [FEATURE]: Optional Parameters in Dart
- [FEATURE]: "empty" method in Dart
- [BUG]: Cannot fetch external links from $ref due inputStream.once error
- [FEATURE]: Use externally provided class
- [FEATURE]: cJSON - generate source/header pairs
- [FEATURE]: Can we add string to all json types?
- [BUG]: Uncaught ReferenceError: process is not defined in the browser HOT 3
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 quicktype.