From b332957b9c49afc209a2b6e13d9389c5775ed7c8 Mon Sep 17 00:00:00 2001 From: michio Date: Thu, 21 Mar 2024 07:17:53 +0330 Subject: [PATCH] fix: conflicting option with nestJs and outputIndex - when we have nestJs=true and outputIndex=true, multiple protobufPackage variables will be generated, which breaks the barrel file type checking, since we have multiple exports with the same name. - the default nestjs distinct PACKAGE_NAME and SERVICE_NAME variables won't be generated. --- integration/nestjs-metadata-grpc-js/hero.ts | 2 -- integration/nestjs-metadata-observables/hero.ts | 2 -- integration/nestjs-metadata-restparameters/hero.ts | 2 -- integration/nestjs-metadata/hero.ts | 2 -- integration/nestjs-restparameters/hero.ts | 2 -- integration/nestjs-simple-observables/hero.ts | 2 -- .../nestjs-simple-restparameters/google/protobuf/empty.ts | 2 -- integration/nestjs-simple-restparameters/hero.ts | 2 -- integration/nestjs-simple-usedate/google/protobuf/empty.ts | 2 -- .../nestjs-simple-usedate/google/protobuf/timestamp.ts | 2 -- integration/nestjs-simple-usedate/hero.ts | 2 -- integration/nestjs-simple/google/protobuf/empty.ts | 2 -- integration/nestjs-simple/google/protobuf/struct.ts | 2 -- integration/nestjs-simple/google/protobuf/timestamp.ts | 2 -- integration/nestjs-simple/hero.ts | 2 -- src/main.ts | 3 ++- src/options.ts | 4 +++- 17 files changed, 5 insertions(+), 32 deletions(-) diff --git a/integration/nestjs-metadata-grpc-js/hero.ts b/integration/nestjs-metadata-grpc-js/hero.ts index 0d46ee1cb..cd51a7c5c 100644 --- a/integration/nestjs-metadata-grpc-js/hero.ts +++ b/integration/nestjs-metadata-grpc-js/hero.ts @@ -5,8 +5,6 @@ import { GrpcMethod, GrpcStreamMethod } from "@nestjs/microservices"; import * as _m0 from "protobufjs/minimal"; import { Observable } from "rxjs"; -export const protobufPackage = "hero"; - export interface HeroById { id: number; } diff --git a/integration/nestjs-metadata-observables/hero.ts b/integration/nestjs-metadata-observables/hero.ts index 197aa85d4..5ebe81b51 100644 --- a/integration/nestjs-metadata-observables/hero.ts +++ b/integration/nestjs-metadata-observables/hero.ts @@ -3,8 +3,6 @@ import { Metadata } from "@grpc/grpc-js"; import { GrpcMethod, GrpcStreamMethod } from "@nestjs/microservices"; import { Observable } from "rxjs"; -export const protobufPackage = "hero"; - export interface HeroById { id: number; } diff --git a/integration/nestjs-metadata-restparameters/hero.ts b/integration/nestjs-metadata-restparameters/hero.ts index 6ef287bac..0609bac47 100644 --- a/integration/nestjs-metadata-restparameters/hero.ts +++ b/integration/nestjs-metadata-restparameters/hero.ts @@ -3,8 +3,6 @@ import { Metadata } from "@grpc/grpc-js"; import { GrpcMethod, GrpcStreamMethod } from "@nestjs/microservices"; import { Observable } from "rxjs"; -export const protobufPackage = "hero"; - export interface HeroById { id: number; } diff --git a/integration/nestjs-metadata/hero.ts b/integration/nestjs-metadata/hero.ts index 54483ffd9..1805e8b01 100644 --- a/integration/nestjs-metadata/hero.ts +++ b/integration/nestjs-metadata/hero.ts @@ -3,8 +3,6 @@ import { Metadata } from "@grpc/grpc-js"; import { GrpcMethod, GrpcStreamMethod } from "@nestjs/microservices"; import { Observable } from "rxjs"; -export const protobufPackage = "hero"; - export interface HeroById { id: number; } diff --git a/integration/nestjs-restparameters/hero.ts b/integration/nestjs-restparameters/hero.ts index abf56feb9..0cdda9bf3 100644 --- a/integration/nestjs-restparameters/hero.ts +++ b/integration/nestjs-restparameters/hero.ts @@ -2,8 +2,6 @@ import { GrpcMethod, GrpcStreamMethod } from "@nestjs/microservices"; import { Observable } from "rxjs"; -export const protobufPackage = "hero"; - export interface HeroById { id: number; } diff --git a/integration/nestjs-simple-observables/hero.ts b/integration/nestjs-simple-observables/hero.ts index 7c884baf4..5f68072e0 100644 --- a/integration/nestjs-simple-observables/hero.ts +++ b/integration/nestjs-simple-observables/hero.ts @@ -2,8 +2,6 @@ import { GrpcMethod, GrpcStreamMethod } from "@nestjs/microservices"; import { Observable } from "rxjs"; -export const protobufPackage = "hero"; - export interface HeroById { id: number; } diff --git a/integration/nestjs-simple-restparameters/google/protobuf/empty.ts b/integration/nestjs-simple-restparameters/google/protobuf/empty.ts index 642a965dc..422307ee0 100644 --- a/integration/nestjs-simple-restparameters/google/protobuf/empty.ts +++ b/integration/nestjs-simple-restparameters/google/protobuf/empty.ts @@ -1,7 +1,5 @@ /* eslint-disable */ -export const protobufPackage = "google.protobuf"; - /** * A generic empty message that you can re-use to avoid defining duplicated * empty messages in your APIs. A typical example is to use it as the request diff --git a/integration/nestjs-simple-restparameters/hero.ts b/integration/nestjs-simple-restparameters/hero.ts index b7bb522a5..2c2794938 100644 --- a/integration/nestjs-simple-restparameters/hero.ts +++ b/integration/nestjs-simple-restparameters/hero.ts @@ -3,8 +3,6 @@ import { GrpcMethod, GrpcStreamMethod } from "@nestjs/microservices"; import { Observable } from "rxjs"; import { Empty } from "./google/protobuf/empty"; -export const protobufPackage = "hero"; - export interface User { id: number; name: string; diff --git a/integration/nestjs-simple-usedate/google/protobuf/empty.ts b/integration/nestjs-simple-usedate/google/protobuf/empty.ts index 642a965dc..422307ee0 100644 --- a/integration/nestjs-simple-usedate/google/protobuf/empty.ts +++ b/integration/nestjs-simple-usedate/google/protobuf/empty.ts @@ -1,7 +1,5 @@ /* eslint-disable */ -export const protobufPackage = "google.protobuf"; - /** * A generic empty message that you can re-use to avoid defining duplicated * empty messages in your APIs. A typical example is to use it as the request diff --git a/integration/nestjs-simple-usedate/google/protobuf/timestamp.ts b/integration/nestjs-simple-usedate/google/protobuf/timestamp.ts index 466815132..96f4d69f0 100644 --- a/integration/nestjs-simple-usedate/google/protobuf/timestamp.ts +++ b/integration/nestjs-simple-usedate/google/protobuf/timestamp.ts @@ -1,7 +1,5 @@ /* eslint-disable */ -export const protobufPackage = "google.protobuf"; - /** * A Timestamp represents a point in time independent of any time zone or local * calendar, encoded as a count of seconds and fractions of seconds at diff --git a/integration/nestjs-simple-usedate/hero.ts b/integration/nestjs-simple-usedate/hero.ts index cf606e35d..fff7422ae 100644 --- a/integration/nestjs-simple-usedate/hero.ts +++ b/integration/nestjs-simple-usedate/hero.ts @@ -4,8 +4,6 @@ import { wrappers } from "protobufjs"; import { Observable } from "rxjs"; import { Empty } from "./google/protobuf/empty"; -export const protobufPackage = "hero"; - export interface HeroById { id: number; } diff --git a/integration/nestjs-simple/google/protobuf/empty.ts b/integration/nestjs-simple/google/protobuf/empty.ts index 642a965dc..422307ee0 100644 --- a/integration/nestjs-simple/google/protobuf/empty.ts +++ b/integration/nestjs-simple/google/protobuf/empty.ts @@ -1,7 +1,5 @@ /* eslint-disable */ -export const protobufPackage = "google.protobuf"; - /** * A generic empty message that you can re-use to avoid defining duplicated * empty messages in your APIs. A typical example is to use it as the request diff --git a/integration/nestjs-simple/google/protobuf/struct.ts b/integration/nestjs-simple/google/protobuf/struct.ts index de6125dfb..c650fc8ce 100644 --- a/integration/nestjs-simple/google/protobuf/struct.ts +++ b/integration/nestjs-simple/google/protobuf/struct.ts @@ -1,8 +1,6 @@ /* eslint-disable */ import { wrappers } from "protobufjs"; -export const protobufPackage = "google.protobuf"; - /** * `NullValue` is a singleton enumeration to represent the null value for the * `Value` type union. diff --git a/integration/nestjs-simple/google/protobuf/timestamp.ts b/integration/nestjs-simple/google/protobuf/timestamp.ts index 229cba608..29d01fd3a 100644 --- a/integration/nestjs-simple/google/protobuf/timestamp.ts +++ b/integration/nestjs-simple/google/protobuf/timestamp.ts @@ -1,7 +1,5 @@ /* eslint-disable */ -export const protobufPackage = "google.protobuf"; - /** * A Timestamp represents a point in time independent of any time zone or local * calendar, encoded as a count of seconds and fractions of seconds at diff --git a/integration/nestjs-simple/hero.ts b/integration/nestjs-simple/hero.ts index a6d0e0788..11254571c 100644 --- a/integration/nestjs-simple/hero.ts +++ b/integration/nestjs-simple/hero.ts @@ -6,8 +6,6 @@ import { Empty } from "./google/protobuf/empty"; import { Struct } from "./google/protobuf/struct"; import { Timestamp } from "./google/protobuf/timestamp"; -export const protobufPackage = "hero"; - export interface HeroById { id: number; } diff --git a/src/main.ts b/src/main.ts index 76d47fc2c..3df540c39 100644 --- a/src/main.ts +++ b/src/main.ts @@ -132,7 +132,8 @@ export function generateFile(ctx: Context, fileDesc: FileDescriptorProto): [stri const chunks: Code[] = []; // Indicate this file's source protobuf package for reflective use with google.protobuf.Any - if (options.exportCommonSymbols) { + // since when nestJs=true, we have distinct protobuf package names we should exclude it here + if (options.exportCommonSymbols && !options.nestJs) { chunks.push(code`export const protobufPackage = '${fileDesc.package}';`); } diff --git a/src/options.ts b/src/options.ts index fbc90e1c6..08d33cb34 100644 --- a/src/options.ts +++ b/src/options.ts @@ -258,7 +258,9 @@ export function optionsFromParameter(parameter: string | undefined): Options { options.initializeFieldsAsUndefined = false; } - if (options.outputIndex) { + // since when nestJs=true, we have distinct protobuf package names we should exclude it here + // otherwise, the common symbols won't be generated at all + if (options.outputIndex && !options.nestJs) { options.exportCommonSymbols = false; }