The OpenAPI code generator makes client libraries from a swagger API. You can find it here:
https://github.com/OpenAPITools/openapi-generator
It’s fairly easy to run. In my case I’m trying to generate go:
java -jar openapi-generator-cli.jar generate -i tulip_api.json -g go -o out --skip-operation-example --skip-validate-spec --verbose
The input file is https://YOUR_HOST.tulip.co/docs/api/api.json
The problem is, there’s something about tulip_api.json that it doesn’t like, and I can’t figure out what it is:
[main] INFO o.o.codegen.TemplateManager - writing file /mnt/c/work/go/tulip-mqtt/crap/out/api_machine_monitoring.go
[main] INFO o.o.codegen.TemplateManager - writing file /mnt/c/work/go/tulip-mqtt/crap/out/docs/MachineMonitoringApi.md
[main] INFO o.o.codegen.TemplateManager - writing file /mnt/c/work/go/tulip-mqtt/crap/out/api_misc.go
[main] INFO o.o.codegen.TemplateManager - writing file /mnt/c/work/go/tulip-mqtt/crap/out/docs/MiscApi.md
[main] INFO o.o.codegen.TemplateManager - writing file /mnt/c/work/go/tulip-mqtt/crap/out/api_tulip_apps.go
[main] INFO o.o.codegen.TemplateManager - writing file /mnt/c/work/go/tulip-mqtt/crap/out/docs/TulipAppsApi.md
[main] INFO o.o.codegen.TemplateManager - writing file /mnt/c/work/go/tulip-mqtt/crap/out/api_tulip_machines.go
[main] INFO o.o.codegen.TemplateManager - writing file /mnt/c/work/go/tulip-mqtt/crap/out/docs/TulipMachinesApi.md
Exception in thread "main" java.lang.RuntimeException: Could not generate api file for 'TulipTables'
at org.openapitools.codegen.DefaultGenerator.generateApis(DefaultGenerator.java:665)
at org.openapitools.codegen.DefaultGenerator.generate(DefaultGenerator.java:891)
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.NullPointerException
at org.openapitools.codegen.languages.GoClientCodegen.constructExampleCode(GoClientCodegen.java:541)
at org.openapitools.codegen.languages.GoClientCodegen.constructExampleCode(GoClientCodegen.java:492)
at org.openapitools.codegen.languages.GoClientCodegen.postProcessOperationsWithModels(GoClientCodegen.java:460)
at org.openapitools.codegen.DefaultGenerator.processOperations(DefaultGenerator.java:1212)
at org.openapitools.codegen.DefaultGenerator.generateApis(DefaultGenerator.java:568)
I’m not sure where the problem is, but there’s something about the ‘tables’ api description in the swagger file that it doesn’t like. It generates the object models OK but when it generates the API modules it creates api_misc.go, api_machine_monitoring.go, api_tulip_machines.go, api_tulip_apps.go, then tries to generate tables and it fails. There must be something different about the way the tables API is defined in the swagger.
I tried separately generating ONLY the documentation (in markdown) and that process worked without errors. So the problem seems to be in the code generation itself.
A big part of the point of using swagger is code generation, and this particular generator is fairly widely used. I use it to generate go and java clients all the time.
The code generator does generate some warnings, but I don’t think they’re really related. I’m not fluent enough with OpenAPI 3 to know if these are actual problems:
[main] WARN o.o.codegen.DefaultCodegen - allOf with multiple schemas defined. Using only the first one: AnyType
[main] WARN o.o.codegen.DefaultCodegen - allOf with multiple schemas defined. Using only the first one: ApiError
[main] WARN o.o.codegen.DefaultCodegen - allOf with multiple schemas defined. Using only the first one: CRUDModel
[main] WARN o.o.codegen.DefaultCodegen - Empty operationId found for path: get /tables/{tableId}/runAggregation. Renamed to auto-generated operationId: tablesTableIdRunAggregationGet
[main] WARN o.o.codegen.DefaultCodegen - Empty operationId found for path: patch /tables/{tableId}/records/{recordId}/increment. Renamed to auto-generated operationId: tablesTableIdRecordsRecordIdIncrementPatch
[main] WARN o.o.codegen.DefaultCodegen - Empty operationId found for path: post /attributes/report. Renamed to auto-generated operationId: attributesReportPost
[main] WARN o.o.codegen.DefaultCodegen - More than one inline schema specified in allOf:. Only the first one is recognized. All others are ignored.
[main] WARN o.o.codegen.utils.ModelUtils - #/components/parameters/tableFilterAggregator is not defined
Any ideas?