microsoft / kiota-typescript

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

`writer.writeObjectValue` creates invalid JSON #1153

Closed hognevevle closed 2 months ago

hognevevle commented 3 months ago

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

andrueastman commented 3 months ago

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?

hognevevle commented 3 months ago

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.

koros commented 2 months ago

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!

hognevevle commented 2 months ago

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