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

Exception in deserilization of post message #1487

Open
chandraarora opened this issue Nov 14, 2024 · 10 comments
Open

Exception in deserilization of post message #1487

chandraarora opened this issue Nov 14, 2024 · 10 comments
Labels
status:waiting-for-author-feedback Issue that we've responded but needs author feedback to close type:question An issue that's a question

Comments

@chandraarora
Copy link

chandraarora commented Nov 14, 2024

Hi

We are getting the error when we try a method, please see the definition below. We are sending base64 string for byte[] and it is getting encoded again?

Could you please suggest what is wrong here?

Thanks
Chandra

System.Text.DecoderFallbackException: Unable to translate bytes [8B] at index 1 from specified code page to Unicode.
at System.Text.DecoderExceptionFallbackBuffer.Throw(Byte[] bytesUnknown, Int32 index)
at System.Text.DecoderExceptionFallbackBuffer.Fallback(Byte[] bytesUnknown, Int32 index)
at System.Text.Encoding.GetCharsWithFallback(ReadOnlySpan1 bytes, Int32 originalBytesLength, Span1 chars, Int32 originalCharsLength, DecoderNLS decoder)
at System.Text.UTF8Encoding.GetCharsWithFallback(ReadOnlySpan1 bytes, Int32 originalBytesLength, Span1 chars, Int32 originalCharsLength, DecoderNLS decoder)
at System.Text.Encoding.GetCharsWithFallback(Byte* pOriginalBytes, Int32 originalByteCount, Char* pOriginalChars, Int32 originalCharCount, Int32 bytesConsumedSoFar, Int32 charsWrittenSoFar, DecoderNLS decoder)
at System.Text.Encoding.GetChars(Byte* pBytes, Int32 byteCount, Char* pChars, Int32 charCount, DecoderNLS decoder)
at System.Text.DecoderNLS.GetChars(Byte* bytes, Int32 byteCount, Char* chars, Int32 charCount, Boolean flush)
at System.Text.DecoderNLS.GetChars(Byte[] bytes, Int32 byteIndex, Int32 byteCount, Char[] chars, Int32 charIndex, Boolean flush)
at System.Text.DecoderNLS.GetChars(Byte[] bytes, Int32 byteIndex, Int32 byteCount, Char[] chars, Int32 charIndex)
at Microsoft.AspNetCore.WebUtilities.HttpRequestStreamReader.ReadIntoBuffer()

Open API method

/api/v2/ConvertToPdf:
    post:
      consumes:
        - application/json
      tags:
        - convert
      description: >-
        Convert any type of office documents or images into PDFs
      operationId: ConvertToPdf_V1
      parameters:
        - in: body
          name: body
          required: false
          schema:
            properties:
              docContent:
                description: Map source File Content from the source action
                **format: byte
                title: File Content
                type: string**
              document:
                description: document
                properties:
                  Name:
                    description: Source file name with file extension
                    title: File Name
                    type: string
                required:
                  - Name
                type: object
            required:
              - docContent
              - document
            type: object
      responses:
        '200':
          description: File Converted successfully!
          headers:
            FileName:
              description: File Name
              type: string
            PageCount:
              description: Page Count
              type: string
          schema:
            description: Output file content from the API response
            format: binary
            title: File Content
            type: string
      summary: Convert to PDF
@github-project-automation github-project-automation bot moved this to Needs Triage 🔍 in Kiota Nov 14, 2024
@baywet
Copy link
Member

baywet commented Nov 15, 2024

Hi @chandraarora
Thank you for using kiota and for reaching out.

Can you confirm in which language your client application is please?

@baywet baywet added status:waiting-for-author-feedback Issue that we've responded but needs author feedback to close type:question An issue that's a question labels Nov 15, 2024
@baywet baywet moved this from Needs Triage 🔍 to Waits for author 🔁 in Kiota Nov 15, 2024
@chandraarora
Copy link
Author

chandraarora commented Nov 15, 2024 via email

@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 Nov 15, 2024
@baywet
Copy link
Member

baywet commented Nov 15, 2024

Thank you for the additional information.

Can you share the code snippet where you're initializing the request?

Also why are you sharing a dotnet stack trace in the initial post?

@baywet baywet added status:waiting-for-author-feedback Issue that we've responded but needs author feedback to close and removed Needs: Attention 👋 labels Nov 15, 2024
@chandraarora
Copy link
Author

chandraarora commented Nov 15, 2024

Stack strace trace message is from server side and unable to deserialize in controller.
it happens like this

input - >{ "docName": "test.docx", "docContent": "asdf" }

