diff --git a/src/generate-generic-service-definition.ts b/src/generate-generic-service-definition.ts index c9a0e1723..963fd9d9e 100644 --- a/src/generate-generic-service-definition.ts +++ b/src/generate-generic-service-definition.ts @@ -88,13 +88,14 @@ function generateMethodOptions(ctx: Context, options: MethodOptions | undefined) chunks.push(code`idempotencyLevel: 'NO_SIDE_EFFECTS',`); } - if (options._unknownFields !== undefined) { + if ("_unknownFields" in options) { + const msgUnknownFields: any = (options as any)["_unknownFields"]; const unknownFieldsChunks: Code[] = []; unknownFieldsChunks.push(code`{`); - for (const key in options._unknownFields) { - const values = options._unknownFields[key]; + for (const key of Object.keys(msgUnknownFields)) { + const values = msgUnknownFields[key] as Uint8Array[]; const valuesChunks: Code[] = []; for (const value of values) { diff --git a/src/schema.ts b/src/schema.ts index 085438bb7..0e6f7c49d 100644 --- a/src/schema.ts +++ b/src/schema.ts @@ -101,8 +101,12 @@ export function generateSchema(ctx: Context, fileDesc: FileDescriptorProto, sour let fileOptions: Code | undefined; if (fileDesc.options) { - fileOptions = encodedOptionsToOptions(ctx, ".google.protobuf.FileOptions", fileDesc.options._unknownFields); - delete fileDesc.options._unknownFields; + fileOptions = encodedOptionsToOptions( + ctx, + ".google.protobuf.FileOptions", + (fileDesc.options as any)["_unknownFields"] + ); + delete (fileDesc.options as any)["_unknownFields"]; } const messagesOptions: Code[] = []; @@ -121,9 +125,9 @@ export function generateSchema(ctx: Context, fileDesc: FileDescriptorProto, sour const methodOptions = encodedOptionsToOptions( ctx, ".google.protobuf.MethodOptions", - method.options._unknownFields + (method.options as any)["_unknownFields"] ); - delete method.options._unknownFields; + delete (method.options as any)["_unknownFields"]; if (methodOptions) { methodsOptions.push(code`'${method.name}': ${methodOptions}`); } @@ -132,8 +136,12 @@ export function generateSchema(ctx: Context, fileDesc: FileDescriptorProto, sour let serviceOptions: Code | undefined; if (service.options) { - serviceOptions = encodedOptionsToOptions(ctx, ".google.protobuf.ServiceOptions", service.options._unknownFields); - delete service.options._unknownFields; + serviceOptions = encodedOptionsToOptions( + ctx, + ".google.protobuf.ServiceOptions", + (service.options as any)["_unknownFields"] + ); + delete (service.options as any)["_unknownFields"]; } if (methodsOptions.length > 0 || serviceOptions) { @@ -154,9 +162,9 @@ export function generateSchema(ctx: Context, fileDesc: FileDescriptorProto, sour const valueOptions = encodedOptionsToOptions( ctx, ".google.protobuf.EnumValueOptions", - value.options._unknownFields + (value.options as any)["_unknownFields"] ); - delete value.options._unknownFields; + delete (value.options as any)["_unknownFields"]; if (valueOptions) { valuesOptions.push(code`'${value.name}': ${valueOptions}`); } @@ -165,8 +173,12 @@ export function generateSchema(ctx: Context, fileDesc: FileDescriptorProto, sour let enumOptions: Code | undefined; if (Enum.options) { - enumOptions = encodedOptionsToOptions(ctx, ".google.protobuf.EnumOptions", Enum.options._unknownFields); - delete Enum.options._unknownFields; + enumOptions = encodedOptionsToOptions( + ctx, + ".google.protobuf.EnumOptions", + (Enum.options as any)["_unknownFields"] + ); + delete (Enum.options as any)["_unknownFields"]; } if (valuesOptions.length > 0 || enumOptions) { @@ -227,14 +239,13 @@ function getExtensionValue(ctx: Context, extension: FieldDescriptorProto, data: function encodedOptionsToOptions( ctx: Context, extendee: string, - encodedOptions?: { [key: number]: Uint8Array[] } + encodedOptions: { [key: number]: Uint8Array[] } ): Code | undefined { if (!encodedOptions) { return undefined; } const resultOptions: Code[] = []; - for (const key in encodedOptions) { - const value = encodedOptions[key]; + for (const [key, value] of Object.entries(encodedOptions)) { const extension = extensionCache[extendee][parseInt(key, 10) >>> 3]; resultOptions.push(getExtensionValue(ctx, extension, value)); } @@ -248,8 +259,12 @@ function resolveMessageOptions(ctx: Context, message: DescriptorProto): Code | u const fieldsOptions: Code[] = []; message.field.forEach((field) => { if (field.options) { - const fieldOptions = encodedOptionsToOptions(ctx, ".google.protobuf.FieldOptions", field.options._unknownFields); - delete field.options._unknownFields; + const fieldOptions = encodedOptionsToOptions( + ctx, + ".google.protobuf.FieldOptions", + (field.options as any)["_unknownFields"] + ); + delete (field.options as any)["_unknownFields"]; if (fieldOptions) { fieldsOptions.push(code`'${field.name}': ${fieldOptions}`); } @@ -259,8 +274,12 @@ function resolveMessageOptions(ctx: Context, message: DescriptorProto): Code | u const oneOfsOptions: Code[] = []; message.oneofDecl.forEach((oneOf) => { if (oneOf.options) { - const oneOfOptions = encodedOptionsToOptions(ctx, ".google.protobuf.OneofOptions", oneOf.options._unknownFields); - delete oneOf.options._unknownFields; + const oneOfOptions = encodedOptionsToOptions( + ctx, + ".google.protobuf.OneofOptions", + (oneOf.options as any)["_unknownFields"] + ); + delete (oneOf.options as any)["_unknownFields"]; if (oneOfOptions) { oneOfsOptions.push(code`'${oneOf.name}': ${oneOfOptions}`); } @@ -279,8 +298,12 @@ function resolveMessageOptions(ctx: Context, message: DescriptorProto): Code | u let messageOptions: Code | undefined; if (message.options) { - messageOptions = encodedOptionsToOptions(ctx, ".google.protobuf.MessageOptions", message.options._unknownFields); - delete message.options._unknownFields; + messageOptions = encodedOptionsToOptions( + ctx, + ".google.protobuf.MessageOptions", + (message.options as any)["_unknownFields"] + ); + delete (message.options as any)["_unknownFields"]; } if (fieldsOptions.length > 0 || oneOfsOptions.length > 0 || nestedOptions.length > 0 || messageOptions) {