ForkDatabricks (DBRX)Databricks (DBRX)published Nov 26, 2024seen 5d

databricks/protoc-gen-jsonschema

forked from chrusty/protoc-gen-jsonschema

Open original ↗

Captured source

source ↗
published Nov 26, 2024seen 5dcaptured 10hhttp 200method plain

databricks/protoc-gen-jsonschema

Description: Protobuf to JSON-Schema compiler

Language: Go

License: Apache-2.0

Stars: 0

Forks: 0

Open issues: 0

Created: 2024-11-26T14:05:47Z

Pushed: 2026-05-21T08:37:50Z

Default branch: main

Fork: yes

Parent repository: chrusty/protoc-gen-jsonschema

Archived: no

README: Protobuf to JSON-Schema compiler ================================

This takes protobuf definitions and converts them into JSONSchemas, which can be used to dynamically validate JSON messages.

Useful for people who define their data using ProtoBuf, but use JSON for the "wire" format.

"Heavily influenced" by Google's protobuf-to-BigQuery-schema compiler.

> This is a Databricks fork slightly modified for Queta's usage.

Generated Schemas -----------------

  • One JSONSchema file is generated for each root-level proto message and ENUM. These are intended to be stand alone self-contained schemas which can be used to validate a payload derived from their source proto message
  • Nested message schemas become referenced "definitions". This means that you know the name of the proto message they came from, and their schema is not duplicated (within the context of one JSONSchema file at least)

Logic -----

  • For each proto file provided
  • Generates schema for each ENUM
  • JSONSchema filename deried from ENUM name
  • Generates schema for each Message
  • Builds a list of every nested message and converts them to JSONSchema
  • Recursively converts attributes and nested messages within the root message
  • Optionally makes all fields required
  • Optionally allows NULL values
  • Optionally allows additional properties
  • Optionally marks all fields required
  • Specially marked fields are labelled required (options.proto)
  • Specially marked fields are omitted (options.proto)
  • Special handling for "OneOf"
  • Special handling for arrays
  • Special handling for maps
  • Injects references to nested messages
  • JSONSchema filename derived from Message name
  • Bundles these into a protoc generator response

Installation ------------

> Note: This tool requires Go 1.11+ to be installed.

Install this plugin using Go:

go install github.com/chrusty/protoc-gen-jsonschema/cmd/protoc-gen-jsonschema@latest

Usage -----

> Note: This plugin requires the `protoc` CLI to be installed.

protoc-gen-jsonschema is designed to run like any other proto generator. The following examples show how to use options flags to enable different generator behaviours (more examples in the Makefile too).

protoc \ # The protobuf compiler
--jsonschema_out=. \ # jsonschema out directory
--proto_path=testdata/proto testdata/proto/ArrayOfPrimitives.proto # proto input directories and folders

Configuration Parameters ------------------------

The following configuration parameters are supported. They should be added to the protoc command and can be combined as a comma-delimited string. Some examples are included in the following Examples section.

Options can also be provided in this format (which is easier on the eye):

protoc \
--plugin=${HOME}/go/bin/protoc-gen-jsonschema \
--jsonschema_opt=enforce_oneof
--jsonschema_opt=file_extension=schema.json \
--jsonschema_opt=disallow_additional_properties \
--jsonschema_out=schemas \
--proto_path=proto

| CONFIG | DESCRIPTION | |--------|-------------| |all_fields_required| Require all fields in schema | |allow_null_values| Allow null values in schema | |debug| Enable debug logging | |disallow_additional_properties| Disallow additional properties in schema | |disallow_bigints_as_strings| Disallow big integers as strings | |enforce_oneof| Interpret Proto "oneOf" clauses | |enums_as_strings_only| Only include strings in the allowed values for enums | |file_extension| Specify a custom file extension for generated schemas | |json_fieldnames| Use JSON field names only | |prefix_schema_files_with_package| Prefix the output filename with package | |proto_and_json_fieldnames| Use proto and JSON field names | |type_names_with_no_package| When generating type names and refs, do not include the full package in the type name |

Custom Proto Options --------------------

If you don't want to use the configuration parameters (admittedly quite a nasty cli syntax) then some of the generator behaviour can be controlled using custom proto options. These are defined in [options.proto](options.proto), and your protoc command will need to include this file. See the [sample protos](internal/converter/testdata/proto) and generator commands in the [Makefile](Makefile).

Enum Options

These apply to specifically marked enums, giving you more finely-grained control than with the CLI flags.

  • [enums_as_constants](internal/converter/testdata/proto/ImportedEnum.proto): Encode ENUMs (and their annotations) as CONST
  • [enums_as_strings_only](internal/converter/testdata/proto/OptionEnumsAsStringsOnly.proto): ENUM values are only strings (not the numeric counterparts)
  • [enums_trim_prefix](internal/converter/testdata/proto/OptionEnumsTrimPrefix.proto): ENUM values have enum name prefix removed

Field Options

These apply to specifically marked fields, giving you more finely-grained control than with the CLI flags.

  • [ignore](internal/converter/testdata/proto/OptionIgnoredField.proto): Ignore (omit) a specific field
  • [required](internal/converter/testdata/proto/OptionRequiredField.proto): Mark a specific field as being REQUIRED

File Options

These options apply to an entire proto file.

  • [ignore](internal/converter/testdata/proto/OptionIgnoredFile.proto): Ignore (skip) a specific file
  • [extension](internal/converter/testdata/proto/OptionFileExtension.proto): Specify a custom file-extension for the generated schema for this file

Message Options

These options apply to a specific proto message.

  • [ignore](internal/converter/testdata/proto/OptionIgnoredMessage.proto): Ignore (skip) a specific message
  • [all_fields_required](internal/converter/testdata/proto/OptionRequiredMessage.proto): Mark all fields in a specific message as "required"
  • [allow_null_values](internal/converter/testdata/proto/OptionAllowNullValues.proto): Additionally allow null values for all fields in a message

-…

Excerpt shown — open the source for the full document.

Notability

notability 3.0/10

Routine fork by Databricks