swaggest / jsonschema-go

JSON Schema mapping for Go
https://pkg.go.dev/github.com/swaggest/jsonschema-go
MIT License
102 stars 13 forks source link

Duplicate Fields have empty definitions with `InlineRefs` enabled #87

Closed alirankine-dojo closed 11 months ago

alirankine-dojo commented 11 months ago

Describe the bug When using InlineRefs, multiple properties of the same type leave all but one fields without an inline definition.

To Reproduce When running the following code:

package jstest

import (
    "encoding/json"
    "testing"

    "github.com/swaggest/jsonschema-go"
)

type ExampleEvent struct {
    ID          string `json:"id,omitempty"`
    NewData     Data   `json:"new_data"`
    CurrentData Data   `json:"current_data"`
    OldData     Data   `json:"old_data"`
}

type Data struct {
    ID   string `json:"id,omitempty"`
    Name string `json:"name,omitempty"`
}

func TestRaw(t *testing.T) {
    ref := jsonschema.Reflector{}

    gen, _ := ref.Reflect(&ExampleEvent{}, func(rc *jsonschema.ReflectContext) {
        rc.InlineRefs = true
    })
    data, _ := json.MarshalIndent(gen, "", " ")
    t.Error(string(data))
}

The following schema is produced:

{
    "properties": {
        "current_data": {},
        "id": {
            "type": "string"
        },
        "new_data": {
            "properties": {
                "id": {
                    "type": "string"
                },
                "name": {
                    "type": "string"
                }
            },
            "type": "object"
        },
        "old_data": {}
    },
    "type": "object"
}

Expected behavior An inline definition should be produced for each of the 'current_data', 'new_data', old_data' fields

vearutop commented 11 months ago

Thank you for reporting this, please try new v0.3.53.