kiota icon indicating copy to clipboard operation
kiota copied to clipboard

Fix serialization and deserialization of composed types in TS

Open rkodev opened this issue 1 year ago • 2 comments

Fix serialization and deserialization of composed types in TS https://github.com/microsoft/kiota/issues/5353

Changes made to fix the serializer will include writing an undefined value for the string and and the collection of string

export function serializeSuccess(writer: SerializationWriter, success: Partial<string[] | string> | undefined | null = {}) : void {
    if(success){
        switch (true) {
            case typeof success === "string":
                writer.writeStringValue(undefined, success as string);
            break;
            case Array.isArray(success) && (success).every(item => typeof item === 'string') :
                writer.writeCollectionOfObjectValues<string>(undefined, success as string []);
            break;
        }
    }
}

changes made to fix thd deserializer. since undefined is not an acceptable key in a record, the serializers will have to invoke the method with an empty key i.e "" to as the assign the value of success to the node

export function deserializeIntoSuccess(success: Partial<Parsable | string[] | string> | undefined = {}) : Record<string, (node: ParseNode) => void> {
    return {
        "" : n => { success = n.getStringValue() ?? n.getCollectionOfPrimitiveValues<string>(); },
    }
}

rkodev avatar Sep 25 '24 03:09 rkodev

Sample builder

/* tslint:disable */
/* eslint-disable */
// Generated by Microsoft Kiota
// @ts-ignore
import { type BaseRequestBuilder, type Parsable, type ParsableFactory, type ParseNode, type RequestConfiguration, type RequestInformation, type RequestsMetadata, type SerializationWriter } from '@microsoft/kiota-abstractions';

/**
 * Creates a new instance of the appropriate class based on discriminator value
 * @param parseNode The parse node to use to read the discriminator value and create the object
 * @returns {string[] | string}
 */
// @ts-ignore
export function createSuccessFromDiscriminatorValue(parseNode: ParseNode | undefined) : ((instance?: Parsable) => Record<string, (node: ParseNode) => void>) {
    return deserializeIntoSuccess;
}
/**
 * The deserialization information for the current model
 * @returns {Record<string, (node: ParseNode) => void>}
 */
// @ts-ignore
export function deserializeIntoSuccess(success: Partial<Parsable | string[] | string> | undefined = {}) : Record<string, (node: ParseNode) => void> {
    return {
        "" : n => { success = n.getCollectionOfPrimitiveValues<string>() ?? n.getStringValue()},
    }
}
/**
 * Builds and executes requests for operations under /example1
 */
export interface Example1RequestBuilder extends BaseRequestBuilder<Example1RequestBuilder> {
    /**
     * Test generating error with message property.
     * @param requestConfiguration Configuration for the request such as headers, query parameters, and middleware options.
     * @returns {Promise<string[] | string>}
     */
     post(requestConfiguration?: RequestConfiguration<object> | undefined) : Promise<string[] | string | undefined>;
    /**
     * Test generating error with message property.
     * @param requestConfiguration Configuration for the request such as headers, query parameters, and middleware options.
     * @returns {RequestInformation}
     */
     toPostRequestInformation(requestConfiguration?: RequestConfiguration<object> | undefined) : RequestInformation;
}
/**
 * Serializes information the current object
 * @param writer Serialization writer to use to serialize this model
 */
// @ts-ignore
export function serializeSuccess(writer: SerializationWriter, success: Partial<string[] | string> | undefined | null = {}) : void {
    if (success === undefined || success === null) return;
    switch (true) {
        case Array.isArray(success) && (success).every(item => typeof item === 'string') :
            writer.writeCollectionOfPrimitiveValues<string>(undefined, success as string[]);
            break;
        case typeof success === "string":
            writer.writeStringValue(undefined, success as string);
            break;
    }
}
export type Success = string[] | string;
/**
 * Uri template for the request builder.
 */
export const Example1RequestBuilderUriTemplate = "{+baseurl}/example1";
/**
 * Metadata for all the requests in the request builder.
 */
export const Example1RequestBuilderRequestsMetadata: RequestsMetadata = {
    post: {
        uriTemplate: Example1RequestBuilderUriTemplate,
        responseBodyContentType: "application/json",
        adapterMethodName: "send",
        responseBodyFactory:  createSuccessFromDiscriminatorValue,
    },
};
/* tslint:enable */
/* eslint-enable */


rkodev avatar Oct 07 '24 12:10 rkodev

Any chance we can double check the failing intergration test?

andrueastman avatar Nov 05 '24 06:11 andrueastman