output -> "\u001f�\b\u0000\u0000\u0000\u0000\u0000\u0000\n�VJ�Ov��+I�+Q�RJ,NIS�\u0001��%�*Y)��\u0016���'W(�\u0002\u0000F�Ô+\u0000\u0000\u0000",

Code sample

import { ApiKeyAuthenticationProvider, ApiKeyLocation, RequestConfiguration  } from "@microsoft/kiota-abstractions";
import { FetchRequestAdapter } from "@microsoft/kiota-http-fetchlibrary";
import { createPdf4meClient } from "../src/client/pdf4meClient.js";
import { ConvertToPdfPostRequestBody, ConvertToPdfPostRequestBody_document } from "../src/client/api/v2/convertToPdf/index.js";
import { WebhookSubscribePostRequestBody} from "../src/client/api/v2/webhookSubscribe/index.js";

async function  convertToPdf():  Promise<void> {
  try {
// API requires no authentication, so use the anonymous
// authentication provider
const authProvider = new ApiKeyAuthenticationProvider("Basic ****", "Authorization", ApiKeyLocation.Header);
// Create request adapter using the fetch-based implementation

const adapter = new FetchRequestAdapter(authProvider);
adapter.baseUrl= "http://localhost:5000";
// Create the API client
const client = createPdf4meClient(adapter);

const document :  ConvertToPdfPostRequestBody_document = 
{
  name: "test.png"
}

const body : ConvertToPdfPostRequestBody = 
{ 
  docContent : "base64 of png",
  document : document
}

const postReq =  client.api.v2.convertToPdf.post(body);

}catch (err) {
 console.log(err);
  }

}

convertToPdf() 

@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 Nov 15, 2024
@baywet
Copy link
Member

baywet commented Nov 15, 2024

Thank you for the additional information.

When setting the doc content, are you base64 encoding the byte array? The client is already doing that for you.
Removing the base64 encoding from your application should solve your issue.

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

@baywet baywet added status:waiting-for-author-feedback Issue that we've responded but needs author feedback to close and removed Needs: Attention 👋 labels Nov 15, 2024
@chandraarora
Copy link
Author

chandraarora commented Nov 15, 2024 via email

@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 Nov 15, 2024
@baywet baywet added status:waiting-for-author-feedback Issue that we've responded but needs author feedback to close and removed Needs: Attention 👋 labels Nov 15, 2024
@chandraarora
Copy link
Author

Image
DocContent is generated as string type from openapi spec, i can not assign byte []. May be i misunderstood.

@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 Nov 15, 2024
@baywet
Copy link
Member

baywet commented Nov 18, 2024

Thank you for the additional information.

If you try generating a csharp client instead, what's the type of the property?

@baywet baywet added status:waiting-for-author-feedback Issue that we've responded but needs author feedback to close and removed Needs: Attention 👋 labels Nov 18, 2024
@chandraarora
Copy link
Author

chandraarora commented Nov 18, 2024 via email

@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 Nov 18, 2024
@baywet
Copy link
Member

baywet commented Nov 18, 2024

Thank you for the additional information.

In TypeScript, here is the serialization code that's generated.

export function serializeConvertToPdfPostRequestBody(writer: SerializationWriter, convertToPdfPostRequestBody: Partial<ConvertToPdfPostRequestBody> | undefined | null = {}) : void {
    if (convertToPdfPostRequestBody) {
        writer.writeStringValue("docContent", convertToPdfPostRequestBody.docContent);
        writer.writeObjectValue<ConvertToPdfPostRequestBody_document>("document", convertToPdfPostRequestBody.document, serializeConvertToPdfPostRequestBody_document);
        writer.writeAdditionalData(convertToPdfPostRequestBody.additionalData);
    }
}

And the equivalent in CSharp

public virtual void Serialize(ISerializationWriter writer)
        {
            _ = writer ?? throw new ArgumentNullException(nameof(writer));
            writer.WriteStringValue("docContent", DocContent);
            writer.WriteObjectValue<global::Graphdotnetv4.Api.V2.ConvertToPdf.ConvertToPdfPostRequestBody_document>("document", Document);
            writer.WriteAdditionalData(AdditionalData);
        }

As you can see, both languages are calling "writeStringValue" which in JSON ends up writing a JSON string without further transformations being applied.

I encourage you to compare the application code between the dotnet and javascript applications to understand whether the pre-processing you're applying results in different strings being set on the object for the same initial document.

@baywet baywet added status:waiting-for-author-feedback Issue that we've responded but needs author feedback to close and removed Needs: Attention 👋 labels Nov 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status:waiting-for-author-feedback Issue that we've responded but needs author feedback to close type:question An issue that's a question
Projects
Status: Waits for author 🔁
Development

No branches or pull requests

2 participants