chrusty / protoc-gen-jsonschema

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

bug: enforce_oneof with json_fieldnames results in inconsistent schema #152

Closed sherif-elmetainy closed 1 year ago

sherif-elmetainy commented 1 year ago

Hello and thanks for this nice package.

I encountered the following problem with generating schemas using the following options:

protoc --plugin=$HOME/go/bin/protoc-gen-jsonschema --jsonschema_opt=enforce_oneof --jsonschema_opt=file_extension=schema.json --jsonschema_out=json_fieldnames:. --proto_path=. my_message.proto

I use snake_case for the field names. for example my_first_field. the enforce_oneof option results in oneOf property in the schema having the field names in snake_case. However, the json_fieldname option causes the properties property in the schema to list the fields using camelCase. This inconsistency result in schema that can never be valid.

Here is a test message to demonstrate the problem:

message MyMessage {
    optional string my_first_field = 1;
    optional string my_second_field = 2;
}

And here is the schema generated by running the command:

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "$ref": "#/definitions/MyMessage",
    "definitions": {
        "MyMessage": {
            "properties": {
                "myFirstField": {
                    "type": "string"
                },
                "mySecondField": {
                    "type": "string"
                }
            },
            "additionalProperties": false,
            "type": "object",
            "oneOf": [
                {
                    "required": [
                        "my_first_field"
                    ]
                },
                {
                    "required": [
                        "my_second_field"
                    ]
                }
            ],
            "title": "My Message"
        }
    }
}
chrusty commented 1 year ago

Hello @sherif-elmetainy. I've merged a PR which may affect your issue. Are you able to build the latest master branch and give it a try?

sherif-elmetainy commented 1 year ago

@chrusty Thanks for the fix. The latest version fixed the bug. Now the oneOf properties and the properties are both using camel case.