-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
Support emitting default values in JSON #233
Comments
I was able to implement this using the
I think this behavior is a hurdle and unexpected behavior for new users of this system. I propose one of two changes:
What is opinion here? |
I think I'm comfortable with your first suggested solution. Let's not do anything without getting thoughts from @yugui. What do you think? |
@yugui Wanted to follow up on this - what are your thoughts? |
Sorry for my late reply. |
Not Merged? |
very, very useful! |
This is now fixed |
@achew22, @philipithomas I believe somehow @achew22 's commit for EmitEmpty has dropped out. I was trying it and it doesn't work. I you look at the code changes in https://github.com/grpc-ecosystem/grpc-gateway/pull/145/files you'll notice there is no mention of 'Emit' anywhere. So perhaps it got lost rebasing or something? |
Hi there! Any progress on this one? I see the issue closed but #242 isn't merged yet. My team would love to see this one merged to simplify our interfaces and reduce boilerplate code on clients. Thank you! |
Ah, I see what happened here. I was mistaken to close this -- my bad. Unfortunately there hasn't been progress. @ornithocoder, if you were to extend the work and fix the tests I would be more than happy to merge it as soon as it goes green. It's just a little bit of test cleanup required. |
Hi @philipithomas / @achew22, here's part of the patch for the tests. It doesn't fix all the tests, tho. diff --git a/examples/browser/a_bit_of_everything_service.spec.js b/examples/browser/a_bit_of_everything_service.spec.js
index edcbebe..f99c867 100644
--- a/examples/browser/a_bit_of_everything_service.spec.js
+++ b/examples/browser/a_bit_of_everything_service.spec.js
@@ -34,6 +34,15 @@ describe('ABitOfEverythingService', function() {
sint32_value: 2147483647,
sint64_value: "4611686018427387903",
nonConventionalNameValue: "camelCase",
+ single_nested: null,
+ nested: [ ],
+ enum_value: "ZERO",
+ repeated_string_value: [ ],
+ map_value: Object({ }),
+ mapped_string_value: Object({ }),
+ mapped_nested_value: Object({ }),
+ timestamp_value: null,
+ repeated_enum_value: [ ],
};
beforeEach(function(done) {
@@ -72,10 +81,9 @@ describe('ABitOfEverythingService', function() {
sint32_value: 2147483647,
sint64_value: "4611686018427387903",
nonConventionalNameValue: "camelCase",
-
nested: [
- { name: "bar", amount: 10 },
- { name: "baz", amount: 20 },
+ { name: "bar", amount: 10, ok: 'FALSE' },
+ { name: "baz", amount: 20, ok: 'FALSE' },
],
repeated_string_value: ["a", "b", "c"],
oneof_string: "x",
@@ -83,9 +91,13 @@ describe('ABitOfEverythingService', function() {
map_value: { a: 1, b: 2 },
mapped_string_value: { a: "x", b: "y" },
mapped_nested_value: {
- a: { name: "x", amount: 1 },
- b: { name: "y", amount: 2 },
+ a: { name: "x", amount: 1, ok: 'FALSE' },
+ b: { name: "y", amount: 2, ok: 'FALSE' },
},
+ single_nested: null,
+ enum_value: "ZERO",
+ timestamp_value: null,
+ repeated_enum_value: [ ],
};
beforeEach(function(done) {
diff --git a/examples/browser/echo_service.spec.js b/examples/browser/echo_service.spec.js
index 97888c3..eca49f9 100644
--- a/examples/browser/echo_service.spec.js
+++ b/examples/browser/echo_service.spec.js
@@ -21,7 +21,7 @@ describe('EchoService', function() {
{id: "foo"},
{responseContentType: "application/json"}
).then(function(resp) {
- expect(resp.obj).toEqual({id: "foo"});
+ expect(resp.obj).toEqual({id: "foo", num: '0'});
}).catch(function(err) {
done.fail(err);
}).then(done);
@@ -34,7 +34,7 @@ describe('EchoService', function() {
{body: {id: "foo"}},
{responseContentType: "application/json"}
).then(function(resp) {
- expect(resp.obj).toEqual({id: "foo"});
+ expect(resp.obj).toEqual({id: "foo", num: '0'});
}).catch(function(err) {
done.fail(err);
}).then(done); |
One of the recurring themes of this project has been trouble around the default marshaller. This change modifies it to be more what people expect when they first start the project. 1. It emits the proto3 json style version of field names instead of the field name as it appeared in the .proto file. 2. It emits zero values for fields. This means that if you have a field that is unset it will now have a value unlike before. Fixes: grpc-ecosystem#540, grpc-ecosystem#375, grpc-ecosystem#254, grpc-ecosystem#233
One of the recurring themes of this project has been trouble around the default marshaller. This change modifies it to be more what people expect when they first start the project. 1. It emits the proto3 json style version of field names instead of the field name as it appeared in the .proto file. 2. It emits zero values for fields. This means that if you have a field that is unset it will now have a value unlike before. Upgrade to swagger-codegen 2.4.0 Also fix a regex-o in .travis.yml. + needed to be escaped. Fixes: grpc-ecosystem#540, grpc-ecosystem#375, grpc-ecosystem#254, grpc-ecosystem#233
In order to make APIs more restful, require the marshaler to emit default values. By default, it omits empty types. This causes unexpected REST behavior by omitting all false values or zero-valued integers. Closes grpc-ecosystem#233
In order to make APIs more restful, require the marshaler to emit default values. By default, it omits empty types. This causes unexpected REST behavior by omitting all false values or zero-valued integers. Closes grpc-ecosystem#233
In order to make APIs more restful, require the marshaler to emit default values. By default, it omits empty types. This causes unexpected REST behavior by omitting all false values or zero-valued integers. Closes grpc-ecosystem#233
In order to make APIs more restful, require the marshaler to emit default values. By default, it omits empty types. This causes unexpected REST behavior by omitting all false values or zero-valued integers. Closes grpc-ecosystem#233
In order to make APIs more restful, require the marshaler to emit default values. By default, it omits empty types. This causes unexpected REST behavior by omitting all false values or zero-valued integers. If you like to override its behavior back, use the WithMarshalerOption: ```go gwmux := runtime.NewServeMux(runtime.WithMarshalerOption(runtime.MIMEWildcard, &runtime.JSONPb{OrigName: true, EmitDefaults: false})) ``` Closes grpc-ecosystem#233
In order to make APIs more restful, require the marshaler to emit default values. By default, it omits empty types. This causes unexpected REST behavior by omitting all false values or zero-valued integers. If you like to override its behavior back, use the WithMarshalerOption: ```go gwmux := runtime.NewServeMux(runtime.WithMarshalerOption(runtime.MIMEWildcard, &runtime.JSONPb{OrigName: true, EmitDefaults: false})) ``` Closes grpc-ecosystem#233
This is part of the v2 work in #546. |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
We want to support this in v2 |
This was merged with #1377 |
Would it make sense to mark all fields in the generated swagger Seems like it may be challenging to do given people use the same message in both request/response, in which case the swagger generator would need to generate 2 schemas (unless there is a way for an Operation to say its response fields are all required, but I am doubtful that exists). |
In proto3 all fields are required by default. Nullability is explicit using the |
Right now,
github.com/golang/protobuf
hardcodesomitempty
, and the gateway does not support a way to enable theEmitDefaults
option inThis produces unintended behavior - like all false booleans or zero integers being omitted from JSON apis. I don't think it's wise to assume that API consumers know that the absence of a response parameter means that it is a specific empty value.
I'm opening this issue for tracking - I'm not sure what the best solution is at this time though.
The text was updated successfully, but these errors were encountered: