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

Can't use Custom struct as data type in @Success #181

Closed
jac1013 opened this issue Aug 4, 2018 · 10 comments
Closed

Can't use Custom struct as data type in @Success #181

jac1013 opened this issue Aug 4, 2018 · 10 comments

Comments

@jac1013
Copy link

jac1013 commented Aug 4, 2018

Describe the bug
I can't use a custom struct as a success response.

To Reproduce
Steps to reproduce the behavior:

  1. Create your own struct only using primitive attributes.
  2. In your documentation try using it like this @Success 200 {array} path.to.struct.MyOwnStruct
  3. Generate the documentation (it should successfully be generated)
  4. when checking swagger documentation you should see a red alert in the top stating the following:
    Resolver error at paths./api/path.get.responses.200.schema.items.$ref Could not resolve reference because of: Could not resolve pointer: /definitions/path.to.struct.MyOwnStruct does not exist in document

Expected behavior
I should be able to see in the documentation the structure of MyOwnStruct as the case of a successful response.

Screenshots
Because it couldn't find the struct this is the result in the swagger documentation
image

Your swag version
v1.3.2

Desktop (please complete the following information):

  • OS: LinuxMint
  • Browser Chrome
  • Version 67.0.3396.62

Additional context
None

@pei0804
Copy link
Member

pei0804 commented Aug 5, 2018

Can you give me error source code example?
I want to know about the location of the struct and the execution place of swag init.

@jac1013
Copy link
Author

jac1013 commented Aug 6, 2018

Sure, so the exact locations is ./business/structures/MyOwnStruct and this is the code folder hierarchy (this is the root)

image

swag init runs successfully when I use business.structures.MyOwnStruct as data type but then throws the error I provided above when checking the swagger documentation. MyOwnStruct doesn't have nested Structs on it, it only uses primitive types. If I use string (instead of MyOwnStruct) as data type all works flawlessly.

The code is running from ./cmd/rest/main.go and the exact error we receive in the swagger alert is:
Resolver error at paths./example/example-path.get.responses.200.schema.items.$ref Could not resolve reference because of: Could not resolve pointer: /definitions/business.structures.MyOwnStruct does not exist in document

MyOwnStruct is just a bunch of primitive types with json definitions:

type MyOwnStruct struct {
	Cp float64 `json:"cp"`
	Op float64 `json:"op"`
	H float64 `json:"h"`
	L float64 `json:"l"`
	V float64 `json:"v"`
	Nt float64 `json:"nt"`
	Ot float64 `json:"ot"`
	Ct float64 `json:"ct"`
}

I hope this helps.

Thanks for the cool library :).

@pei0804
Copy link
Member

pei0804 commented Aug 7, 2018

I was able to reproduce. I'm looking for problems now.
https://github.com/pei0804/swag-sandbox

@dselans
Copy link
Contributor

dselans commented Aug 20, 2018

Do not reference the object as business.structures.MyOwnStruct but as structures.MyOwnStruct -- this means, you should import structures in the same file your doc comments live.

Something like:

var (
_ = structures.SomeStructure{}
)

.. would be enough to satisfy the importer.

@pei0804
Copy link
Member

pei0804 commented Mar 20, 2019

If You have problem reopen issue.

@pei0804 pei0804 closed this as completed Mar 20, 2019
@ZavilenskyIlya
Copy link

If You have problem reopen issue.

I have the same problem, do you have a solution?

@mark-jordanovic-lewis
Copy link

mark-jordanovic-lewis commented Jul 26, 2019

I cannot import any data types, stdlib, external, home rolled, or, defined in-line of the success or failure kwds using swag into my documentation. Is there a solution?

Go v1.9 -> 1.12

@ahoulgrave
Copy link

Having same issue with a type defined in the same package:

// @Success 200 {object} customResponse
type customResponse struct {
	Total    int              `json:"total" example:"100"`
	Pages    int              `json:"pages" example:"5"`
	Object []core.Object `json:"objects"`
}

@ubogdan
Copy link
Contributor

ubogdan commented Aug 5, 2019

This is invalid for swagger.
// @Success 200 {object} customResponse
the definition needs to be in fromat package.Type , no matter if it's on the same package.

@songxiaokui
Copy link

如果您有问题,请重新打开。

我有同样的问题,您有解决方案吗?

If You have problem reopen issue.

I have the same problem, do you have a solution?

I have the same problem, do you have a solution?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants