microsoft / kiota-typescript

TypeScript libraries for Kiota-generated API clients.
https://aka.ms/kiota/docs
MIT License
32 stars 25 forks source link

Request Payload with empty string values ignores all values and send empty object payload #1129

Closed rners01 closed 2 months ago

rners01 commented 4 months ago

Hi, I'd encountered the issue with empty payload request using kiota generated API client. Client ignores payload with empty string or undefined values, eg. property: "" or property: undefined. Before the request was made I console.log my payload and those values were present.

Im thinking potentially the problem could be with the serializer..

My setup:

export function createApiClient(requestAdapter: RequestAdapter) {
    registerDefaultSerializer(JsonSerializationWriterFactory);
    registerDefaultDeserializer(JsonParseNodeFactory);
    if (requestAdapter.baseUrl === undefined || requestAdapter.baseUrl === "") {
        requestAdapter.baseUrl = "https://myrestapi.com";
    }
    const pathParameters: Record<string, unknown> = {
        "baseurl": requestAdapter.baseUrl,
    };
    return apiClientProxifier<ApiClient>(requestAdapter, pathParameters, ApiClientNavigationMetadata, undefined);
}

// and then...

createApiClient(
    new FetchRequestAdapter(
      new BaseBearerTokenAuthenticationProvider({
        getAuthorizationToken() {
          const token = sessionStorage.getItem(TOKEN_KEY)

          return Promise.resolve(token)
        },
        getAllowedHostsValidator: () => new AllowedHostsValidator(new Set([apiUrl])),
      }),
      undefined,
      new JsonSerializationWriterFactory(),
      KiotaClientFactory.create(window.fetch.bind(window)),
      undefined,
    ),
  )

I'm using:

    "@microsoft/kiota-abstractions": "1.0.0-preview.45",
    "@microsoft/kiota-http-fetchlibrary": "1.0.0-preview.44",
    "@microsoft/kiota-serialization-json": "1.0.0-preview.45",
andrueastman commented 3 months ago

Possibly related to https://github.com/microsoft/kiota-typescript/pull/1120 cc @koros

koros commented 3 months ago

Hi @rners01,

Thanks for raising the issue. I would like to seek more clarification; at the moment the serialization step intentionally skips all undefined fields see: https://github.com/microsoft/kiota-typescript/issues/1079, e.g

const Item = {
    id: "123"
    name: "Foo",
    serverId: undefined
}

In the example above since the serverId is undefined, its omitted, after serialization the payload becomes:

{
    "id": "123",
    "name": "Foo"
}

Please confirm whether this is what you are seeing on your end and whether it addresses your issue

rners01 commented 3 months ago

@koros Not really, it skips "" empty string values in my case too

baywet commented 3 months ago

@rners01 thanks for the additional information. Any chance you could try with the latest version of the kiota dependencies published today? @koros implemented fixes for that specific scenario already.

microsoft-github-policy-service[bot] commented 3 months ago

This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment.