Skip to content

Commit

Permalink
feat: Upgrade to openapi-generator 4.3.1 + remove openapi-v2 converter
Browse files Browse the repository at this point in the history
  • Loading branch information
gnuletik committed May 12, 2020
1 parent eb4682c commit 7d738c7
Show file tree
Hide file tree
Showing 30 changed files with 651 additions and 2,665 deletions.
2 changes: 1 addition & 1 deletion .openapi-generator/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4.3.0
4.3.1
28 changes: 7 additions & 21 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
generator-dir := generator
generator-config := $(addprefix $(generator-dir)/,typescript-fetch.config.yaml)
spec-openapi-v3 := $(addprefix ${generator-dir}/,openapi-v3.yml)
spec-openapi-v2 := $(addprefix ${generator-dir}/,openapi-v2.yml)
spec-openapi := $(addprefix ${generator-dir}/,openapi.yml)
api-dest := .
API_URL = https://wasabi.telemeta.org/timeside/api/
OPEN_API_URL = https://sandbox.wasabi.telemeta.org/timeside/api/schema/

all: install api

Expand All @@ -13,31 +12,18 @@ ifeq (, $(shell which docker))
$(error No "docker" in PATH, consider doing apt install docker)
endif

# Check api-spec-converter
ifeq (, $(shell which api-spec-converter))
$(error No "api-spec-converter" in PATH, consider doing "sudo npm install -g api-spec-converter")
endif

# Log dependencies' version
api-spec-converter --version
docker --version

$(spec-openapi-v3):
curl --fail $(API_URL)openapi.yml -o $(spec-openapi-v3)

$(spec-openapi-v2): $(spec-openapi-v3)
api-spec-converter \
--from=openapi_3 \
--to=swagger_2 \
--syntax=yaml \
--order=alpha $(spec-openapi-v3) > $(spec-openapi-v2)
$(spec-openapi):
curl --fail $(OPEN_API_URL) -o $(spec-openapi)

api: $(spec-openapi-v2)
api: $(spec-openapi)
docker run --rm \
-v ${PWD}:/local \
--user "$$(id -u):$$(id -g)" \
openapitools/openapi-generator-cli:v4.3.0 generate \
-i /local/$(spec-openapi-v2) \
openapitools/openapi-generator-cli:v4.3.1 generate \
-i /local/$(spec-openapi) \
-g typescript-fetch \
-c /local/$(generator-config) \
-o /local/$(api-dest)
Expand Down
87 changes: 3 additions & 84 deletions generator/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,88 +93,11 @@ and it works ! However, we don't use Angular, so we'll have to find another solu


For more information on this generator and why they forked swagger-codegen, see https://github.com/OpenAPITools/openapi-generator/blob/master/docs/qna.md
As of today, the last version is [4.2.2](https://hub.docker.com/r/openapitools/openapi-generator-cli/tags)
As of today, the last version is [4.3.1](https://hub.docker.com/r/openapitools/openapi-generator-cli/tags)

```
docker run --rm -v ${PWD}:/local openapitools/openapi-generator-cli:v4.2.2 generate \
-i /local/openapi-schema.yaml \
-g typescript-fetch \
-o /local/openapi-generator-timeside-ts-fetch
```

But the specification fails to validate with the following errors:

```
Exception in thread "main" org.openapitools.codegen.SpecValidationException: There were issues with the specification. The option can be disabled via validateSpec (Maven/Gradle) or --skip-validate-spec (CLI).
| Error count: 43, Warning count: 0
Errors:
-attribute paths.'/timeside/api/selections/{uuid}/'(patch).requestBody.content.schema is not of type `object`
-attribute paths.'/timeside/api/experiences/'(post).requestBody.content.schema is not of type `object`
-attribute paths.'/timeside/api/providers/'(post).requestBody.content.schema is not of type `object`
-attribute paths.'/timeside/api/results/'(post).requestBody.content.schema is not of type `object`
-attribute paths.'/timeside/api/tasks/{uuid}/'(patch).requestBody.content.schema is not of type `object`
-attribute paths.'/timeside/api/annotation_tracks/{uuid}/'(patch).requestBody.content.schema is not of type `object`
-attribute paths.'/timeside/api/experiences/{uuid}/'(put).requestBody.content.schema is not of type `object`
....
```

I tried to validate the file with https://apitools.dev/swagger-parser/online/ and it pass the validation.

We can skip the validation with the `--skip-validate-spec` parameter but this will generate a non-compilable codebase (the `models` directory is imported in index.ts but it does not exist).

### Convert specs from OpenAPI v3.0 to OpenAPI v2.0

We can use the following tool to convert the specification from OpenAPI v3.0 to Swagger v2.0 : https://github.com/LucyBot-Inc/api-spec-converter

Install with:
```
sudo npm install -g api-spec-converter
```

And convert it with:
```
api-spec-converter \
--from=openapi_3 \
--to=swagger_2 \
--syntax=yaml \
--order=alpha openapi-schema.yaml > openapiv2-schema.yml
```

Note : During this conversion, we may lost some data because OpenAPI v3.0 specs include more information than Swagger v2.0 like :
- Added support for multiple root URLs.
- Added support for content type negotiation.
- Extended JSON Schema support to include `oneOf`, `anyOf` and `not` support.
- Added a callback mechanism to describe Webhooks.

For more informations, see https://blog.runscope.com/posts/tutorial-upgrading-swagger-2-api-definition-to-openapi-3


### Try again with swagger-codegen v2.X using OpenAPI v2 specs

Using swagger-codegen-cli
```
docker run --rm -v ${PWD}:/local swaggerapi/swagger-codegen-cli:2.4.10 generate \
-i /local/openapiv2-schema.yml \
-l typescript-fetch \
-o /local/swagger-codegen-timeside-ts-fetch
```

It works !

However, the generated code is not fully compatible with Typescript 3.6+. To make it work we need to:
- Install portable-fetch (npm install --save portable-fetch)
- Replace portable-fetch import : `import * as portableFetch from 'portable-fetch'` with `import portableFetch from 'portable-fetch'`
- Replace `GlobalFetch` type with `WindowOrWorkerGlobalScope['fetch']`
- Make an optional constructor call optional
- Give a default value for the name property of `RequiredError`
- Ask eslint to ignore the directory of the project

### Try again with openapi-generator usign OpenAPI v2 specs

Using openapi-generator
```
docker run --rm -v ${PWD}:/local openapitools/openapi-generator-cli:v4.2.2 generate \
-i /local/openapiv2-schema.yml \
docker run --rm -v ${PWD}:/local openapitools/openapi-generator-cli:v4.3.1 generate \
-i /local/openapi-schema.yml \
-g typescript-fetch \
-o /local/openapi-generator-timeside-ts-fetch
```
Expand All @@ -201,7 +124,3 @@ The generated build is better than the one generated by swagger-codegen because
- Compile successfully without any manual changes

So, `openapi-generator` seems to be the best option right now.

### Note for maintainer

In future months / years, OpenAPI v3.0 may be better supported by codegen tools. You may want to give another try to generate the client without converting it to OpenAPI v2.0.
Loading

0 comments on commit 7d738c7

Please sign in to comment.