diff --git a/integration/grpc-js/google/protobuf/struct.ts b/integration/grpc-js/google/protobuf/struct.ts index 80a98258e..bdf63bfd3 100644 --- a/integration/grpc-js/google/protobuf/struct.ts +++ b/integration/grpc-js/google/protobuf/struct.ts @@ -67,27 +67,27 @@ export interface Struct_FieldsEntry { */ export interface Value { /** Represents a null value. */ - nullValue: + nullValue?: | NullValue | undefined; /** Represents a double value. */ - numberValue: + numberValue?: | number | undefined; /** Represents a string value. */ - stringValue: + stringValue?: | string | undefined; /** Represents a boolean value. */ - boolValue: + boolValue?: | boolean | undefined; /** Represents a structured value. */ - structValue: + structValue?: | { [key: string]: any } | undefined; /** Represents a repeated `Value`. */ - listValue: Array | undefined; + listValue?: Array | undefined; } /** diff --git a/integration/meta-typings/simple.ts b/integration/meta-typings/simple.ts index 78ba9ff31..70dbde53a 100644 --- a/integration/meta-typings/simple.ts +++ b/integration/meta-typings/simple.ts @@ -78,8 +78,8 @@ export interface Nested_InnerMessage_DeepMessage { } export interface OneOfMessage { - first: string | undefined; - last: string | undefined; + first?: string | undefined; + last?: string | undefined; } export interface SimpleWithWrappers { diff --git a/integration/nice-grpc/google/protobuf/struct.ts b/integration/nice-grpc/google/protobuf/struct.ts index 41ef5d717..de3ac954c 100644 --- a/integration/nice-grpc/google/protobuf/struct.ts +++ b/integration/nice-grpc/google/protobuf/struct.ts @@ -67,27 +67,27 @@ export interface Struct_FieldsEntry { */ export interface Value { /** Represents a null value. */ - nullValue: + nullValue?: | NullValue | undefined; /** Represents a double value. */ - numberValue: + numberValue?: | number | undefined; /** Represents a string value. */ - stringValue: + stringValue?: | string | undefined; /** Represents a boolean value. */ - boolValue: + boolValue?: | boolean | undefined; /** Represents a structured value. */ - structValue: + structValue?: | { [key: string]: any } | undefined; /** Represents a repeated `Value`. */ - listValue: Array | undefined; + listValue?: Array | undefined; } /** diff --git a/integration/oneof-properties/oneof-properties-test.ts b/integration/oneof-properties/oneof-properties-test.ts index 50a3ed74b..d52f50697 100644 --- a/integration/oneof-properties/oneof-properties-test.ts +++ b/integration/oneof-properties/oneof-properties-test.ts @@ -161,4 +161,12 @@ describe('oneof=properties (default)', () => { let fromJson = PleaseChoose.fromJSON(pbjsJson); expect(fromJson).toEqual(debbie); }); + + it('should allow to omit oneOf fields', () => { + const msg: PleaseChoose = { + name: 'foo', + age: 42, + }; + expect(msg).toBeDefined(); + }); }); diff --git a/integration/oneof-properties/oneof.ts b/integration/oneof-properties/oneof.ts index a80318c1d..d371b089d 100644 --- a/integration/oneof-properties/oneof.ts +++ b/integration/oneof-properties/oneof.ts @@ -9,28 +9,28 @@ export interface PleaseChoose { * Use this if you want a number. Numbers are great. Who doesn't * like them? */ - aNumber: + aNumber?: | number | undefined; /** * Use this if you want a string. Strings are also nice. Not as * nice as numbers, but what are you going to do... */ - aString: string | undefined; - aMessage: + aString?: string | undefined; + aMessage?: | PleaseChoose_Submessage | undefined; /** * We also added a bool option! This was added after the 'age' * field, so it has a higher number. */ - aBool: boolean | undefined; - bunchaBytes: Uint8Array | undefined; - anEnum: PleaseChoose_StateEnum | undefined; + aBool?: boolean | undefined; + bunchaBytes?: Uint8Array | undefined; + anEnum?: PleaseChoose_StateEnum | undefined; age: number; - either: string | undefined; - or: string | undefined; - thirdOption: string | undefined; + either?: string | undefined; + or?: string | undefined; + thirdOption?: string | undefined; } export enum PleaseChoose_StateEnum { diff --git a/integration/options/options.ts b/integration/options/options.ts index 4cae7acd2..f0b907540 100644 --- a/integration/options/options.ts +++ b/integration/options/options.ts @@ -16,7 +16,7 @@ export interface MyMessage { foo?: number | undefined; foo2?: number | undefined; bar?: string | undefined; - quux: string | undefined; + quux?: string | undefined; } export interface RequestType { diff --git a/integration/simple-optionals/simple.ts b/integration/simple-optionals/simple.ts index b9057e316..d3e4dfd82 100644 --- a/integration/simple-optionals/simple.ts +++ b/integration/simple-optionals/simple.ts @@ -169,8 +169,8 @@ export interface Nested_InnerMessage_DeepMessage { } export interface OneOfMessage { - first: string | undefined; - last: string | undefined; + first?: string | undefined; + last?: string | undefined; } export interface SimpleWithWrappers { diff --git a/integration/simple-prototype-defaults/simple.ts b/integration/simple-prototype-defaults/simple.ts index 57f940fdf..6671674e5 100644 --- a/integration/simple-prototype-defaults/simple.ts +++ b/integration/simple-prototype-defaults/simple.ts @@ -173,8 +173,8 @@ export interface Nested_InnerMessage_DeepMessage { } export interface OneOfMessage { - first: string | undefined; - last: string | undefined; + first?: string | undefined; + last?: string | undefined; } export interface SimpleWithWrappers { diff --git a/integration/simple-snake/google/protobuf/struct.ts b/integration/simple-snake/google/protobuf/struct.ts index 802676863..94feaf5b2 100644 --- a/integration/simple-snake/google/protobuf/struct.ts +++ b/integration/simple-snake/google/protobuf/struct.ts @@ -67,27 +67,27 @@ export interface Struct_FieldsEntry { */ export interface Value { /** Represents a null value. */ - null_value: + null_value?: | NullValue | undefined; /** Represents a double value. */ - number_value: + number_value?: | number | undefined; /** Represents a string value. */ - string_value: + string_value?: | string | undefined; /** Represents a boolean value. */ - bool_value: + bool_value?: | boolean | undefined; /** Represents a structured value. */ - struct_value: + struct_value?: | { [key: string]: any } | undefined; /** Represents a repeated `Value`. */ - list_value: Array | undefined; + list_value?: Array | undefined; } /** diff --git a/integration/simple-snake/simple.ts b/integration/simple-snake/simple.ts index 68f20abca..5d416272c 100644 --- a/integration/simple-snake/simple.ts +++ b/integration/simple-snake/simple.ts @@ -170,8 +170,8 @@ export interface Nested_InnerMessage_DeepMessage { } export interface OneOfMessage { - first: string | undefined; - last: string | undefined; + first?: string | undefined; + last?: string | undefined; } export interface SimpleWithWrappers { diff --git a/integration/simple-string-enums/google/protobuf/struct.ts b/integration/simple-string-enums/google/protobuf/struct.ts index 4e88bc237..dfc921bd5 100644 --- a/integration/simple-string-enums/google/protobuf/struct.ts +++ b/integration/simple-string-enums/google/protobuf/struct.ts @@ -77,27 +77,27 @@ export interface Struct_FieldsEntry { */ export interface Value { /** Represents a null value. */ - nullValue: + nullValue?: | NullValue | undefined; /** Represents a double value. */ - numberValue: + numberValue?: | number | undefined; /** Represents a string value. */ - stringValue: + stringValue?: | string | undefined; /** Represents a boolean value. */ - boolValue: + boolValue?: | boolean | undefined; /** Represents a structured value. */ - structValue: + structValue?: | { [key: string]: any } | undefined; /** Represents a repeated `Value`. */ - listValue: Array | undefined; + listValue?: Array | undefined; } /** diff --git a/integration/simple-unrecognized-enum/simple.ts b/integration/simple-unrecognized-enum/simple.ts index 708c09602..e0a1ad32a 100644 --- a/integration/simple-unrecognized-enum/simple.ts +++ b/integration/simple-unrecognized-enum/simple.ts @@ -157,8 +157,8 @@ export interface Nested_InnerMessage_DeepMessage { } export interface OneOfMessage { - first: string | undefined; - last: string | undefined; + first?: string | undefined; + last?: string | undefined; } export interface SimpleWithWrappers { diff --git a/integration/simple/simple.ts b/integration/simple/simple.ts index 696e581cf..b0881b28b 100644 --- a/integration/simple/simple.ts +++ b/integration/simple/simple.ts @@ -174,8 +174,8 @@ export interface Nested_InnerMessage_DeepMessage { } export interface OneOfMessage { - first: string | undefined; - last: string | undefined; + first?: string | undefined; + last?: string | undefined; } export interface SimpleWithWrappers { diff --git a/integration/struct/google/protobuf/struct.ts b/integration/struct/google/protobuf/struct.ts index 80a98258e..bdf63bfd3 100644 --- a/integration/struct/google/protobuf/struct.ts +++ b/integration/struct/google/protobuf/struct.ts @@ -67,27 +67,27 @@ export interface Struct_FieldsEntry { */ export interface Value { /** Represents a null value. */ - nullValue: + nullValue?: | NullValue | undefined; /** Represents a double value. */ - numberValue: + numberValue?: | number | undefined; /** Represents a string value. */ - stringValue: + stringValue?: | string | undefined; /** Represents a boolean value. */ - boolValue: + boolValue?: | boolean | undefined; /** Represents a structured value. */ - structValue: + structValue?: | { [key: string]: any } | undefined; /** Represents a repeated `Value`. */ - listValue: Array | undefined; + listValue?: Array | undefined; } /** diff --git a/integration/type-registry/google/protobuf/struct.ts b/integration/type-registry/google/protobuf/struct.ts index 6f035c984..957a66993 100644 --- a/integration/type-registry/google/protobuf/struct.ts +++ b/integration/type-registry/google/protobuf/struct.ts @@ -71,27 +71,27 @@ export interface Struct_FieldsEntry { export interface Value { $type: "google.protobuf.Value"; /** Represents a null value. */ - nullValue: + nullValue?: | NullValue | undefined; /** Represents a double value. */ - numberValue: + numberValue?: | number | undefined; /** Represents a string value. */ - stringValue: + stringValue?: | string | undefined; /** Represents a boolean value. */ - boolValue: + boolValue?: | boolean | undefined; /** Represents a structured value. */ - structValue: + structValue?: | { [key: string]: any } | undefined; /** Represents a repeated `Value`. */ - listValue: Array | undefined; + listValue?: Array | undefined; } /** diff --git a/integration/types-with-underscores/file.ts b/integration/types-with-underscores/file.ts index f8764f977..391d5bce2 100644 --- a/integration/types-with-underscores/file.ts +++ b/integration/types-with-underscores/file.ts @@ -4,7 +4,7 @@ import * as _m0 from "protobufjs/minimal"; export const protobufPackage = ""; export interface Baz { - foo: FooBar | undefined; + foo?: FooBar | undefined; } export interface FooBar { diff --git a/integration/unknown-fields/options.ts b/integration/unknown-fields/options.ts index 8f9c57745..f56eae9a9 100644 --- a/integration/unknown-fields/options.ts +++ b/integration/unknown-fields/options.ts @@ -13,7 +13,7 @@ export interface MyMessage { foo?: number | undefined; foo2?: number | undefined; bar?: string | undefined; - quux: string | undefined; + quux?: string | undefined; } export interface RequestType { diff --git a/integration/use-numeric-enum-json/google/protobuf/struct.ts b/integration/use-numeric-enum-json/google/protobuf/struct.ts index d17382273..8b67dc03d 100644 --- a/integration/use-numeric-enum-json/google/protobuf/struct.ts +++ b/integration/use-numeric-enum-json/google/protobuf/struct.ts @@ -67,27 +67,27 @@ export interface Struct_FieldsEntry { */ export interface Value { /** Represents a null value. */ - nullValue: + nullValue?: | NullValue | undefined; /** Represents a double value. */ - numberValue: + numberValue?: | number | undefined; /** Represents a string value. */ - stringValue: + stringValue?: | string | undefined; /** Represents a boolean value. */ - boolValue: + boolValue?: | boolean | undefined; /** Represents a structured value. */ - structValue: + structValue?: | { [key: string]: any } | undefined; /** Represents a repeated `Value`. */ - listValue: Array | undefined; + listValue?: Array | undefined; } /** diff --git a/integration/value/google/protobuf/struct.ts b/integration/value/google/protobuf/struct.ts index 80a98258e..bdf63bfd3 100644 --- a/integration/value/google/protobuf/struct.ts +++ b/integration/value/google/protobuf/struct.ts @@ -67,27 +67,27 @@ export interface Struct_FieldsEntry { */ export interface Value { /** Represents a null value. */ - nullValue: + nullValue?: | NullValue | undefined; /** Represents a double value. */ - numberValue: + numberValue?: | number | undefined; /** Represents a string value. */ - stringValue: + stringValue?: | string | undefined; /** Represents a boolean value. */ - boolValue: + boolValue?: | boolean | undefined; /** Represents a structured value. */ - structValue: + structValue?: | { [key: string]: any } | undefined; /** Represents a repeated `Value`. */ - listValue: Array | undefined; + listValue?: Array | undefined; } /** diff --git a/src/types.ts b/src/types.ts index 65b6df404..537a6e6e3 100644 --- a/src/types.ts +++ b/src/types.ts @@ -337,6 +337,7 @@ export function isScalar(field: FieldDescriptorProto): boolean { // properties. When useOptionals='all', all fields are translated into // optional properties, with the exception of map Entry key/values, which must // always be present. +// OneOf fields are always optional, whenever oneof=unions option not in use. export function isOptionalProperty( field: FieldDescriptorProto, messageOptions: MessageOptions | undefined, @@ -348,6 +349,8 @@ export function isOptionalProperty( return ( (optionalMessages && isMessage(field) && !isRepeated(field)) || (optionalAll && !messageOptions?.mapEntry) || + // don't bother verifying that oneof is not union. union oneofs generate their own properties. + isWithinOneOf(field) || field.proto3Optional ); }