microsoft / kiota-typescript

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

generated typescipt code error #1053

Closed sword-jin closed 9 months ago

sword-jin commented 9 months ago

related to: https://github.com/microsoft/kiota/issues/4018

I am using this openapi:

openapi: '3.0.2'
info:
  title: JSONPlaceholder
  version: '1.0'
servers:
  - url: https://jsonplaceholder.typicode.com/

paths:
  /api/download/{blobId}:
    parameters:
    - in: path
      name: blobId
      schema:
        type: string
      required: true
      description: BLOB ID
    get:
      summary: download temporary BLOB
      responses:
        '302':
          description: Found the BLOB and returned a signed URL to download it.
          headers:
            Location:
              required: true
              schema:
                type: string
        '400':
          description: Invalid ID
        '403':
          description: Not allowed
        '404':
          description: not found.
        default:
          description: General Error
          content:
            application/json:
              schema:
                type: string
kiota generate -d openapi.yaml --language typescript --clean-output --class-name Client -o ./out/openapi

I got this error:

error TS2322: Type '(writer: SerializationWriter, multipartBody: MultipartBody | undefined) => void' is not assignable to type 'PrimitiveTypesForDeserialization | ModelSerializerFunction<Parsable> | undefined'.
  Type '(writer: SerializationWriter, multipartBody: MultipartBody | undefined) => void' is not assignable to type 'ModelSerializerFunction<Parsable>'.
    Types of parameters 'multipartBody' and 'value' are incompatible.
      Type 'Parsable | undefined' is not assignable to type 'MultipartBody | undefined'.
        Type 'Parsable' is missing the following properties from type 'MultipartBody': _boundary, _parts, addOrReplacePart, getPartValue, and 4 more.

59         requestBodySerializer: serializeMultipartBody,
           ~~~~~~~~~~~~~~~~~~~~~

The generated code is:

/**
 * Metadata for all the requests in the request builder.
 */
export const UploadRequestBuilderRequestsMetadata: RequestsMetadata = {
    post: {
        responseBodyContentType: "application/json",
        adapterMethodName: "sendCollectionOfPrimitiveAsync",
        responseBodyFactory:  "string",
        requestBodyContentType: "multipart/form-data",
        requestBodySerializer: serializeMultipartBody,
        requestInformationContentSetMethod: "setContentFromParsable",
    },
    put: {
        responseBodyContentType: "application/json",
        adapterMethodName: "sendPrimitiveAsync",
        responseBodyFactory:  "string",
        requestInformationContentSetMethod: "setStreamContent",
    },
};
sam-eah commented 7 months ago

Hi everyone, I feel like I have a similar issue:

Some generated files (3 files in particular) throw the same error: Cannot find name 'createGuidFromDiscriminatorValue'.

For example output\subscription\createTrial\item\index.ts

/* tslint:disable */
/* eslint-disable */
// Generated by Microsoft Kiota
// @ts-ignore
import { createProblemDetailsFromDiscriminatorValue, serializeMspSubscriptionCmd, type MspSubscriptionCmd, type ProblemDetails } from '../../../models/';
// @ts-ignore
import { type BaseRequestBuilder, type Parsable, type ParsableFactory, type RequestConfiguration, type RequestInformation, type RequestsMetadata } from '@microsoft/kiota-abstractions';
// @ts-ignore
import { type Guid } from 'guid-typescript';

/**
 * Builds and executes requests for operations under /Subscription/CreateTrial/{mspSubscriptionTechnicalId}
 */
export interface WithMspSubscriptionTechnicalItemRequestBuilder extends BaseRequestBuilder<WithMspSubscriptionTechnicalItemRequestBuilder> {
    /**
     * @param body The request body
     * @param requestConfiguration Configuration for the request such as headers, query parameters, and middleware options.
     * @returns {Promise<Guid>}
     * @throws {ProblemDetails} error when the service returns a 400 status code
     * @throws {ProblemDetails} error when the service returns a 404 status code
     */
     post(body: MspSubscriptionCmd, requestConfiguration?: RequestConfiguration<object> | undefined) : Promise<Guid | undefined>;
    /**
     * @param body The request body
     * @param requestConfiguration Configuration for the request such as headers, query parameters, and middleware options.
     * @returns {RequestInformation}
     */
     toPostRequestInformation(body: MspSubscriptionCmd, requestConfiguration?: RequestConfiguration<object> | undefined) : RequestInformation;
}
/**
 * Uri template for the request builder.
 */
export const WithMspSubscriptionTechnicalItemRequestBuilderUriTemplate = "{+baseurl}/Subscription/CreateTrial/{mspSubscriptionTechnicalId}";
/**
 * Metadata for all the requests in the request builder.
 */
export const WithMspSubscriptionTechnicalItemRequestBuilderRequestsMetadata: RequestsMetadata = {
    post: {
        uriTemplate: WithMspSubscriptionTechnicalItemRequestBuilderUriTemplate,
        responseBodyContentType: "text/plain;q=0.9",
        errorMappings: {
            400: createProblemDetailsFromDiscriminatorValue as ParsableFactory<Parsable>,
            404: createProblemDetailsFromDiscriminatorValue as ParsableFactory<Parsable>,
        },
        adapterMethodName: "send",
        responseBodyFactory:  createGuidFromDiscriminatorValue,
        requestBodyContentType: "application/json-patch+json",
        requestBodySerializer: serializeMspSubscriptionCmd,
        requestInformationContentSetMethod: "setContentFromParsable",
    },
};
/* tslint:enable */
/* eslint-enable */

We can indeed see that createGuidFromDiscriminatorValue was never declared nor imported (I could not find any export named as such anyway). I wish I could help some more but I'm not that familiar with how kiota's generation work, don't hesitate to ask me for more information. Thanks for your help!

    "@microsoft/kiota-abstractions": "^1.0.0-preview.50",
    "@microsoft/kiota-http-fetchlibrary": "^1.0.0-preview.49",
    "@microsoft/kiota-serialization-form": "^1.0.0-preview.39",
    "@microsoft/kiota-serialization-json": "^1.0.0-preview.50",
    "@microsoft/kiota-serialization-multipart": "^1.0.0-preview.28",
    "@microsoft/kiota-serialization-text": "^1.0.0-preview.47",

EDIT: I just found this issue that looks similar as well: https://github.com/microsoft/kiota/issues/4018 maybe it can help

EDIT 2: I'm noticing that this error appears for the 3 endpoints from which I'm returning ActionResult<Guid>, maybe this is related?

EDIT 3: After replacing my endoints with ActionResult<GuidDTO>, using the following class, the ts files are correctly generated!

using System;

namespace Watsoft.Backend.API.Controllers
{
    public class GuidDTO
    {
        public Guid Guid { get; set; }
    }
}

FINAL EDIT: Created seperate issue here: https://github.com/microsoft/kiota/issues/4508