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

Incorrect openapi spec being generated from typescript types. #5

Closed
avenmia opened this issue May 18, 2021 · 2 comments
Closed

Incorrect openapi spec being generated from typescript types. #5

avenmia opened this issue May 18, 2021 · 2 comments
Labels

Comments

@avenmia
Copy link

avenmia commented May 18, 2021

I have a project where I want to convert my typescript types into an openapi spec. When I run the typeconv command from the cli, the openapi spec that is generated includes various unsupported key words which don't comply.

I made a sample repo to highlight the problem.

https://github.com/avenmia/test-typeconv-error-repo

When running the following command:

typeconv -f ts -t oapi --oapi-version 3 -o openapitypes './test-func/types.ts'

for my types file, I get the following yaml:

openapi: 3.0.0
info:
  title: Converted from types.yaml with typeconv
  version: '3'
paths: {}
$id: types.yaml
$comment: >-
  Generated from test-func\types.ts by core-types-json-schema
  (https://github.com/grantila/core-types-json-schema) on behalf of typeconv
  (https://github.com/grantila/typeconv)
components:
  schemas:
    Book:
      properties:
        title:
          type: string
        author:
          type: string
        genre:
          anyOf:
            - const: Comedy
              type: string
            - const: Horror
              type: string
            - const: Romance
              type: string
      required:
        - title
        - author
        - genre
      additionalProperties: false
      description: "@swagger components:\r\n  schemas:\r\n    Book:\r\n      type: object\r\n      description: A book.\r\n      properties:\r\n        title:\r\n          type: string\r\n        author:\r\n          type: string\r\n        genre:\r\n          type: string"
      type: object

The yaml is invalid according to:
https://apitools.dev/swagger-parser/online/

but if you remove the $id and $comments fields then convert it from v3 to v2 using https://lucybot-inc.github.io/api-spec-converter/ then it does validate.

Another thing to note is that const is an unsupported keyword in version 3. I believe these are getting generated when an interface defines a type using the "or" operator. An example can be found in the repo when the Book genre property has type equal to "Comedy" | "Horror" | "Romance".

Ideally, the output of specifying version 3 for the openapi spec should output valid openapi version 3 yaml.

@grantila
Copy link
Owner

Thanks for this input! I'll make sure to fix this, yes the output should absolutely be valid openapi 3 yaml.

I'm also surprised by the genre type, it should be a string with an enum property, not an anyOf of consts, that's a bug too.

grantila added a commit to grantila/core-types-ts that referenced this issue Oct 17, 2021
@github-actions
Copy link

🎉 This issue has been resolved in version 1.6.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants