/rules_openapi

🍃 bazel rules for generating code from openapi specifications

Primary LanguagePythonMIT LicenseMIT

bazel openapi rules Build Status

Bazel rules for generating sources and libraries from openapi schemas

Rules

Getting started

To use the Openapi rules, add the following to your projects WORKSPACE file

rules_openapi_version="c449fd6d1ac8b3dffb8bae3bca7305167b800267" # update this as needed

git_repository(
    name = "io_bazel_rules_openapi",
    commit = rules_openapi_version,
    remote = "git@github.com:meetup/rules_openapi.git",
)

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

Then in your BUILD file, just add the following so the rules will be available:

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

openapi_gen

openapi_gen(name, spec, api_package, model_package, invoker_package)

Generates .srcjar containing generated source files from a given openapi specification

These rules rely on swagger-codegen which defines many configuration options. Not all configuration options are implemented in these rules yet but contributions are welcome. You can also request features here

Attributes
name Name, required

A unique name for this rule.

spec String, required

Path to .yaml or .json file containing openapi specification

language String, required

Name of language to generate.

If you wish to use a custom language, you'll need to create a jar containing your custom codegen module, then use deps to add the custom codegen module to the classpath.

Note, not all swagger codegen provided languages generate the exact same source given the exact same set of arguments. Be aware of this in cases where you expect bazel not to perform a previous executed action for the same sources.

api_package String, optional

package for api.

module_package String, optional

package for models.

invoker_package String, optional

package for invoker.

additional_properties Dict of strings, optional

Additional properties that can be referenced by the codegen templates. This allows setting parameters that you'd normally put in config.json, for example the Java library template:

    language = "java",
    additional_properties = {
        "library": "feign",
    },
system_properties Dict of strings, optional

System properties to pass to swagger-codegen. This allows setting parameters that you'd normally set with -D, for example to disable test generation:

    language = "java",
    system_properties = {
        "apiTests": "false",
        "modelTests": "false",
    },
type_mappings Dict of strings, optional

Allows control of the types used in generated code with swagger-codegen's --type-mappings parameter. For example to use Java 8's LocalDateTime class:

    language = "java",
    additional_properties = {
        "dateLibrary": "java8",
    },
    type_mappings = {
        "OffsetDateTime": "java.time.LocalDateTime",
    },

An example of what a custom language may look like

java_import(
  name = "custom-scala-codegen",
  jars = ["custom-scala-codegen.jar"]
)

openapi_gen(
  name = "petstore-client-src",
  language = "custom-scala",
  spec = "petstore-spec.json",
  api_package = "com.example.api",
  model_package = "com.example.model",
  invoker_package = "com.example",
  deps = [
    ":custom-scala-codegen"
  ]
)

scala_library(
   name = "petstore-client",
   srcs = [":petstore-client-src"]
)

Meetup 2017