Can it generate TS definition for OpenAPI 3.0.x specification?
SirMishaa opened this issue Β· comments
Hi,
It looks like it can't generate TS definition for OpenAPI 3.0.x specification, I got the following error when I try :
VVVV From VVVV
{ oneOf: [ { '$ref': '#/components/schemas/Document' } ] }
Error: Can't convert item to NodeType : {"oneOf":[{"$ref":"#/components/schemas/Document"}]}
The schema is the follow :
openapi: 3.0.3
info:
title: Document upload API
description: Document upload API
version: 1.0.0
servers:
- url: http://localhost:8080
paths:
/document:
post:
description: Upload a new document
requestBody:
content:
multipart/form-data:
schema:
type: object
properties:
file:
type: string
format: binary
objectType:
type: string
description: The type of risk object of the document is attached to
example: ContractRequest
enum:
- PolicyOwner
- Car
- Motorbike
- ContractRequest
- Contract
objectId:
type: number
description: The id of the risk object of the document is attached to
example: 25421
documentType:
type: string
enum:
- DriverLicense
- IdentityCard
- AccidentRecord
- PlateRegistrationCertificate
- OdometerPicture
- PurchaseInvoice
- CurrentContract
description: The type of the document (DriverLicense, IdentityCard, AccidentRecord, PlateRegistrationCertificate, OdometerPicture, PurchaseInvoice, CurrentContract)
example: DriverLicense
required: true
responses:
'200':
description: OK
content:
application/json:
schema:
type: object
properties:
documentId:
type: string
description: The uuid of the document
example: 123e4567-e89b-12d3-a456-426655440000
'400':
description: Bad request
content:
application/json:
schema:
type: object
properties:
httpCode:
type: integer
example: 400
errorMessage:
type: string
description: The error message
example: Invalid request
invalidFields:
type: array
nullable: false
items:
type: object
properties:
field:
type: string
description: The name of the invalid field
example: file
message:
type: string
description: The error message
example: File is required
/documents:
get:
description: Retrieve all documents
responses:
'200':
description: OK
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/Document'
'400':
description: Bad request
content:
application/json:
schema:
type: object
properties:
httpCode:
type: integer
example: 404
errorMessage:
type: string
description: The error message
example: Unable to find documents
/document/{documentUuid}:
get:
description: Retrieve a document by it's uuid
parameters:
- name: documentUuid
in: path
required: true
schema:
type: string
example: 123e4567-e89b-12d3-a456-426655440000
responses:
'200':
description: OK
content:
application/json:
schema:
oneOf:
- $ref: '#/components/schemas/Document'
'400':
description: Bad request
content:
application/json:
schema:
type: object
properties:
httpCode:
type: integer
example: 404
errorMessage:
type: string
description: The error message
example: Unable to find document with given uuid
/required_documents:
get:
description: Get all the required documents that have to be uploaded for a contract request
parameters:
- name: contractRequestId
in: query
required: true
schema:
type: string
example: 123e4567-e89b-12d3-a456-426655440000
responses:
'200':
description: OK
components:
schemas:
Document:
type: object
properties:
uuid:
type: string
description: The uuid of the document
example: 123e4567-e89b-12d3-a456-426655440000
objectType:
type: string
description: The type of risk object of the document is attached to
example: ContractRequest
enum:
- PolicyOwner
- Car
- Motorbike
- ContractRequest
- Contract
objectId:
type: number
description: The id of the risk object of the document is attached to
example: 25421
documentType:
type: string
enum:
- DriverLicense
- IdentityCard
- AccidentRecord
- PlateRegistrationCertificate
- OdometerPicture
- PurchaseInvoice
- CurrentContract
description: The type of the document (DriverLicense, IdentityCard, AccidentRecord, PlateRegistrationCertificate, OdometerPicture, PurchaseInvoice, CurrentContract)
example: DriverLicense
fileName:
type: string
description: The name of the file
example: driver-license.jpg
mimeType:
type: string
description: The mime type of the file
example: image/jpeg
createdAt:
type: string
description: The date the document was created
example: 2020-01-01T00:00:00.000Z
updatedAt:
type: string
description: The date the document was last updated
example: 2020-01-01T00:00:00.000Z
Note that also the 3.1 specification is very weakly supported
Can you see it here : https://openapi.tools/
JetBrains plugins are not even working with 3.1 :/
So, I think it would be great to support the 3.0 specification, which is used a lot
Yeah it should work, I'll check the code ASAP to handle the file you submitted
It should work now, could you confirm it ?
Indeed, this is working great, thank you really much !
Note that it looks like there is an issue when running the command using yarn directly. I'm using a mac pro-M2 and I don't have issue with others binary files.
seraphinWeb on ξ feat/docs-upload/new-layout [!+?] via ξ v14.20.0 via π on βοΈ (eu-central-1)
β― rm -fr node_modules/
seraphinWeb on ξ feat/docs-upload/new-layout [!+?] via ξ v14.20.0 via π on βοΈ (eu-central-1)
β― yarn install
yarn install v1.22.19
[1/4] π Resolving packages...
[2/4] π Fetching packages...
[3/4] π Linking dependencies...
warning " > pdfjs-dist@2.7.570" has unmet peer dependency "worker-loader@^3.0.7".
warning "@grafikart/o2ts > @readme/openapi-parser@2.4.0" has unmet peer dependency "openapi-types@>=7".
warning " > rollup-plugin-commonjs@10.1.0" has unmet peer dependency "rollup@>=1.12.0".
warning " > vite-plugin-pug@0.3.1" has incorrect peer dependency "vite@~2".
warning " > vite-plugin-pug@0.3.1" has unmet peer dependency "picocolors@~1".
[4/4] π¨ Building fresh packages...
β¨ Done in 4.11s.
seraphinWeb on ξ feat/docs-upload/new-layout [!+?] via ξ v14.20.0 via π on βοΈ (eu-central-1) took 4s
β― yarn run o2ts openapi/document-upload-api.yaml
yarn run v1.22.19
$ /Users/mishaa/yago/seraphinWeb/node_modules/.bin/o2ts openapi/document-upload-api.yaml
/Users/mishaa/yago/seraphinWeb/node_modules/.bin/o2ts: line 1: import: command not found
/Users/mishaa/yago/seraphinWeb/node_modules/.bin/o2ts: line 2: import: command not found
/Users/mishaa/yago/seraphinWeb/node_modules/.bin/o2ts: line 3: import: command not found
/Users/mishaa/yago/seraphinWeb/node_modules/.bin/o2ts: line 4: import: command not found
/Users/mishaa/yago/seraphinWeb/node_modules/.bin/o2ts: line 5: try: command not found
/Users/mishaa/yago/seraphinWeb/node_modules/.bin/o2ts: line 6: syntax error near unexpected token `('
/Users/mishaa/yago/seraphinWeb/node_modules/.bin/o2ts: line 6: ` const args = process.argv.slice(2);'
error Command failed with exit code 2.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
seraphinWeb on ξ feat/docs-upload/new-layout [!+?] via ξ v14.20.0 via π on βοΈ (eu-central-1)
β― node ./node_modules/.bin/o2ts openapi/document-upload-api.yaml
openapi/document-upload-api.ts created with successβ
I'm running on Node 14 for the project at work. (I promise we're going to upgrade :p)
It seems like the issue is fixed by adding the following Shebang:
#!/usr/bin/env node
// This one ^^
import OpenAPI from '@readme/openapi-parser';
import { format } from 'prettier';
import { writeFileSync } from 'node:fs';
import { SchemaParser } from './SchemaParser.js';
try {
const args = process.argv.slice(2);
const yamlFile = args[0] ?? './openapi.yml';
const tsFile = args[1] ? args[1] : args[0].replace('.yml', '.ts').replace('.yaml', '.ts');
const apiSchema = await OpenAPI.parse(yamlFile);
const options = new SchemaParser(apiSchema);
const code = options.convertToCode();
writeFileSync(tsFile, format(code, { semi: false, parser: "typescript" }));
process.stdout.write(`${tsFile} created with success`);
process.exit(0);
}
catch (err) {
console.error(err);
}
Maybe you should consider adding it?
Thank you for this library