Skip to content

Parsable interface should be splat #737

@jasonjoh

Description

@jasonjoh

Generated models do not correctly implement Parsable. TypeScript compiler gives the following error.

Class 'WeatherForecast' incorrectly implements interface 'Parsable'.
  Property 'additionalData' is missing in type 'WeatherForecast' but required in type 'Parsable'.ts(2420)

Additional information

  • Created new ASP.NET/React app using dotnet new react.
  • Added Swashbuckle with dotnet add package Swashbuckle.AspNetCore
  • Updated Startup.cs to generate OpenAPI by:
    • Add following code at end of ConfigureServices:
      services.AddSwaggerGen(c =>
      {
          c.AddServer(new Microsoft.OpenApi.Models.OpenApiServer
          {
              Url = "https://localhost:5001"
          });
          c.SwaggerDoc("v1", new Microsoft.OpenApi.Models.OpenApiInfo {
              Title = "Weather API",
              Version = "v1"
          });
      });
    • Add folowing code to Configure just before the app.UseHttpsRedirection(); line:
      app.UseSwagger();
      app.UseSwaggerUI(c =>
      {
          c.SwaggerEndpoint("/swagger/v1/swagger.json", "Issues API V1");
          c.RoutePrefix = "swagger";
      });
  • Run the project. Once project is running, run kiota.exe as follows (from root of project):
    kiota.exe --language TypeScript --openapi https://localhost:5001/swagger/v1/swagger.json --output .\ClientApp\src\api
    
  • From your command-line, go the ClientApp directory in the project and run npm start. You'll see something like:
    TypeScript error in C:/Source/temp/KiotaTest/ClientApp/src/api/weatherForecast/weatherForecast.ts(3,14):
    Class 'WeatherForecast' incorrectly implements interface 'Parsable'.
      Property 'additionalData' is missing in type 'WeatherForecast' but required in type 'Parsable'.  TS2420
    
        1 | import {Parsable, ParseNode, SerializationWriter} from '@microsoft/kiota-abstractions';
        2 | 
      > 3 | export class WeatherForecast implements Parsable {
          |              ^
        4 |     private _date?: Date | undefined;
        5 |     private _summary?: string | undefined;
        6 |     private _temperatureC?: number | undefined;
    

The problem seems to be caused by the following line in swagger.json: "additionalProperties": false. Removing this line causes the generation to succeed.

Generated OpenAPI

{
  "openapi": "3.0.1",
  "info": {
    "title": "Weather API",
    "version": "v1"
  },
  "servers": [
    {
      "url": "https://localhost:5001"
    }
  ],
  "paths": {
    "/WeatherForecast": {
      "get": {
        "tags": [
          "WeatherForecast"
        ],
        "responses": {
          "200": {
            "description": "Success",
            "content": {
              "text/plain": {
                "schema": {
                  "type": "array",
                  "items": {
                    "$ref": "#/components/schemas/WeatherForecast"
                  }
                }
              },
              "application/json": {
                "schema": {
                  "type": "array",
                  "items": {
                    "$ref": "#/components/schemas/WeatherForecast"
                  }
                }
              },
              "text/json": {
                "schema": {
                  "type": "array",
                  "items": {
                    "$ref": "#/components/schemas/WeatherForecast"
                  }
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "WeatherForecast": {
        "type": "object",
        "properties": {
          "date": {
            "type": "string",
            "format": "date-time"
          },
          "temperatureC": {
            "type": "integer",
            "format": "int32"
          },
          "temperatureF": {
            "type": "integer",
            "format": "int32",
            "readOnly": true
          },
          "summary": {
            "type": "string",
            "nullable": true
          }
        },
        "additionalProperties": false
      }
    }
  }
}

Metadata

Metadata

Assignees

Labels

fixedgeneratorIssues or improvements relater to generation capabilities.type:bugA broken experience

Type

No type

Projects

Status

Done ✔️

Relationships

None yet

Development

No branches or pull requests

Issue actions