meetup / rules_openapi

🍃 bazel rules for generating code from openapi specifications
MIT License
52 stars 25 forks source link

Support OpenApi-generator v5.0.0 #48

Closed ex0ns closed 1 year ago

ex0ns commented 3 years ago

Hello

I am trying to use this rules to generate scala code from an OpenAPI 3.0 spec, my WORKSPACE file contains the following section:

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

RULES_OPEN_API_VERSION = "f0f42afb855139ad5346659d089c32fb756d068e"

RULES_OPEN_API_SHA256 = "9570186948f1f65c61d2c6c6006840ea70888b270f028bbd0eb736caae1cd9df"

http_archive(
     name = "io_bazel_rules_openapi",
     strip_prefix = "rules_openapi-%s" % RULES_OPEN_API_VERSION,
     url = "https://github.com/meetup/rules_openapi/archive/%s.tar.gz" % RULES_OPEN_API_VERSION,
     sha256 = RULES_OPEN_API_SHA256
)

openapi_repositories(
    codegen_cli_version = "5.0.0",
    codegen_cli_sha256 = "839fade01e54ce1eecf012b8c33adb1413cff0cf2e76e23bc8d7673f09626f8e",
    codegen_cli_provider = "openapi"
)

I have to use the openapi provider because it is the only one that support OpenAPI 3.

My BUILD file contains

load("@io_bazel_rules_openapi//openapi:openapi.bzl", "openapi_gen")

openapi_gen(
  name = "client-src",
  spec = "api.yaml",
  api_package = "com.example.api",
  model_package = "com.example.model",
  invoker_package = "com.example",
  language = "scala-sttp",
  additional_properties = {
    "jsonLibrary": "circe",
    "sttpClientVersion": "2.2.0"
  }
)

When building this target, the build fails. After investigation, the generated command is:

external/local_jdk/bin/java -cp external/io_bazel_rules_openapi_org_openapitools_openapi_generator_cli/openapi-generator-cli-5.0.0.jar: org.openapitools.codegen.OpenAPIGenerator generate -i backend/openapi/api.yaml -g scala-sttp -o backend/openapi/client-src -D "" --additional-properties "jsonLibrary=circe,sttpClientVersion=2.2.0" --type-mappings "" --api-package com.example.api --invoker-package com.example --model-package com.example.model

Which fails with [error] Found unexpected parameters: [-D, ]

This issue was documented on OpenAPI-generator side: https://github.com/OpenAPITools/openapi-generator/blob/150e24dc553a8ea5230ffb938ed3e6020e972faa/docs/global-properties.md#note-on-global-property-declaration

The fix would be to use --global-property instead of -D, I can open a PR for that, but I would need a hand to detect if the version of open api is greater than 5.0.0.

if _is_openapi_codegen(ctx) and _openapi_major_version(ctx) > 5:
        gen_cmd += ' --global-property "{properties}"'.format(
            properties = _comma_separated_pairs(ctx.attr.system_properties),
        )
else:
        gen_cmd += ' -D "{properties}"'.format(
            properties = _comma_separated_pairs(ctx.attr.system_properties),
        )

However I don't know what would be the proper way to extract the version number from ctx.file.codegen_cli.path, should we look for a X.Y.Z regex ?

Thanks `

yishanhe commented 3 years ago

@ex0ns Thanks for working on supporting 5.0.0

I think parse

ctx.file._codegen_cli.basename

will help. at least for jar name convention, we all have version in the name.

ex0ns commented 3 years ago

Pull request is now available https://github.com/meetup/rules_openapi/pull/49