openapi-generator icon indicating copy to clipboard operation
openapi-generator copied to clipboard

Rewrite of TypeScript Generators

Open TiFu opened this issue 8 years ago • 72 comments

Motivation

We currently have 7 (!) different generators for TypeScript, which do not share common code. This makes introducing bug fixes to the TypeScript generator family very hard.

Goal

Create a general framework which can be used in any OOP language - unifying the generator structure and making the project easier to work with. At the beginning, I would like to look into merging the TypeScript generators into one and improve the architecture during the implementation.

Feedback

Tear the architecture apart! Focus on the bigger picture first (e.g. missing some important feature? something which is hard to implement with this architecture?) and then go into details (e.g. missing variables). The TODOs in the text below mark parts which I did not think (enough) about yet. Feel free to fill in what's missing.

Architecture

Architecture (looks weird if I include it as an image. sorry!)

The key idea is to extract everything into its own component and hide it behind an interface. The key advantage of this architecture is

  • extendability e.g. new http libraries or more modifiers for requests e.g. a request is fed through multiple components which modify it according to their rules e.g. server sets the correct request url
  • testability - the components can be tested in isolation and can be mocked
  • less duplication of code for different frameworks in the same language

I'll go through the different components one by one and try to explain the idea.

HTTP Library

This part should be clear - hide the details of making a HTTP request behind an interface so that we can use different libraries without changing the API Client. To achieve this we create our own data structures for requests and responses.

TODO: Use promises/futures?

Authentication

Same idea as HTTP library. I want to highlight one part: the authentication interface has a method apply(Request r). This method applies the authentication scheme to the given request e.g. in the case of an API Key it would append the api key.

TODO: OAuth work flow with this implementation?

Response Models & Param Models

TODO: Should this be separated or can response & param models be used interchangeably? i.e. param Pet can also be a response model? If yes these two components should be merged.

This component contains model classes for each parameter & response as well as a (object) serializer and deserializer for these models (e.g. Array<JsonObject> in the API response is transformed to Array<Pet> by the ObjectDeserializer).

Server

This component contains the different servers as defined in OAS.

TODO: handling of variables. I would propose to offer setters for each variable. Variables are not yet supported in OpenAPITools. See #793

Config

See current implementation.

API Client

