chrusty / protoc-gen-jsonschema

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

`oneof` generates both alternates in parallel #135

Closed comdiv closed 1 year ago

comdiv commented 1 year ago
syntax = "proto3";
package somepackage;
message A {
    oneof body {
         X x = 1;
         Y y = 2;
    }
}
message X {
      int x = 1;
}
message Y {
      int x = 1;
}

GENERATES ->

 {
    "$schema": "http://json-schema.org/draft-04/schema#",
    "$ref": "#/definitions/A",
    "definitions": {
        "A": {
            "properties": {
                "x": {
                    "$ref": "#/definitions/somepackage.X",
                    "additionalProperties": true
                },
                "y": {
                    "$ref": "#/definitions/somepackage.Y",
                    "additionalProperties": true
                }
            },
            "additionalProperties": true,
            "type": "object",
            "title": "A"
        },
        "somepackage.X": {
            "properties": {
                "x": {
                    "type": "integer"
                }
            },
            "additionalProperties": true,
            "type": "object",
            "title": "X"
        },
        "somepackage.Y": {
            "properties": {
                "x": {
                    "type": "integer"
                }
            },
            "additionalProperties": true,
            "type": "object",
            "title": "Y"
        }
    }
}

As you can see JsonSchema allows both x and y property persists in one single JSON , while protobuf schema defines that just one property should occurs...

Is where any way to generate more valid JsonSchema or it doesn't support it by nature?

michaelrosett commented 1 year ago

Try prefixing enforce_oneof=true: to your --jsonschema_out protoc argument.

chrusty commented 1 year ago

Thank you @michaelrosett! @comdiv would you be able to try this flag and let us know if it solves your issue?

comdiv commented 1 year ago

Yes, it generates another more correct varianr, thx!

ср, 11 янв. 2023 г., 06:13 Prawn @.***>:

Thank you @michaelrosett https://github.com/michaelrosett! @comdiv https://github.com/comdiv would you be able to try this flag and let us know if it solves your issue?

— Reply to this email directly, view it on GitHub https://github.com/chrusty/protoc-gen-jsonschema/issues/135#issuecomment-1378109719, or unsubscribe https://github.com/notifications/unsubscribe-auth/AASG7FTRAKOZODP3JE2KLLLWRYCL5ANCNFSM6AAAAAATN7D5UQ . You are receiving this because you were mentioned.Message ID: @.***>

chrusty commented 1 year ago

Glad you got it working!