Skip to content

Commit

Permalink
fix: oneof=union breaks wrapper types #458
Browse files Browse the repository at this point in the history
  • Loading branch information
boukeversteegh committed Jan 4, 2022
1 parent d66d0ba commit 607d5cd
Show file tree
Hide file tree
Showing 95 changed files with 1,402 additions and 914 deletions.
4 changes: 4 additions & 0 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,10 @@ Generated code will be placed in the Gradle build directory.

The default behavior is `useExactTypes=true`, which makes `fromPartial` use Exact type for its argument to make TypeScript reject any unknown properties.

- With `--ts_proto_opt=unwrapFromJSON=true`, the generated `fromJSON` method will expect input values for `google.protobuf.Value` and `google.protobuf.Struct` to be provided in "wrapped" format: e.g. `{"stringValue": "hello world"}` instead of just `"hello world"`. Used for compatibility with libraries that do not "unwrap" these values before encoding to JSON, for example `pbjs`. This option currently does not affect other wrapper types.

The default behavior is `unwrapFromJSON=false`, which makes `fromJSON` expect to receive native JSON values.

### Only Types

If you're looking for `ts-proto` to generate only types for your Protobuf types then passing all three of `outputEncodeMethods`, `outputJsonMethods`, and `outputClientImpl` as `false` is probably what you want, i.e.:
Expand Down
7 changes: 5 additions & 2 deletions integration/angular/simple-message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ export const SimpleMessage = {

fromJSON(object: any): SimpleMessage {
const message = createBaseSimpleMessage();
message.numberField =
object.numberField !== undefined && object.numberField !== null ? Number(object.numberField) : 0;
message.numberField = isSet(object.numberField) ? Number(object.numberField) : 0;
return message;
},

Expand Down Expand Up @@ -81,3 +80,7 @@ if (util.Long !== Long) {
util.Long = Long as any;
configure();
}

function isSet(value: any): boolean {
return value !== null && value !== undefined;
}
17 changes: 8 additions & 9 deletions integration/avoid-import-conflicts/simple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,8 @@ export const Simple = {

fromJSON(object: any): Simple {
const message = createBaseSimple();
message.name = object.name !== undefined && object.name !== null ? String(object.name) : '';
message.otherSimple =
object.otherSimple !== undefined && object.otherSimple !== null
? Simple2.fromJSON(object.otherSimple)
: undefined;
message.name = isSet(object.name) ? String(object.name) : '';
message.otherSimple = isSet(object.otherSimple) ? Simple2.fromJSON(object.otherSimple) : undefined;
return message;
},

Expand Down Expand Up @@ -161,10 +158,8 @@ export const SimpleEnums = {

fromJSON(object: any): SimpleEnums {
const message = createBaseSimpleEnums();
message.localEnum =
object.localEnum !== undefined && object.localEnum !== null ? simpleEnumFromJSON(object.localEnum) : 0;
message.importEnum =
object.importEnum !== undefined && object.importEnum !== null ? simpleEnumFromJSON3(object.importEnum) : 0;
message.localEnum = isSet(object.localEnum) ? simpleEnumFromJSON(object.localEnum) : 0;
message.importEnum = isSet(object.importEnum) ? simpleEnumFromJSON3(object.importEnum) : 0;
return message;
},

Expand Down Expand Up @@ -206,3 +201,7 @@ if (util.Long !== Long) {
util.Long = Long as any;
configure();
}

function isSet(value: any): boolean {
return value !== null && value !== undefined;
}
8 changes: 6 additions & 2 deletions integration/avoid-import-conflicts/simple2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ export const Simple = {

fromJSON(object: any): Simple {
const message = createBaseSimple();
message.name = object.name !== undefined && object.name !== null ? String(object.name) : '';
message.age = object.age !== undefined && object.age !== null ? Number(object.age) : 0;
message.name = isSet(object.name) ? String(object.name) : '';
message.age = isSet(object.age) ? Number(object.age) : 0;
return message;
},

Expand Down Expand Up @@ -128,3 +128,7 @@ if (util.Long !== Long) {
util.Long = Long as any;
configure();
}

function isSet(value: any): boolean {
return value !== null && value !== undefined;
}
8 changes: 6 additions & 2 deletions integration/barrel-imports/bar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ export const Bar = {

fromJSON(object: any): Bar {
const message = createBaseBar();
message.name = object.name !== undefined && object.name !== null ? String(object.name) : '';
message.age = object.age !== undefined && object.age !== null ? Number(object.age) : 0;
message.name = isSet(object.name) ? String(object.name) : '';
message.age = isSet(object.age) ? Number(object.age) : 0;
return message;
},

Expand Down Expand Up @@ -88,3 +88,7 @@ if (util.Long !== Long) {
util.Long = Long as any;
configure();
}

function isSet(value: any): boolean {
return value !== null && value !== undefined;
}
8 changes: 6 additions & 2 deletions integration/barrel-imports/foo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ export const Foo = {

fromJSON(object: any): Foo {
const message = createBaseFoo();
message.name = object.name !== undefined && object.name !== null ? String(object.name) : '';
message.bar = object.bar !== undefined && object.bar !== null ? Bar.fromJSON(object.bar) : undefined;
message.name = isSet(object.name) ? String(object.name) : '';
message.bar = isSet(object.bar) ? Bar.fromJSON(object.bar) : undefined;
return message;
},

Expand Down Expand Up @@ -89,3 +89,7 @@ if (util.Long !== Long) {
util.Long = Long as any;
configure();
}

function isSet(value: any): boolean {
return value !== null && value !== undefined;
}
30 changes: 20 additions & 10 deletions integration/batching-with-context/batching.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ export const BatchQueryRequest = {

fromJSON(object: any): BatchQueryRequest {
const message = createBaseBatchQueryRequest();
message.ids = (object.ids ?? []).map((e: any) => String(e));
if (Array.isArray(object?.ids)) {
message.ids = object.ids.map((e: any) => String(e));
}
return message;
},

Expand Down Expand Up @@ -131,7 +133,9 @@ export const BatchQueryResponse = {

fromJSON(object: any): BatchQueryResponse {
const message = createBaseBatchQueryResponse();
message.entities = (object.entities ?? []).map((e: any) => Entity.fromJSON(e));
if (Array.isArray(object?.entities)) {
message.entities = object.entities.map((e: any) => Entity.fromJSON(e));
}
return message;
},

Expand Down Expand Up @@ -184,7 +188,9 @@ export const BatchMapQueryRequest = {

fromJSON(object: any): BatchMapQueryRequest {
const message = createBaseBatchMapQueryRequest();
message.ids = (object.ids ?? []).map((e: any) => String(e));
if (Array.isArray(object?.ids)) {
message.ids = object.ids.map((e: any) => String(e));
}
return message;
},

Expand Down Expand Up @@ -308,8 +314,8 @@ export const BatchMapQueryResponse_EntitiesEntry = {

fromJSON(object: any): BatchMapQueryResponse_EntitiesEntry {
const message = createBaseBatchMapQueryResponse_EntitiesEntry();
message.key = object.key !== undefined && object.key !== null ? String(object.key) : '';
message.value = object.value !== undefined && object.value !== null ? Entity.fromJSON(object.value) : undefined;
message.key = isSet(object.key) ? String(object.key) : '';
message.value = isSet(object.value) ? Entity.fromJSON(object.value) : undefined;
return message;
},

Expand Down Expand Up @@ -362,7 +368,7 @@ export const GetOnlyMethodRequest = {

fromJSON(object: any): GetOnlyMethodRequest {
const message = createBaseGetOnlyMethodRequest();
message.id = object.id !== undefined && object.id !== null ? String(object.id) : '';
message.id = isSet(object.id) ? String(object.id) : '';
return message;
},

Expand Down Expand Up @@ -411,7 +417,7 @@ export const GetOnlyMethodResponse = {

fromJSON(object: any): GetOnlyMethodResponse {
const message = createBaseGetOnlyMethodResponse();
message.entity = object.entity !== undefined && object.entity !== null ? Entity.fromJSON(object.entity) : undefined;
message.entity = isSet(object.entity) ? Entity.fromJSON(object.entity) : undefined;
return message;
},

Expand Down Expand Up @@ -461,7 +467,7 @@ export const WriteMethodRequest = {

fromJSON(object: any): WriteMethodRequest {
const message = createBaseWriteMethodRequest();
message.id = object.id !== undefined && object.id !== null ? String(object.id) : '';
message.id = isSet(object.id) ? String(object.id) : '';
return message;
},

Expand Down Expand Up @@ -556,8 +562,8 @@ export const Entity = {

fromJSON(object: any): Entity {
const message = createBaseEntity();
message.id = object.id !== undefined && object.id !== null ? String(object.id) : '';
message.name = object.name !== undefined && object.name !== null ? String(object.name) : '';
message.id = isSet(object.id) ? String(object.id) : '';
message.name = isSet(object.name) ? String(object.name) : '';
return message;
},

Expand Down Expand Up @@ -696,3 +702,7 @@ if (util.Long !== Long) {
util.Long = Long as any;
configure();
}

function isSet(value: any): boolean {
return value !== null && value !== undefined;
}
30 changes: 20 additions & 10 deletions integration/batching/batching.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ export const BatchQueryRequest = {

fromJSON(object: any): BatchQueryRequest {
const message = createBaseBatchQueryRequest();
message.ids = (object.ids ?? []).map((e: any) => String(e));
if (Array.isArray(object?.ids)) {
message.ids = object.ids.map((e: any) => String(e));
}
return message;
},

Expand Down Expand Up @@ -129,7 +131,9 @@ export const BatchQueryResponse = {

fromJSON(object: any): BatchQueryResponse {
const message = createBaseBatchQueryResponse();
message.entities = (object.entities ?? []).map((e: any) => Entity.fromJSON(e));
if (Array.isArray(object?.entities)) {
message.entities = object.entities.map((e: any) => Entity.fromJSON(e));
}
return message;
},

Expand Down Expand Up @@ -182,7 +186,9 @@ export const BatchMapQueryRequest = {

fromJSON(object: any): BatchMapQueryRequest {
const message = createBaseBatchMapQueryRequest();
message.ids = (object.ids ?? []).map((e: any) => String(e));
if (Array.isArray(object?.ids)) {
message.ids = object.ids.map((e: any) => String(e));
}
return message;
},

Expand Down Expand Up @@ -306,8 +312,8 @@ export const BatchMapQueryResponse_EntitiesEntry = {

fromJSON(object: any): BatchMapQueryResponse_EntitiesEntry {
const message = createBaseBatchMapQueryResponse_EntitiesEntry();
message.key = object.key !== undefined && object.key !== null ? String(object.key) : '';
message.value = object.value !== undefined && object.value !== null ? Entity.fromJSON(object.value) : undefined;
message.key = isSet(object.key) ? String(object.key) : '';
message.value = isSet(object.value) ? Entity.fromJSON(object.value) : undefined;
return message;
},

Expand Down Expand Up @@ -360,7 +366,7 @@ export const GetOnlyMethodRequest = {

fromJSON(object: any): GetOnlyMethodRequest {
const message = createBaseGetOnlyMethodRequest();
message.id = object.id !== undefined && object.id !== null ? String(object.id) : '';
message.id = isSet(object.id) ? String(object.id) : '';
return message;
},

Expand Down Expand Up @@ -409,7 +415,7 @@ export const GetOnlyMethodResponse = {

fromJSON(object: any): GetOnlyMethodResponse {
const message = createBaseGetOnlyMethodResponse();
message.entity = object.entity !== undefined && object.entity !== null ? Entity.fromJSON(object.entity) : undefined;
message.entity = isSet(object.entity) ? Entity.fromJSON(object.entity) : undefined;
return message;
},

Expand Down Expand Up @@ -459,7 +465,7 @@ export const WriteMethodRequest = {

fromJSON(object: any): WriteMethodRequest {
const message = createBaseWriteMethodRequest();
message.id = object.id !== undefined && object.id !== null ? String(object.id) : '';
message.id = isSet(object.id) ? String(object.id) : '';
return message;
},

Expand Down Expand Up @@ -554,8 +560,8 @@ export const Entity = {

fromJSON(object: any): Entity {
const message = createBaseEntity();
message.id = object.id !== undefined && object.id !== null ? String(object.id) : '';
message.name = object.name !== undefined && object.name !== null ? String(object.name) : '';
message.id = isSet(object.id) ? String(object.id) : '';
message.name = isSet(object.name) ? String(object.name) : '';
return message;
},

Expand Down Expand Up @@ -644,3 +650,7 @@ if (util.Long !== Long) {
util.Long = Long as any;
configure();
}

function isSet(value: any): boolean {
return value !== null && value !== undefined;
}
6 changes: 5 additions & 1 deletion integration/bytes-as-base64/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function createBaseMessage(): Message {
export const Message = {
fromJSON(object: any): Message {
const message = createBaseMessage();
message.data = object.data !== undefined && object.data !== null ? bytesFromBase64(object.data) : new Uint8Array();
message.data = isSet(object.data) ? bytesFromBase64(object.data) : new Uint8Array();
return message;
},

Expand Down Expand Up @@ -88,3 +88,7 @@ if (util.Long !== Long) {
util.Long = Long as any;
configure();
}

function isSet(value: any): boolean {
return value !== null && value !== undefined;
}
Loading

0 comments on commit 607d5cd

Please sign in to comment.