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

Add GUID support for route params #1116

Conversation

illunix
Copy link
Contributor

@illunix illunix commented Mar 19, 2024

fixes #1113

Copy link
Member

@baywet baywet left a comment

Choose a reason for hiding this comment

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

Thanks for the contribution.
It needs further changes and unit tests.
I'll reply shortly to the questions on the issue to provide guidance

@@ -252,6 +253,8 @@ export class RequestInformation implements RequestInformationSetContent {
writer.writeTimeOnlyValue(undefined, value as any as TimeOnly);
} else if (value instanceof Duration) {
writer.writeDurationValue(undefined, value as any as Duration);
} else if (value instanceof Guid) {
Copy link
Member

Choose a reason for hiding this comment

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

This if statement is for the request body, it's good to fix the issue here as well as people will eventually run into it, but it's not going to address the initial issue.

Copy link
Contributor

@yasaichi yasaichi Apr 3, 2024

Choose a reason for hiding this comment

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

@illunix
I think there are 2 possible ways to convert a Guid value in path parameters to a string:

Inside RequestInformation.URL

This getter is called just before StdUriTemplate.expand. Using the method seems better to me.

public get URL(): string {
  const rawUrl = this.pathParameters[
    RequestInformation.raw_url_key
  ] as string;
  if (this.uri) {
    ...
  } else {
    const data = {} as { [key: string]: unknown };
    for (const key in this.queryParameters) {
      if (this.queryParameters[key] !== null && this.queryParameters[key] !== undefined) {
        data[key] = this.queryParameters[key];
      }
    }
    for (const key in this.pathParameters) {
      if (this.pathParameters[key]) {
        data[key] = this.pathParameters[key]; // <- here
      }
    }
    return StdUriTemplate.expand(this.urlTemplate, data);
  }
}

Inside apiClientProxifier

This function is called when you write like apiClient.foos.byId(fooId).

export function apiClientProxifier<T extends object>(
  requestAdapter: RequestAdapter,
  pathParameters: Record<string, unknown>,
  navigationMetadata?: Record<string, NavigationMetadata>,
  requestsMetadata?: RequestsMetadata,
): T {
  if (!requestAdapter) throw new Error("requestAdapter cannot be undefined");
  if (!pathParameters) throw new Error("pathParameters cannot be undefined");
  return new Proxy({} as T, {
    get(target, property) {
      ...
      if (navigationMetadata) {
        const navigationCandidate = navigationMetadata[name];
        if (navigationCandidate) {
          ...
          return (...argArray: any[]) => {
            // navigation method like indexers or multiple path parameters
            const downWardPathParameters = getPathParameters(pathParameters);
            if (
              navigationCandidate.pathParametersMappings &&
              navigationCandidate.pathParametersMappings.length > 0
            ) {
              for (let i = 0; i < argArray.length; i++) {
                const element = argArray[i];
                downWardPathParameters[
                  navigationCandidate.pathParametersMappings[i]
                ] = element; // <- here
              }
            }
            return apiClientProxifier(
              requestAdapter,
              downWardPathParameters,
              navigationCandidate.navigationMetadata,
              navigationCandidate.requestsMetadata,
            );
          };
        }
        throw new Error(
          `couldn't find navigation property ${name} data: ${JSON.stringify(
            navigationMetadata,
          )}`,
        );
      }
    },
  });
}

I hope that this will help you. Thanks

Co-authored-by: Vincent Biret <vincentbiret@hotmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

Guid uri template path/query parameter values should be normalized to string
3 participants