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

Fix serialization and deserialization of composed types in TS #5461

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

rkodev
Copy link
Contributor

@rkodev rkodev commented Sep 25, 2024

Fix serialization and deserialization of composed types in TS #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 rkodev force-pushed the fix/ts-composed-types branch 4 times, most recently from 8b7c090 to 070f0ac Compare October 7, 2024 12:02
@rkodev
Copy link
Contributor Author

rkodev commented Oct 7, 2024

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 */

Copy link
Member

@andrueastman andrueastman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we also enable the intergration test at

"Rationale": "https://github.com/microsoft/kiota/issues/5256"

@rkodev rkodev marked this pull request as draft October 8, 2024 15:16
@rkodev rkodev force-pushed the fix/ts-composed-types branch 3 times, most recently from 4dbb95a to 7971456 Compare October 15, 2024 11:15
Copy link

sonarcloud bot commented Oct 15, 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: No status
Development

Successfully merging this pull request may close these issues.

2 participants