chrusty / protoc-gen-jsonschema

Protobuf to JSON-Schema compiler
Apache License 2.0
496 stars 101 forks source link

Add `type_names_with_no_package` option to generate schemas without fully qualified package names #160

Closed blakesmith closed 9 months ago

blakesmith commented 1 year ago

Hi there!

tl;dr: This PR adds an option to disable fully qualified package type names, so they can be used for additional code generation.

More details:

I'm using this protobuf plugin to generate message schemas in both protobuf, as well as JSON compatible Java objects. My workflow is:

  1. Define messages in protobuf format.
  2. Use protoc-gen-jsonschema to generate a JSON Schema of objects.
  3. Use jsonschema2pojo to convert the JSON schema into plain old java object code.

The problem:

jsonschema2pojo needs package unqualified type names to generate proper class names, otherwise type names such as sample.MyMessage will generate a Java class named SampleMyMessage, instead of just MyMessage. jsonschema2pojo also treats dots as ref fragment delimiters by default, which breaks compilation entirely.

I added a simple flag that allows the caller to specify whether we want to generate package qualified type names (and their references) or not.

Thanks for the great tool, let me know what you think!

Tests: Unit.