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

Read-only properties should be read-only but are "set" #390

Closed
sebastienlevert opened this issue Oct 18, 2022 · 2 comments
Closed

Read-only properties should be read-only but are "set" #390

sebastienlevert opened this issue Oct 18, 2022 · 2 comments
Assignees
Labels
bug Something isn't working needs more information
Milestone

Comments

@sebastienlevert
Copy link

When using kiota with the Bing News API, we get errors that doesn't allow assigning read-only props because they are read-only :

bingNews/models/response.ts:43:14 - error TS2540: Cannot assign to '_webSearchUrl' because it is a read-only property.

43         this._webSearchUrl = value;
                ~~~~~~~~~~~~~

    at createTSError (/home/slevert/source/sebastienlevert/KiotaApp/src/js/node_modules/ts-node/src/index.ts:859:12)
    at reportTSError (/home/slevert/source/sebastienlevert/KiotaApp/src/js/node_modules/ts-node/src/index.ts:863:19)
    at getOutput (/home/slevert/source/sebastienlevert/KiotaApp/src/js/node_modules/ts-node/src/index.ts:1077:36)
    at Object.compile (/home/slevert/source/sebastienlevert/KiotaApp/src/js/node_modules/ts-node/src/index.ts:1433:41)
    at Module.m._compile (/home/slevert/source/sebastienlevert/KiotaApp/src/js/node_modules/ts-node/src/index.ts:1617:30)
    at Module._extensions..js (node:internal/modules/cjs/loader:1203:10)
    at Object.require.extensions.<computed> [as .ts] (/home/slevert/source/sebastienlevert/KiotaApp/src/js/node_modules/ts-node/src/index.ts:1621:12)
    at Module.load (node:internal/modules/cjs/loader:1027:32)
    at Function.Module._load (node:internal/modules/cjs/loader:868:12)
    at Module.require (node:internal/modules/cjs/loader:1051:19) {
  diagnosticCodes: [ 2540 ]

The generated model :

import {Identifiable} from './index';
import {Parsable, ParseNode, SerializationWriter} from '@microsoft/kiota-abstractions';

/** Defines a response. All schemas that could be returned at the root of a response should inherit from this */
export class Response extends Identifiable implements Parsable {
    /** The URL To Bing's search result for this item. */
    private readonly _webSearchUrl?: string | undefined;
    /**
     * Instantiates a new Response and sets the default values.
     */
    public constructor() {
        super();
    };
    /**
     * The deserialization information for the current model
     * @returns a Record<string, (node: ParseNode) => void>
     */
    public getFieldDeserializers() : Record<string, (node: ParseNode) => void> {
        return {...super.getFieldDeserializers(),
            "webSearchUrl": n => { this.webSearchUrl = n.getStringValue(); },
        };
    };
    /**
     * Serializes information the current object
     * @param writer Serialization writer to use to serialize this model
     */
    public serialize(writer: SerializationWriter) : void {
        if(!writer) throw new Error("writer cannot be undefined");
        super.serialize(writer);
    };
    /**
     * Gets the webSearchUrl property value. The URL To Bing's search result for this item.
     * @returns a string
     */
    public get webSearchUrl() {
        return this._webSearchUrl;
    };
    /**
     * Sets the webSearchUrl property value. The URL To Bing's search result for this item.
     * @param value Value to set for the webSearchUrl property.
     */
    public set webSearchUrl(value: string | undefined) {
        this._webSearchUrl = value;
    };
}

I would assume this._webSearchUrl should not be a read-only prop as it's being set later.

I used the following command to generate the models :

kiota generate -l typescript -o bingNews -d https://raw.githubusercontent.com/Azure/azure-rest-api-specs/master/specification/cognitiveservices/data-plane/NewsSearch/stable/v1.0/NewsSearch.json -c BingNewsClient -n Bing
@baywet
Copy link
Member

baywet commented Apr 26, 2023

@sebastienlevert can you test again for this one, I believe I already solved that a while ago since setting readonly was wrong. The reason it was wrong is that even though it's readonly from an API perspective, we still need to be able to set it from a model perspective when deserializing from the API.

@baywet baywet added bug Something isn't working needs more information labels Apr 26, 2023
@sebastienlevert sebastienlevert added this to the Kiota v1.5 milestone Jul 19, 2023
@sebastienlevert
Copy link
Author

It's all good now and tested. Closing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working needs more information
Projects
Archived in project
Development

No branches or pull requests

2 participants