Eywek / typoa

🏗 Build OpenAPI definitions from Typescript typings
MIT License
24 stars 4 forks source link

Polymorph interfaces #13

Open derolf opened 3 years ago

derolf commented 3 years ago

Consider

interface Foo {
  foo: number;
}
interface Bar extends Foo {
  bar: number;
}

Currently, Bar generates a "flat" representation. However, we should embed Foo using allOf into Bar.

Happy to work on a PR if you agree!

Eywek commented 3 years ago

Hello,

Yes, you can try to open a PR if you want

derolf commented 3 years ago

Working on it already, do you know how to use ts-morph to only get the own props of a node?

BTW. I think that ONLY interface should generate explicit schema objects, all other types should always be inlined.

Otherwise, you get a bunch of weird intermediate explicit schema types in the schema, like this:

        Patched__title-string--__:
            type: object
            properties:
                title:
                    type: string

which stems from Patched<{title: string}> with

export type Patched<T> = {
  [P in keyof T]?: T[P] extends Array<infer U> ? Array<Patched<U>> : T[P] extends ReadonlyArray<infer U> ? ReadonlyArray<Patched<U>> : Patched<T[P]>;
};

These make it hard to maintain a stable OpenAPI spec and the codegens and users downstream cry. (BTW I have handwritten codegens for openAPI -> TypeScript and openAPI -> Dart)

Eywek commented 3 years ago

do you know how to use ts-morph to only get the own props of a node?

Nope sorry, I think you'll need to dig into ts-morph to find the method

I think that ONLY interface should generate explicit schema objects, all other types should always be inlined

IMO interfaces & types (i.e. type Foo = { bar: string } or interface Foo { bar: string }) (not only interfaces since not everyone use interfaces) should generate explicit schema objects and mapped types should be always inlined

Eywek commented 2 years ago

Otherwise, you get a bunch of weird intermediate explicit schema types in the schema, like this:

should be better with https://github.com/Eywek/typoa/commit/bba5a2445d899cc310bd19a8558bad38f976fdb5