meetup / rules_openapi Goto Github PK
View Code? Open in Web Editor NEW๐ bazel rules for generating code from openapi specifications
License: MIT License
๐ bazel rules for generating code from openapi specifications
License: MIT License
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
`
ERROR: /private/var/tmp/_bazel_dkapoor/3baa8d03663d350dcfcfed1ae0b311db/external/io_bazel_rules_openapi/openapi/openapi.bzl:5:5: //external:io_bazel_rules_openapi_io_swagger_swagger_codegen_cli: no such attribute 'sha256' in 'maven_jar' rule
maven_jar rule is deprecated in favor of rules_jvm_external
https://docs.bazel.build/versions/master/be/workspace.html#maven_jar
The above works with bazel 0.23, fails with bazel 0.29.1-homebrew
In May 2018, swagger-codegen contributors have decided to fork Swagger Codegen to maintain a community-driven version called "OpenAPI Generator" (https://openapi-generator.tech), which supports both OpenAPI/Swagger spec v2 and v3.
For the reasons behind the fork, please refer to the Q&A: https://github.com/OpenAPITools/openapi-generator/blob/master/docs/qna.md.
For migration, please refer to https://github.com/OpenAPITools/openapi-generator/blob/master/docs/migration-from-swagger-codegen.md
I have the following code:
load("@io_bazel_rules_openapi//openapi:openapi.bzl", "openapi_gen")
load("@io_bazel_rules_go//go:def.bzl", "go_library")
openapi_gen(
name="sample-go-src",
language="go",
spec="sample.yaml",
api_package = "com.shingo.swagger.api",
model_package = "com.shingo.swagger.model",
)
go_library(
name = "sample-go",
srcs = ["//bazel-bin/model/sample_go_codegen.srcjar"], # produced by sample-go-src
)
If I build sample-go-src
, I can see that it builds successfully and outputs a .srcjar
file.
bazel build -c opt model:sample-go-src 1 err | 16:51:20
INFO: Analyzed target //model:sample-go-src (0 packages loaded, 0 targets configured).
INFO: Found 1 target...
Target //model:sample-go-src up-to-date:
bazel-bin/model/sample-go-src_codegen.srcjar
INFO: Elapsed time: 0.634s, Critical Path: 0.56s
INFO: 2 processes: 1 internal, 1 linux-sandbox.
INFO: Build completed successfully, 2 total actions
However, if I build sample-go
, I get the below error:
bazel build -c opt model:sample-go 2 err | 16:54:00
ERROR:<workspace-dir>/model/BUILD:14:11: no such package 'bazel-bin/model/sample_go_codegen.srcjar': BUILD file not found in any of the following directories. Add a BUILD file to a directory to mark it as a package.
-<workspace-dir>/bazel-bin/model/sample_go_codegen.srcjar and referenced by '//model:sample-go'
ERROR: Analysis of target '//model:sample-go' failed; build aborted: no such package 'bazel-bin/model/sample_go_codegen.srcjar': BUILD file not found in any of the following directories. Add a BUILD file to a directory to mark it as a package.
- <workspace-dir>/bazel-bin/model/sample_go_codegen.srcjar
INFO: Elapsed time: 0.062s
INFO: 0 processes.
FAILED: Build did NOT complete successfully (1 packages loaded, 0 targets configured)
Consequently, I cannot declare dependency on the go_library
to use in my Go code.
To me the question is how to handle other openapi implementations e.g. go-swagger? We have prel. support in rules_go
since it needs the go toolchain.
Hello, in order to use v7.4.0
from https://github.com/OpenAPITools/openapi-generator it was necessary for me to run the tool (in the ci pipeline) with another java toolchain:
container_pull(
name = "bazel",
digest = "sha256:3348b14c7b45a4fbf06fee34996a8d15da72f43bb282a81f4720a0c0a5c37dca", # https://console.cloud.google.com/gcr/images/bazel-public/global/bazel
registry = "gcr.io",
repository = "bazel-public/bazel",
)
diff --git openapi/openapi.bzl openapi/openapi.bzl
index 76f4467..8296635 100644
--- openapi/openapi.bzl
+++ openapi/openapi.bzl
@@ -141,7 +141,7 @@ def _new_generator_command(ctx, gen_dir, rjars):
# fixme: by default, swagger-codegen is rather verbose. this helps with that but can also mask useful error messages
# when it fails. look into log configuration options. it's a java app so perhaps just a log4j.properties or something
- gen_cmd += " 2>/dev/null"
+ #gen_cmd += " 2>/dev/null"
return gen_cmd
def _impl(ctx):
@@ -243,7 +243,7 @@ openapi_gen = rule(
"type_mappings": attr.string_dict(),
"import_mappings": attr.string(),
"_jdk": attr.label(
- default = Label("@bazel_tools//tools/jdk:current_java_runtime"),
+ default = Label("@bazel_tools//tools/jdk:remote_jdk11"),
providers = [java_common.JavaRuntimeInfo],
),
"codegen_cli": attr.label(
would you be open to make the _jdk
arg public? ref: https://github.com/meetup/rules_openapi/blob/master/openapi/openapi.bzl#L245
openapi generator v7.4.0
rules_openapi e42769a98fef5634bee0f921e4b90786a6b3292aa11fe8d2f84c045ac435ab29
bazel 7.1.0
Related to bazelbuild/bazel#10131
what is the difference between this repo and https://github.com/OpenAPITools/openapi-generator-bazel. are they two individual bazel rule project for openapi generator? or difference scopes? I found committers are almost same group of people. Thanks.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.