Skip to content
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

Improper regex usage in the proto-gen getRelativeTypeName method #113

Open
eggp opened this issue Jun 20, 2024 · 4 comments
Open

Improper regex usage in the proto-gen getRelativeTypeName method #113

eggp opened this issue Jun 20, 2024 · 4 comments

Comments

@eggp
Copy link

eggp commented Jun 20, 2024

Current code:
https://github.com/smnbbrv/ngx-grpc/blob/master/packages/protoc-gen-ng/src/input/proto.ts#L132

When generating my own proto, the pbType becomes: .Profiles.Operation
The regex fails on this because it is incorrect.

Fixed regex:
pbType.match(/^\.(([A-Za-z0-9._]*)\.)?([A-Za-z0-9._]+$)/)

Uppercase letters also need to be included.

@eggp
Copy link
Author

eggp commented Jun 20, 2024

I will correct it myself and show the solution I believe is correct, where only the package name is removed because the rest is needed, and this way my complex proto messages will translate correctly.

  getRelativeTypeName(pbType: string, thisProtoPackageName = '') {
    Services.Logger.debug(`Getting relative type "${pbType}" name from package "${thisProtoPackageName}"`);

    const meta = this.resolveTypeMetadata(pbType);
    // const [, , /* packageName */, typeName] = pbType.match(/^\.(([a-z0-9._]*)\.)?([A-Za-z0-9._]+$)/) as RegExpMatchArray;
    const typeName = this.removePackageName(pbType);

    if (meta.proto === this) {
      return (thisProtoPackageName ? thisProtoPackageName + '.' : '') + typeName;
    }

    return this.getDependencyPackageName(meta.proto) + '.' + typeName;
  }

  removePackageName(input:string) {
    const parts = input.split('.');
    if (parts.length > 2) {
      return parts.slice(2).join('.');
    }
    return input;
  }

In the future, when I have time, I will create a demo proto directory to showcase the error and the results of the corrected code.

@eggp
Copy link
Author

eggp commented Jun 20, 2024

I create a example repository:

https://github.com/eggp/ngx-grpc-issue-113

Directories:

  • proto => example complex proto
  • generated_wrong => Compiled with the current version.
  • generated_good => Compiled with fixed code

@smnbbrv
Copy link
Owner

smnbbrv commented Jun 21, 2024

This was already reported some time ago I think. Having uppercase characters in package names is not recommended however:

https://protobuf.dev/programming-guides/style/#packages

Package names should be in lowercase. Package names should have unique names based on the project name, and possibly based on the path of the file containing the protocol buffer type definitions.

@eggp
Copy link
Author

eggp commented Jun 27, 2024

This was already reported some time ago I think. Having uppercase characters in package names is not recommended however:

https://protobuf.dev/programming-guides/style/#packages

Package names should be in lowercase. Package names should have unique names based on the project name, and possibly based on the path of the file containing the protocol buffer type definitions.

This is valid and I understand, but couldn't a switch be integrated into the compiler?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants