chrusty / protoc-gen-jsonschema

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

Cannot compile when two messages have the same name #85

Closed Kiyo5hi closed 3 years ago

Kiyo5hi commented 3 years ago

I've defined two protos with exactly the same content, but in different folders as such:

proto/
├─ a/
│  ├─ pet.proto
├─ b/
│  ├─ pet.proto

As I expect the output should be in the same structure as shown above just like when using --python_out or other options, protoc throws Tried to write the same file twice.

Even if I rename one of the messages to message Pet1, the output structure is flat, like so:

schema/
├─ Pet.jsonschema
├─ Pet1.jsonschema

Should that be fixed? Cuz reading and writing files recursively should be a feature.

chrusty commented 3 years ago

Hello @Kiyo5hi, and thanks for your interest in the project.

In your example, are your proto files separated into different packages? Proto packages are namespaced, and protoc-gen-jsonschema produces one file per message.

Eg is a/pet.proto in a package called "a", and b/pet.proto in a package with a different name like "b"?

If so, then you could look at using the prefix_schema_files_with_package option. This would actually result in the generated jsonschema files being in different directories (matching your proto package structure). This option was introduced a long time ago and probably doesn't get a lot of use, but it might just be the thing you need! You will see an example of this here.

Please let me know how you get on with this!

Otherwise you could consider just running protoc more than once (once against the A directory, then a second time against the B directory (though I'm not entirely sure of your use-case of course).

Kiyo5hi commented 3 years ago

That does the math for me, though it does not maintain the original directory structure but using packages is a solution; thank you!

BTW, how do I apply multiple options at once?

chrusty commented 3 years ago

I'm pleased to hear that works!

Use multiple options with this syntax:

protoc --jsonschema_out=prefix_schema_files_with_package,all_fields_required:.

chrusty commented 3 years ago

(Separated by comma)