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

fix: use optional chaining for routing parameters #1138

Merged
merged 1 commit into from
May 23, 2022
Merged

Conversation

alexander-fenster
Copy link
Contributor

Fixes #1137. The generated code for routing headers should use optional chaining (?.) instead of just dots for nested field names. Currently, it breaks compilation for Cloud Run v2 client library.

The change includes a small refactor: it's not correct to concatenate a dot-separated name in the generator code because, by design, the generator has no knowledge of the target language. All language specific stuff must be limited to the templates. So now the generator will only keep the list of camelCased field names, that will be joined (with ?.) in the template.

@alexander-fenster alexander-fenster requested a review from a team as a code owner May 21, 2022 02:05
@@ -266,8 +266,8 @@ request.{{ oneComment.paramName.toCamelCase() }}
let routingParameter = {};
{%- for paramArray in method.dynamicRoutingRequestParams %}
{%- for param in paramArray %}
if((typeof request.{{ param.fieldRetrieve }} !== "undefined") && RegExp('{{ param.messageRegex | safe }}').test(request.{{ param.fieldRetrieve }}!)){
Object.assign(routingParameter, { {{ param.fieldSend }}: request.{{ param.fieldRetrieve }}!.match(RegExp('{{ param.namedSegment | safe }}'))![0]})}
if ((typeof request.{{ param.fieldRetrieve.join('?.') }} !== "undefined") && RegExp('{{ param.messageRegex | safe }}').test(request.{{ param.fieldRetrieve.join('?.') }}!)){
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, the old behavior we just had the object access baked into the string, and now we actually use an array type?

Seems like a good refactor to me.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the generator keeps the parts as an array, and we only merge them with ?. in the template.

@alexander-fenster alexander-fenster merged commit b1aa357 into main May 23, 2022
@alexander-fenster alexander-fenster deleted the undefined branch May 23, 2022 17:54
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

Successfully merging this pull request may close these issues.

protos for cloud run service generate invalid client
3 participants