grantila / typeconv

Convert between JSON Schema, TypeScript, GraphQL, Open API and SureType
MIT License
424 stars 8 forks source link

TS to OAPI #11

Closed terary closed 3 years ago

terary commented 3 years ago

Schema ref are missing "s"

- $ref: '#/components/schema/TJunctionOperator'

components:  
  schemas:

Its a minor thing with a big impact.

marcin-wlodarczyk commented 3 years ago

Input:

index.ts

export interface Bar {
    name: 'bar';
}

export interface Foo {
    bar: Bar;
}

Command:

typeconv -f ts -t oapi --oapi-format yaml --oapi-version 3 './**/*.ts'

Converted 2 types in 1 files, in 0.0s

Output (Wrong):

openapi: 3.0.0
info:
  title: Converted from index.yaml with typeconv
  version: '3'
paths: {}
$id: index.yaml
$comment: >-
  Generated from index.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:
    Bar:
      properties:
        name:
          const: bar
          type: string
      required:
        - name
      additionalProperties: false
      type: object
    Foo:
      properties:
        bar:
          $ref: '#/components/schema/Bar'
      required:
        - bar
      additionalProperties: false
      type: object

Expected Output:

openapi: 3.0.0
info:
  title: Converted from index.yaml with typeconv
  version: '3'
paths: {}
$id: index.yaml
$comment: >-
  Generated from index.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:
    Bar:
      properties:
        name:
          const: bar
          type: string
      required:
        - name
      additionalProperties: false
      type: object
    Foo:
      properties:
        bar:
          $ref: '#/components/schemas/Bar'
      required:
        - bar
      additionalProperties: false
      type: object

Temporary solution:

find . -type f -name '*.yaml' -print0 | xargs -0 sed -i '' -e 's/components\/schema/components\/schemas/g'

chrismatheson commented 3 years ago

Just wanted to say that I think this library it a really awesome piece of code. Its saved me hours and hours so far.

Ive also noticed this problem, and a little further one as well (but I think related)

references from other files dont have the filename.

if I have

//foo.d.ts

export type Thingy = {
 a: number
}
//top-level.d.ts
import {Thingy} from './foo.d.ts'

export Flubber = {
  flub: Thingy
}

when these are converted to openAPI schema the reference within Flubber is

bar:
          $ref: '#/components/schema/Thingy'

but I expect it should be

bar:
          $ref: 'foo.yaml#/components/schema/Thingy'
grantila commented 3 years ago

Interesting, I'll certainly look into this.

The last example, I'm afraid there's no plan to support typescript referencing other files. That escalates quickly into something pretty complex, needing to really understand how TypeScript handles types, rather than "just" extracting them from a single-file AST. Perhaps there are tools that can bundle this for you as a pre-processing step?

github-actions[bot] commented 3 years ago

:tada: This issue has been resolved in version 1.5.0 :tada:

The release is available on:

Your semantic-release bot :package::rocket: