Skip to content

Generates unnecessary definitions when struct inherits the fields from another #81

Description

@SVilgelm

I'm currently using very outdated library v1.1.1-0.20190620131940-98aa997b1687, and I want to upgrade to latest, but I was really surprised when the swagger validate shows much more warnings with new version.

Here is an example of two structs:

type Parent struct {
	FieldA string
}

type Child struct {
	Parent
	FieldB int
}

with v1.1.1 it generates just one definition:

{
  "definitions": {
    "restfulspec.Child": {
      "required": [
        "FieldA",
        "FieldB"
      ],
      "properties": {
        "FieldA": {
          "type": "string"
        },
        "FieldB": {
          "type": "integer",
          "format": "int32"
        }
      }
    }
  }
}

but v2.6.1 generates two definitions:

{
  "definitions": {
    "restfulspec.Child": {
      "required": [
        "FieldA",
        "FieldB"
      ],
      "properties": {
        "FieldA": {
          "type": "string"
        },
        "FieldB": {
          "type": "integer",
          "format": "int32"
        }
      }
    },
    "restfulspec.Parent": {
      "required": [
        "FieldA"
      ],
      "properties": {
        "FieldA": {
          "type": "string"
        }
      }
    }
  }
}

and the swagger validate shows a warning:

WARNING: definition "#/definitions/restfulspec.Parents" is not used anywhere

It would be great if you can exclude fix the issue.
Here is the test case:

type Parent struct {
	FieldA string
}

type Child struct {
	Parent
	FieldB int
}

func TestParentChildArray(t *testing.T) {
	db := definitionBuilder{Definitions: spec.Definitions{}, Config: Config{}}
	db.addModelFrom(Child{})
	s := spec.Schema{
		SchemaProps: spec.SchemaProps{
			Definitions: db.Definitions,
		},
	}
	_, ok := db.Definitions["restfulspec.Parent"]
	if ok {
		data, _ := json.MarshalIndent(s, "", "  ")
		t.Log(string(data))
		t.Fail()
	}
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions