go-swagger icon indicating copy to clipboard operation
go-swagger copied to clipboard

go-swagger doesn't work for group type ?

Open zhiqiangxu opened this issue 6 years ago • 0 comments

Problem statement

It seems go-swagger doesn't work properly for group type, it's a follow up of issue 1890, since it's closed I think a new issue is deserved.

Actually 2 problems:

  1. group type problem
  2. difference between 2 separate files and 1 single file

Swagger specification

Swagger 2.0

Steps to reproduce

It works if the two types(annotated with swagger:parameters and swagger:model ) are defined in separate file:

foo.go:

package public

type (

	// FooOutput for output
	// swagger:model FooOutput
	FooOutput struct {
		Addr string
	}
)

// Foo for socket addr
func Foo() {
	// swagger:route GET /foo FooInput
	// get socket address
	//     Responses:
	//       200: body:FooOutput

}

bar.go:

package public
// FooInput for input
// swagger:parameters FooInput
type FooInput struct {
    // in: query
    App string
    // in: query
    UID string
}

The generated spec(swagger generate spec -m) is ok:

{
  "swagger": "2.0",
  "paths": {
    "/foo": {
      "get": {
        "description": "get socket address",
        "operationId": "FooInput",
        "parameters": [
          {
            "type": "string",
            "name": "App",
            "in": "query"
          },
          {
            "type": "string",
            "name": "UID",
            "in": "query"
          }
        ],
        "responses": {
          "200": {
            "description": "FooOutput",
            "schema": {
              "$ref": "#/definitions/FooOutput"
            }
          }
        }
      }
    }
  },
  "definitions": {
    "FooOutput": {
      "type": "object",
      "properties": {
        "Addr": {
          "type": "string"
        }
      },
      "x-go-package": "public"
    }
  }
}

But if I combined the two files into one file with group type:

package public

type (
	// FooInput for input
	// swagger:parameters FooInput
	FooInput struct {
		// in: query
		App string
		// in: query
		UID string
	}


	// FooOutput for output
	// swagger:model FooOutput
	FooOutput struct {
		Addr string
	}
)

// Foo for socket addr
func Foo() {
	// swagger:route GET /foo FooInput
	// get socket address
	//     Responses:
	//       200: body:FooOutput

}

the generated spec is wrong:

{
  "swagger": "2.0",
  "paths": {
    "/foo": {
      "get": {
        "description": "get socket address",
        "operationId": "FooInput",
        "responses": {
          "200": {
            "description": "FooOutput",
            "schema": {
              "$ref": "#/definitions/FooOutput"
            }
          }
        }
      }
    }
  },
  "definitions": {
    "FooInput": {
      "type": "object",
      "properties": {
        "App": {
          "description": "in: query",
          "type": "string"
        },
        "UID": {
          "description": "in: query",
          "type": "string"
        }
      },
      "x-go-package": "public"
    },
    "FooOutput": {
      "type": "object",
      "properties": {
        "Addr": {
          "type": "string"
        }
      },
      "x-go-package": "public"
    }
  }
}

And even if without group type:

package public

// FooInput for input
// swagger:parameters FooInput
type FooInput struct {
	// in: query
	App string
	// in: query
	UID string
}


// FooOutput for output
// swagger:model FooOutput
type FooOutput struct {
	Addr string
}

// Foo for socket addr
func Foo() {
	// swagger:route GET /foo FooInput
	// get socket address
	//     Responses:
	//       200: body:FooOutput

}

the generated spec is a little different from when generated from separate files(FooInput appears in definitions):

{
  "swagger": "2.0",
  "paths": {
    "/foo": {
      "get": {
        "description": "get socket address",
        "operationId": "FooInput",
        "parameters": [
          {
            "type": "string",
            "name": "App",
            "in": "query"
          },
          {
            "type": "string",
            "name": "UID",
            "in": "query"
          }
        ],
        "responses": {
          "200": {
            "description": "FooOutput",
            "schema": {
              "$ref": "#/definitions/FooOutput"
            }
          }
        }
      }
    }
  },
  "definitions": {
    "FooInput": {
      "description": "FooInput for input",
      "type": "object",
      "properties": {
        "App": {
          "description": "in: query",
          "type": "string"
        },
        "UID": {
          "description": "in: query",
          "type": "string"
        }
      },
      "x-go-package": "public"
    },
    "FooOutput": {
      "description": "FooOutput for output",
      "type": "object",
      "properties": {
        "Addr": {
          "type": "string"
        }
      },
      "x-go-package": "public"
    }
  }
}

Environment

latest master branch

OS: mac osx

zhiqiangxu avatar Feb 28 '19 03:02 zhiqiangxu