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

Parsable interface should be splat #737

Closed
Tracked by #1 ...
jasonjoh opened this issue Oct 18, 2021 · 2 comments · Fixed by #1315
Closed
Tracked by #1 ...

Parsable interface should be splat #737

jasonjoh opened this issue Oct 18, 2021 · 2 comments · Fixed by #1315
Assignees
Labels
fixed generator Issues or improvements relater to generation capabilities. type:bug A broken experience

Comments

@jasonjoh
Copy link
Member

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
      }
    }
  }
}
@baywet baywet self-assigned this Oct 18, 2021
@baywet baywet added type:bug A broken experience generator Issues or improvements relater to generation capabilities. labels Oct 18, 2021
@baywet baywet added this to the Community Preview milestone Oct 18, 2021
@darrelmiller
Copy link
Member

Any service driven by OData is always going to require AdditionalProperties to be true, which is the default. To support non-OData services that are explicit about not supporting AdditionalProperties, I guess we will need to refactor the Parseable interface.

@baywet
Copy link
Member

baywet commented Nov 15, 2021

yeah that was my first thought too, we'll need to divide Parseable in two interfaces, probably still Parseable and AdditionalPropertyStore something.

@baywet baywet changed the title Generation of TypeScript SDK using OpenAPI JSON file generated by Swashbuckle generates invalid models Parsable interface should be splat Feb 18, 2022
@baywet baywet added this to Kiota Feb 18, 2022
@baywet baywet moved this to Todo in Kiota Feb 18, 2022
@baywet baywet mentioned this issue Mar 2, 2022
21 tasks
@baywet baywet moved this from Todo to In Progress in Kiota Mar 2, 2022
@baywet baywet added the fixed label Mar 3, 2022
Repository owner moved this from In Progress to Done in Kiota Mar 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fixed generator Issues or improvements relater to generation capabilities. type:bug A broken experience
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

3 participants