kiota-typescript icon indicating copy to clipboard operation
kiota-typescript copied to clipboard

`writer.writeObjectValue` creates invalid JSON

Open hognevevle opened this issue 1 year ago • 3 comments

I have a generated model that looks like this:

export interface ApiCartItem extends Parsable {
  /**
   * The foo property
   */
  foo?: ApiFooCartItem;
  /**
   * The bar property
   */
  bar?: ApiBarCartItem;
}

When consuming the relevant API, and passing the model as { bar: {...} } (item being the key that gets passed to writer.writeObjectValue), the resulting JSON is:

{"item":{,"bar":{...}},

Notice the invalid , being added to the beginning of the item object. The data being passed in to writer.writeObjectValue(...) is valid, but afterwards, the data coming back in contentAsStr (below) now contains the invalid ,.

const serializedContent = writer.getSerializedContent();
  const decoder = new TextDecoder();
  const contentAsStr = decoder.decode(serializedContent);

1.0.0-preview.50

hognevevle avatar Apr 18 '24 20:04 hognevevle

Thanks for raising this @hognevevle

The function takes 3 parameters ideally including a serializeFunction.

https://github.com/microsoft/kiota-typescript/blob/16a68d0811c28a1028ca618dc63e2d3dcedc6a04/packages/serialization/json/src/jsonSerializationWriter.ts#L115

Any chance you can share a full sample that replicates the scenario to help understand the issue better?

andrueastman avatar Apr 22 '24 09:04 andrueastman

Hi @andrueastman

Yes, there is indeed a serializeFunction being passed as well.

export function serializeApiCartItemItemAddRequest(writer: SerializationWriter, apiCartItemItemAddRequest: Partial<ApiCartItemItemAddRequest> | undefined = {}) : void {
    writer.writeObjectValue<ApiCartItem>("item", apiCartItemItemAddRequest.item, serializeApiCartItem);
}
[...]
export function serializeApiCartItem(writer: SerializationWriter, apiCartItem: Partial<ApiCartItem> | undefined = {}) : void {
    writer.writeObjectValue<ApiCartItem>("foo", apiCartItem.foo, serializeApiFooCartItem); // apiCartItem.foo is undefined
    writer.writeObjectValue<ApiCartItem>("bar", apiCartItem.bar, serializeApiBarCartItem); // apiCartItem.bar is an object
}

serializeApi{Foo,Bar}CartItem contains a writer.writeStringValue(...) for each of its properties.

Not sure if this helps -- I'm unable to provide a full code reproduction right now, but let me know if needed and I'll try to prepare that later this week.

hognevevle avatar Apr 23 '24 20:04 hognevevle

Hi @hognevevle, could you provide a simplified version of the API that demonstrates the issue? Feel free to rename fields and remove any unnecessary sections. This will help us reproduce the problem more easily and work towards finding a solution. Thank you!

koros avatar May 15 '24 16:05 koros

Seems to be covered by https://github.com/microsoft/kiota-typescript/issues/1190 for which a fix has been merged. Closing for now.

hognevevle avatar May 22 '24 13:05 hognevevle