The API client coordinates all other components.

  1. Serializes the input parameters
  2. creates a new request r
  3. calls Server#apply(r)
  4. calls `Authentication#apply(r)
  5. deserializes the response
  6. returns a promise with the deserialized response

Callbacks in OAS 3

TODO: create server, hide it behind an interface to make the library exchangeable and create one method for each callback endpoint.

Framework Specific Code

Anything framework specific goes here and acts as a proxy to APIClient.

Technical Committee

@TiFu (2017/07) @taxpon (2017/07) @sebastianhaas (2017/07) @kenisteward (2017/07) @Vrolijkx (2017/09) @macjohnny (2018/01)

Project

William created a project for this issue: https://github.com/OpenAPITools/openapi-generator/projects/4

TiFu avatar Aug 13 '18 20:08 TiFu

@TiFu I went through a similar (smaller) refactor for the C# generator to allow users to customize the generator's underlying HTTP client.

That's #737, if you're interested.

jimschubert avatar Aug 14 '18 02:08 jimschubert

@TiFu I suggest to consider using RxJS / Observables. Maybe there is an easy way to make this configurable?

macjohnny avatar Aug 14 '18 10:08 macjohnny

What would be the use case for Observables in this case? What should be observable?

TiFu avatar Aug 14 '18 10:08 TiFu

@TiFu the return value of the api call. see e.g. https://github.com/OpenAPITools/openapi-generator/blob/master/samples/client/petstore/typescript-angular-v6-provided-in-root/builds/with-npm/api/pet.service.ts#L70

the use case is that observables are the standard for http interaction in angular. Therefore in the template it should be easy to have a configuration option to either have a promise-based generator or an observable-based generator.

macjohnny avatar Aug 14 '18 11:08 macjohnny

I will think about that some more. It looks quite complicated to include Observables together with promises internally (especially observing the different options like body, response and events).

One option is to simply wrap promises in Observables e.g.

import { from } from 'rxjs';
var observable =  from(promise);

I'm just not sure how this could be integrated into the current HTTPLibrary design.

TiFu avatar Aug 14 '18 15:08 TiFu

@TiFu it is very important to handle observables correctly and not only wrap promises, since promises can‘t be cancelled, while observables can, which is an essential feature.

macjohnny avatar Aug 14 '18 15:08 macjohnny

However it is possible to convert an observable into a promise. This should fit all needs. Therefore internally it should be buolt on observables

macjohnny avatar Aug 14 '18 16:08 macjohnny

Oh okay. I have an idea how to solve this issue.

  1. For each api/operation: generate a method which returns a RequestContext (this describes 'how' a request should be executed e.g. url, header params, query params etc)
  • if angular: Generate a class/method for each api/operation which gets the RequestContext from 1. and uses the normal angular http client (which returns an observable - exactly as in the old client) to make the request
  • if not angular: Generate a class/method for each api/operation which gets the RequestContext from 1. and uses the HttpLibrary to make the request

This encapsulates all api logic in 1. and just defers making the request to the library specific code section.

Does that make sense?

TiFu avatar Aug 14 '18 16:08 TiFu

Also #727.

The past 1 1/2 months have been insanely busy for me. I hope I will have some more time to work on this again.

And #347

TiFu avatar Oct 09 '18 15:10 TiFu

After my fork is merged in I’ll be very keen to throw myself and time at this work. My company has run into many issues with the ng clients and node clients so I’m very motivated to help unify the generators.

Place1 avatar Oct 15 '18 21:10 Place1

@Place1 Thanks! Let me know when you are ready/have time to look into it and I will give you a quick overview over the code base - you can already find it on the typescript-refactor branch. The name of the new generator is typescript

I have finished a basic prototype for ts-fetch today (still has a ton of todos/bugs/only basic features, but it outlines the direction I want to take for the other generators). As a next step, I will go through the other generators step by step and try to find the most common features in all generators and create issues for them. There's also a project for the ts-refactor, so I'll assign all relevant issues to this project.

I will be away from tomorrow until Monday evening, so I won't be able to do much until then.

CLI Arguments I currently use to generate the client:

generate -i openapi-generator/modules/openapi-generator/src/test/resources/2_0/petstore.yaml -g typescript -o openapi-generator/samples/client/petstore/typescript/builds/default

TiFu avatar Oct 17 '18 15:10 TiFu

Not sure if this has been brought up elsewhere, but one issue I'm having adopting the generated typescript client is the use of namespaces for enums. If the typescript generator is being overhauled could alternate code styles be considered? Babel 7 doesn't support namespaces in typescript (https://babeljs.io/docs/en/babel-plugin-transform-typescript) and advises the use of ES2015 modules instead. Some parts of the code are already using string unions as instead of a defined enum, or the enum could be declared without the namespace and use a prefix of the enclosing type for uniqueness.

acornett avatar Nov 09 '18 04:11 acornett

@acornett Do you know of any temporary workarounds for this or will I have to avoid using enums all-together?

madshargreave avatar Dec 18 '18 13:12 madshargreave

@acornett noted. Although I have no timeline for finishing the prototype of the overhaul at this time.

TiFu avatar Dec 18 '18 21:12 TiFu

@madshargreave I have not been able to dig into it enough to develop a work around. I will see about working on a more concrete proposal (or if my schedule really opens up maybe a pull request!)

acornett avatar Dec 19 '18 13:12 acornett

see also https://github.com/OpenAPITools/openapi-generator/pull/1884

macjohnny avatar Jan 11 '19 11:01 macjohnny

@TiFu generally the idea is good. However I would strongly disagree on making more abstractions (like HTTP library) on the generated files. Generated files should use directly the chosen HTTP library (e.g. fetch), using an abstraction will make the files more complex and less readable especially to those new to openapi-generator (which they shouldn't need to understand). The goal is, as long as they know fetch then they can read, use, and modify the generated code.

ceefour avatar Jan 20 '19 05:01 ceefour

@ceefour Thanks for the feedback. Unfortunately not using an abstraction for HTTP makes the generator code far more complex e.g. we would have to change how we create the requests (including header params, query params, ...) for every HTTP library and wherever we use it. This would lead to duplicated code on our site which is harder to maintain and prone to bugs.

In the end, the goal would be for people to not have to modify the code directly, but make it extensible through middleware (like we plan on doing for HTTP - you can simply modify the HTTP Request and before its being parsed/send).

Other opinions regarding @ceefour's feedback?

@taxpon (2017/07) @sebastianhaas (2017/07) @kenisteward (2017/07) @Vrolijkx (2017/09) @macjohnny (2018/01)

TiFu avatar Jan 20 '19 10:01 TiFu

@TiFu is it possible to put the "library" on the generator instead.

So parent generators can call implementation-specific "child" generators, keeping the overall project structure uniform.

I'm afraid that by having a runtime library, while it does reduce generator complexity but overall complexity is the same, just moved from the generator to the runtime library.

ceefour avatar Jan 20 '19 13:01 ceefour

@TiFu I support your point that without runtime http abstraction (which is just a single layer and easy to understand) the complexity of the codegen cannot be decreased. @ceefour can you give an example of your idea about "putting the library on the generator instead"?

Personally, I never have a look at the generated code in a project, since it does its job.

macjohnny avatar Jan 21 '19 06:01 macjohnny