Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Models get mixed when using structs from several packages #2783

Open
janosdebugs opened this issue Jun 4, 2022 · 3 comments · May be fixed by #2955
Open

Models get mixed when using structs from several packages #2783

janosdebugs opened this issue Jun 4, 2022 · 3 comments · May be fixed by #2955
Labels
generate spec Related to spec generation from code scanner

Comments

@janosdebugs
Copy link

janosdebugs commented Jun 4, 2022

Problem statement

When generating spec from code, using structs with the same name from different packages results in their model definitions getting mixed up. This is a major problem when integrating third party packages (e.g. Docker and Kubernetes in the same codebase).

Swagger specification

{
  "swagger": "2.0",
  "paths": {},
  "definitions": {
    "Test": {
      "type": "object",
      "required": [
        "c"
      ],
      "properties": {
        "c": {
          "type": "string",
          "x-go-name": "C"
        }
      },
      "x-go-package": "test/c"
    },
    "TestResponseBody": {
      "type": "object",
      "required": [
        "test1",
        "test2"
      ],
      "properties": {
        "test1": {
          "$ref": "#/definitions/Test"
        },
        "test2": {
          "$ref": "#/definitions/Test"
        }
      },
      "x-go-package": "test"
    }
  },
  "responses": {
    "TestResponseBody": {
      "description": "",
      "schema": {
        "$ref": "#/definitions/TestResponseBody"
      }
    }
  }
}

Steps to reproduce

In package A:

// swagger:model TestResponseBody
type TestResponseBody struct {
    // required: true
    // in: body
    Test1 b.Test `json:"test1"`
    // required: true
    // in: body
    Test2 c.Test `json:"test2"`
}

// swagger:response TestResponseBody
type TestResponse struct {
    // required: true
    // in: body
    Body TestResponseBody
}

In package B:

// swagger:model Test
type Test struct {
    B string `json:"b"`
}

In package C:

// swagger:model Test
type Test struct {
    // required: true
    C string `json:"c"`
}

Then run: swagger generate spec.

This will result in the Test model being added only once. Instead, the generation routine should start numbering the models.

Why naming models explicitly doesn't work

When integrating third party data structures, the comments can't be applied to the codebase. This is the case when integrating Docker and Kubernetes, both have a model called Volume, which will result in the following error:

Validation failed. Property 'Driver' listed as required but does not exist in '/definitions/Volume'

Environment

swagger version: 0.29.0
go version: 1.18.2
OS: linux/amd64

@josephspurrier
Copy link
Contributor

I'm noticing the same issue on my side where we have models names similarly across packages. I got around this by using // swagger:model NAME but that's very tedious.

@janosdebugs
Copy link
Author

@josephspurrier indeed, but this is impossible if you are using structs that are from a third party library.

@josephspurrier
Copy link
Contributor

Right. I was hoping this was going to fix my issue where I define structs using the same names in different packages, but unfortunately it doesn't. The Response name is only defined once in the spec that is generated, but they are different structs so they should be separate entities.

    "FinishResponse": {
      "description": "Result of the operation.",
      "schema": {
        "$ref": "#/definitions/Response"
      }
    },
    "StartResponse": {
      "description": "Result of the operation.",
      "schema": {
        "$ref": "#/definitions/Response"
      }
    },

This is what each of my packages look like:

// Package start_response

// swagger:response StartResponse
type swaggerResponse struct {
	// in: body
	Body Response
}

type Response struct {
	// Required: true
	Data *Data `json:"data"`
}
// Package finish_response

// swagger:response FinishResponse
type swaggerResponse struct {
	// in: body
	Body Response
}

type Response struct {
	// Required: true
	Data *Data `json:"data"`
}

rwwiv added a commit to rwwiv/go-swagger that referenced this issue Jul 18, 2023
@rwwiv rwwiv linked a pull request Jul 18, 2023 that will close this issue
@fredbi fredbi added scanner generate spec Related to spec generation from code labels Dec 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
generate spec Related to spec generation from code scanner
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants