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

Issue with Kiota typescript generator where a free flow dictionary object is treated incorrectly as AdditionalData in interface, Unable to consume the model as post request fails with 400. #1633

Open
sandeep-tipparthi opened this issue Mar 9, 2025 · 12 comments
Labels
bug Something isn't working status:waiting-for-author-feedback Issue that we've responded but needs author feedback to close type:bug A broken experience

Comments

@sandeep-tipparthi
Copy link

sandeep-tipparthi commented Mar 9, 2025

Hello Team,

Background of this issue -

I am using Kiota typescript generator to generate sdk code from a swagger definition. Kiota typescript generator when it tries to create models for the swagger, it is incorrectly created for case where element is of type object and is a free flow object.

Here is the sample swagger definition on how the complex object looks like -

{
  "policies": {
    "maxProperties": 256,
    "minProperties": 0,
    "type": "object",
    "additionalProperties": {
      "maxProperties": 64,
      "minProperties": 1,
      "type": "object",
      "additionalProperties": {
        "maxItems": 64,
        "minItems": 1,
        "type": "array",
        "items": {
          "type": "string"
        }
      },
      "description": "The resources for a role.Maps the type of a resource to a list of actions for the resource.The maximum length of resource keys is 100 and the minimum length is 1.The maximum length of action keys is 100 and the minimum length is 1."
    },
    "description": "The services that the role includes.The maximum length of service id keys is 100 and the minimum length is 1."
  }
}

Currently if you notice this policies object is a free flow where the end user is expected to completely send a blank list of properties or send single / multiple free flow parameters. There is no fixed list of properties which it can send.

Currently when i run the kiota generator with typescript, i am seeing the model class generated has interface for policies

export interface CreateRequest extends Parsable {
    /**
     * The policies property
     */
    policies?: CreateRequest_policies | null;
}

export interface CreateRequest_policies extends AdditionalDataHolder, Parsable {
    /**
     * Stores additional data not described in the OpenAPI description found when deserializing. Can be used for serialization as well.
     */
    additionalData?: Record<string, unknown>;
}

If you notice in the above interfaces generated in the model class, it create an unwanted element called "additionalData", due to this when the typescript later in the consuming script where i plan to use this model for building the request. Creates / forms the request body with additionalData which is not something recognized by the API.

As additionalData is something not available or recognized by the API itself, so the server in this case is returning 400 bad request.

Issue to be fixed -
For any free flow object where a key and value pairs are accepted, please make sure that additionalData is not accounted.

@github-project-automation github-project-automation bot moved this to Needs Triage 🔍 in Kiota Mar 9, 2025
@sandeep-tipparthi
Copy link
Author

I have already seen couple of similar issues reported and were observed that they are prematurely closed as there was no response from the party.

@baywet
Copy link
Member

baywet commented Mar 10, 2025

Hi @sandeep-tipparthi
Thank you for using kiota and for reaching out.

Can you please provide:

  • A snippet of how you're creating the request with the kiota client
  • The payload that's being sent
  • The payload you'd expect

Let us know if you have any additional comments or questions.

@baywet baywet added bug Something isn't working status:waiting-for-author-feedback Issue that we've responded but needs author feedback to close type:bug A broken experience labels Mar 10, 2025
@baywet baywet moved this from Needs Triage 🔍 to Waits for author 🔁 in Kiota Mar 10, 2025
@sandeep-tipparthi
Copy link
Author

sandeep-tipparthi commented Mar 10, 2025

@baywet Thank you for your quick response on this.

Here is the snippet where i am preparing the request body - > Note: I am unable to create request without having additionalData as that is something the generated typescript client by Kiota is additionally introducing for a free flow object.

let createRequestBody: Api.createRequest = {
                name: "TestRole",
                description: "Test Role",
                policies: {
                    additionalData: {
                        service: {
                            accounts: ["read"],
                        },
                    }
                },
            }

let response = await this.submitRequest(
                client,
                account,
                createRequestBody
            );

Here is the payload that is sent by this request - > (Captured from the log)

{"description":"TEST ROLE","name":"TEST ROLE","policies":{"additionalData": {"service":{"accounts":["read"]}}}}

Here is what i expect it to send instead, reason the API does not recognize additionalData - >

{"description":"TEST ROLE","name":"TEST ROLE","policies":{"service":{"accounts":["read"]}}}

@microsoft-github-policy-service microsoft-github-policy-service bot added Needs: Attention 👋 and removed status:waiting-for-author-feedback Issue that we've responded but needs author feedback to close labels Mar 10, 2025
@sandeep-tipparthi sandeep-tipparthi changed the title Issue with Kiota typescript generator where a free flow dictionary object is treated incorrectly as AdditionalData in interface, Unable to consume the model as post request fails with 404. Issue with Kiota typescript generator where a free flow dictionary object is treated incorrectly as AdditionalData in interface, Unable to consume the model as post request fails with 400. Mar 10, 2025
@baywet
Copy link
Member

baywet commented Mar 10, 2025

Thank you for the additional information.

Can you also please share the serialize<TypeName> method?

For context the serialization is handled here

@baywet baywet added status:waiting-for-author-feedback Issue that we've responded but needs author feedback to close and removed Needs: Attention 👋 labels Mar 10, 2025
@sandeep-tipparthi
Copy link
Author

Here are those code snippets generated - >

/**

  • Serializes information the current object
  • @param writer Serialization writer to use to serialize this model
    /
    // @ts-ignore
    export function serializeCreateRequest(writer: SerializationWriter, createRequest: Partial | undefined | null = {}) : void {
    if (createRequest) {
    writer.writeStringValue("description", createRequest.description);
    writer.writeStringValue("name", createRequest.name);
    writer.writeObjectValue<createRequest_policies>("policies", createRequest.policies, serializeCreateRequest_policies);
    }
    }
    /
    *
  • Serializes information the current object
  • @param writer Serialization writer to use to serialize this model
    */
    // @ts-ignore
    export function serializeCreateRequest_policies(writer: SerializationWriter, createRequest_policies: Partial<CreateRequest_policies> | undefined | null = {}) : void {
    if (createRequest_policies) {
    writer.writeAdditionalData(createRequest_policies.additionalData);
    }
    }

Let me know if this helps or if you needed anything. For security reasons, i have renamed some of the methods but the generated code is intact.

@microsoft-github-policy-service microsoft-github-policy-service bot added Needs: Attention 👋 and removed status:waiting-for-author-feedback Issue that we've responded but needs author feedback to close labels Mar 10, 2025
@baywet
Copy link
Member

baywet commented Mar 11, 2025

Thank you for the additional information.

I started investigating this issue and found another bug with additional data see #1639

But fixing that test demonstrated that the serialization part is working as expected. Or at least I'm not able to reproduce it.

Can you please triple check which versions of the kiota packages are installed?

@baywet baywet added status:waiting-for-author-feedback Issue that we've responded but needs author feedback to close and removed Needs: Attention 👋 labels Mar 11, 2025
@sandeep-tipparthi
Copy link
Author

sandeep-tipparthi commented Mar 12, 2025

@baywet The version which i had used for generating typescript client using kiota is - "kiotaVersion": "1.23.0", here is the lock file generated for your reference -

{
"descriptionHash": "D9FB3FFA827E15A8C815C6AB454C64C1A2CC5144ED28B00BBBCDB15D5BCD58AF4CD09FACD570E7CC45C03EA7BB3936E9CD7BBBCBC179911EA5A5220EF29135D1",
"descriptionLocation": "../../../../src/XYZ/bin/Debug/net8.0/XYZ-swagger.json",
"lockFileVersion": "1.0.0",
"kiotaVersion": "1.23.0",
"clientClassName": "Client",
"typeAccessModifier": "Public",
"clientNamespaceName": "XYZ",
"language": "TypeScript",
"usesBackingStore": false,
"excludeBackwardCompatible": true,
"includeAdditionalData": true,
"disableSSLValidation": false,
"serializers": [
"Microsoft.Kiota.Serialization.Json.JsonSerializationWriterFactory",
"Microsoft.Kiota.Serialization.Text.TextSerializationWriterFactory",
"Microsoft.Kiota.Serialization.Form.FormSerializationWriterFactory",
"Microsoft.Kiota.Serialization.Multipart.MultipartSerializationWriterFactory"
],
"deserializers": [
"Microsoft.Kiota.Serialization.Json.JsonParseNodeFactory",
"Microsoft.Kiota.Serialization.Text.TextParseNodeFactory",
"Microsoft.Kiota.Serialization.Form.FormParseNodeFactory"
],
"structuredMimeTypes": [
"application/json",
"text/plain;q=0.9",
"application/x-www-form-urlencoded;q=0.2",
"multipart/form-data;q=0.1"
],
"includePatterns": [],
"excludePatterns": [],
"disabledValidationRules": []
}

Also here is the dotnet tools configuration, where we have defined the version to consider - >
"microsoft.openapi.kiota": {
"version": "1.23.0",
"commands": [
"kiota"
],
"rollForward": false
}

What is the version you are suggesting for this change to reflect ? I see the PR is still in Open state and not yet merged to main. Can you please confirm.

@microsoft-github-policy-service microsoft-github-policy-service bot added Needs: Attention 👋 and removed status:waiting-for-author-feedback Issue that we've responded but needs author feedback to close labels Mar 12, 2025
@baywet
Copy link
Member

baywet commented Mar 12, 2025

Thank you for the additional information.

can you please run the following commands and report the result back

npm list @microsoft/kiota-abstractions
npm list @microsoft/kiota-serialization-json
npm list @microsoft/kiota-http-fetchlibrary
npm list @microsoft/kiota-bundle

@baywet baywet added status:waiting-for-author-feedback Issue that we've responded but needs author feedback to close and removed Needs: Attention 👋 labels Mar 12, 2025
@sandeep-tipparthi
Copy link
Author

sandeep-tipparthi commented Mar 12, 2025

@baywet Here is the outcome of the above command - >

Image

@microsoft-github-policy-service microsoft-github-policy-service bot removed the status:waiting-for-author-feedback Issue that we've responded but needs author feedback to close label Mar 12, 2025
@baywet
Copy link
Member

baywet commented Mar 12, 2025

Thank you for the additional information.

To answer your previous question, this has now been merged and released. For sanity, please upgrade and test again (preview number is 86)

@baywet baywet added status:waiting-for-author-feedback Issue that we've responded but needs author feedback to close and removed Needs: Attention 👋 labels Mar 12, 2025
@sandeep-tipparthi
Copy link
Author

@baywet I just have tried upgrading to version preview.86. Although when i consume the model, it still adds additionalData in the request resulting in 400 bad request.

I did npm list as well to ensure it is using the right version and it confirms i am using the right version.

Image

Kindly help with the same. Was the fix really to exclude additionalData when it serializes the request, because i currently still see additionalData included in the request - >

INFO[0028] API Check 'Create Role ' Fail running Expected 201 from POST "{"description":"TEST ROLE","name":"TEST ROLE_32468732","policies":{"additionalData":{"account":{"accounts":["read"]}}}}"

As this additionalData is something introduced by kiota generator, i would expect it should be serializing it properly with only including the internal parameters that are provided as part of additionalData but not send the additionalData element itself.

In my case, this below request body would result in successful request with 201 response.

POST "{"description":"TEST ROLE","name":"TEST ROLE_32468732","policies":{"account":{"accounts":["read"]}}}"

@microsoft-github-policy-service microsoft-github-policy-service bot added Needs: Attention 👋 and removed status:waiting-for-author-feedback Issue that we've responded but needs author feedback to close labels Mar 12, 2025
@baywet
Copy link
Member

baywet commented Mar 12, 2025

Thank you for the additional information.

As I mentioned earlier, I'm not able to reproduce the issue.

Can you put together a draft pull request with a unit test that demonstrates the failure please?

@baywet baywet added status:waiting-for-author-feedback Issue that we've responded but needs author feedback to close and removed Needs: Attention 👋 labels Mar 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working status:waiting-for-author-feedback Issue that we've responded but needs author feedback to close type:bug A broken experience
Projects
Status: Waits for author 🔁
Development

No branches or pull requests

2 participants