Skip to content

Generate TypeScript code from OpenAPI spec #10181

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

Closed
timroes opened this issue Feb 8, 2022 · 2 comments · Fixed by #12071
Closed

Generate TypeScript code from OpenAPI spec #10181

timroes opened this issue Feb 8, 2022 · 2 comments · Fixed by #12071
Assignees
Labels
area/frontend Related to the Airbyte webapp autoteam technical-debt issues to fix code smell

Comments

@timroes
Copy link
Contributor

timroes commented Feb 8, 2022

We should use the openAPI spec and generate TypeScript typings from them (the same as we do in Java). That way we'll be able to prevent errors from changes in the API affecting the frontend, as well as errors happening from manually typing new endpoints. Also reviewing PRs that will add new APIs will get easier, since currently this involves a lot of time comparing TypeScript types against the OpenAPI specification to make sure they are correct.

We have two options for generations:

  • Generate the whole querying code via the generator, or
  • just generate the types of the queries and return values and use them in our own written query code.

Since we currently use react-query, and most common OpenAPI generators are not able to generate query code for that directly, I'd suggest we only generate types for now. Also this will keep us a bit more flexible around our query infrastructure (e.g. around adding Auth to it, etc.), than generating the full query code.

We'll need to figure out which generator to use. The popular OpenAPI generator does unfortunatelly only support generating full query code in TypeScript as far as I can tell. We might need to look for other libraries or check if we can easily just copy types from the query generated code.

@jamakase
Copy link
Contributor

jamakase commented Mar 1, 2022

Tried to use openAPI generator with next command

docker run --rm -v "${PWD}:/local" openapitools/openapi-generator-cli generate \
    -i https://raw.githubusercontent.com/airbytehq/airbyte/master/airbyte-api/src/main/openapi/config.yaml \
    -g typescript \
    -o /local/out/ts

But faced next error

[main] INFO  o.o.codegen.DefaultGenerator - Model ConnectionStateObject not generated since it's a free-form object
Exception in thread "main" java.lang.RuntimeException: Could not process model 'CompleteSourceOauthRequest'.Please make sure that your schema is correct!
	at org.openapitools.codegen.DefaultGenerator.generateModels(DefaultGenerator.java:492)
	at org.openapitools.codegen.DefaultGenerator.generate(DefaultGenerator.java:888)
	at org.openapitools.codegen.cmd.Generate.execute(Generate.java:441)
	at org.openapitools.codegen.cmd.OpenApiGeneratorCommand.run(OpenApiGeneratorCommand.java:32)
	at org.openapitools.codegen.OpenAPIGenerator.main(OpenAPIGenerator.java:66)
Caused by: java.lang.ClassCastException: class java.lang.Boolean cannot be cast to class io.swagger.v3.oas.models.media.Schema (java.lang.Boolean is in module java.base of loader 'bootstrap'; io.swagger.v3.oas.models.media.Schema is in unnamed module of loader 'app')
	at org.openapitools.codegen.languages.TypeScriptClientCodegen.getTypeDeclaration(TypeScriptClientCodegen.java:891)
	at org.openapitools.codegen.DefaultCodegen.fromProperty(DefaultCodegen.java:3538)
	at org.openapitools.codegen.DefaultCodegen.addVars(DefaultCodegen.java:5248)
	at org.openapitools.codegen.DefaultCodegen.addVars(DefaultCodegen.java:5190)
	at org.openapitools.codegen.DefaultCodegen.updateModelForObject(DefaultCodegen.java:2619)
	at org.openapitools.codegen.DefaultCodegen.fromModel(DefaultCodegen.java:2765)
	at org.openapitools.codegen.DefaultGenerator.processModels(DefaultGenerator.java:1270)
	at org.openapitools.codegen.DefaultGenerator.generateModels(DefaultGenerator.java:487)
	... 4 more

More investigation is required.

@jamakase
Copy link
Contributor

Looks like typescript-fetch generator works much better.

docker run --rm -v "${PWD}:/local" openapitools/openapi-generator-cli generate  \
    -i https://raw.githubusercontent.com/airbytehq/airbyte/master/airbyte-api/src/main/openapi/config.yaml \
    -g typescript-fetch \
    -o /local/out/ts

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/frontend Related to the Airbyte webapp autoteam technical-debt issues to fix code smell
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants