swagger-typescript-api icon indicating copy to clipboard operation
swagger-typescript-api copied to clipboard

Unsupported swagger/OpenAPI version: 3.0

Open sudongyuer opened this issue 4 years ago • 6 comments
trafficstars

Snipaste_2021-10-15_16-03-43 Snipaste_2021-10-15_16-04-28 ) In node, can not support

sudongyuer avatar Oct 15 '21 08:10 sudongyuer

I ran into the same issue. I found out that openapi version 3.0 is indeed supported, but the types don't reflect this.

However, simply passing an openapi3 spec works just fine. (I generate mine with @nestjs/swagger's SwaggerModule.createDocument) I just pass it and have to ts-expect-error on the spec property not matching.

The problematic definition is in generateApi's GenerateApiParamsFromSpecLiteral. This points only at a swagger-schema-official.Spec type, not allowing any openapi spec definitions: image

Here's a snippet of my code:

// See https://docs.nestjs.com/openapi/introduction
const mySpec: OpenAPIObject = await SwaggerModule.createDocument(app, obj);
// Generate the API with the resulting spec
await generateApi({
  output: `path/to/my/output/folder`,
  // @ts-expect-error swagger-typescript-api has incorrect types for Spec. It DOES support
  // an openapi v3 spec object (instead of swagger 2.0), but that is not reflected in the types.
  spec: mySpec,
  // and all the other options...
  httpClientType: "axios",
  defaultResponseAsSuccess: false,
  // ...
});

wardds avatar Oct 28 '22 07:10 wardds

@wardds hi, can you share example of spec property value?

js2me avatar Oct 28 '22 11:10 js2me

Hey, my example at first was maybe not so clear, I updated it now. The resulting mySpec object follows the openapi 3.0.0 spec and is typed by nestjs as the following:

export interface OpenAPIObject {
    openapi: string;
    info: InfoObject;
    servers?: ServerObject[];
    paths: PathsObject;
    components?: ComponentsObject;
    security?: SecurityRequirementObject[];
    tags?: TagObject[];
    externalDocs?: ExternalDocumentationObject;
}

wardds avatar Oct 28 '22 11:10 wardds

still the case

inomn avatar Jul 27 '23 17:07 inomn

still the case in 13.0.3

inomn avatar Oct 10 '23 14:10 inomn

@inomn try to rename "swagger" field to "openapi"

spec: {
  // @ts-expect-error wrong library types for spec field with openapi 3
  openapi: "3.0.0",
  // ...
}

Looks like type error, spec can be objects OpenAPI 2 or OpenAPI 3. And they got different required fields. Field swagger doesn't exist in Open API 3

frolovsky avatar Nov 20 '23 09:11 frolovsky