Open jancona opened 4 years ago
I've refactored a new branch 'refactored', try it please.
Thanks for taking a look! I tried the refactored
branch. I still have to add dummy @param
or @failure
declarations in order to generate definitions for the nested types. I'll review the code again to see if I can figure out how to implement this.
My output is
"definitions": {
"model.MyPayload": {
"type": "object",
"properties": {
"map1": {
"type": "object",
"additionalProperties": {
"type": "string"
}
},
"map2": {
"type": "object",
"additionalProperties": {
"type": "string"
}
},
"name": {
"type": "string"
},
"struct1": {
"$ref": "#/definitions/model.MyStruct"
}
}
},
"model.MyStruct": {
"type": "object",
"properties": {
"map1": {
"type": "object",
"additionalProperties": {
"type": "string"
}
},
"map2": {
"type": "object",
"additionalProperties": {
"type": "string"
}
}
}
}
}
That's what I'm looking for. but when I run it, I get:
"definitions": {
"model.MyPayload": {
"type": "object",
"properties": {
"map1": {
"type": "object",
"additionalProperties": {
"type": "string"
}
},
"name": {
"type": "string"
}
}
}
}
I have checked out the refactored
branch, and built by running go build
in swag/cmd/swag
. Is that correct?
jim@jim-mbp:~/jimprojects/swag/cmd/swag$ git status
On branch refactored
Your branch is up to date with 'origin/refactored'.
nothing to commit, working tree clean
jim@jim-mbp:~/jimprojects/swag/cmd/swag$ go build
jim@jim-mbp:~/jimprojects/swag/cmd/swag$ ls -l
total 27208
-rw-r--r-- 1 jim staff 2813 Apr 17 09:47 main.go
-rwxr-xr-x 1 jim staff 13922572 Apr 19 11:53 swag
jim@jim-mbp:~/jimprojects/swag/cmd/swag$
This is what I have in my server.go file:
// Dummy is a dummy endpoint
// @summary dummy
// @router /dummy [get]
// @success 200 {object} model.MyPayload
func (app App) Dummy(w http.ResponseWriter, req *http.Request) {}
I have checked out the
refactored
branch, and built by runninggo build
inswag/cmd/swag
. Is that correct?
Steps are correct. The last step: swag init
Any errror logs ?
Here's the output:
$ /Users/jim/jimprojects/swag/cmd/swag/swag init --generalInfo server.go
2020/04/19 12:08:18 Generate swagger docs....
2020/04/19 12:08:18 Generate general API Info, search dir:./
2020/04/19 12:08:19 Generating model.MyPayload
2020/04/19 12:08:19 create docs.go at docs/docs.go
2020/04/19 12:08:19 create swagger.json at docs/swagger.json
2020/04/19 12:08:19 create swagger.yaml at docs/swagger.yaml
I can add debug Printf statements if you'll tell me where to put them.
If I change my @success
line to:
// @success 200 {object} model.MyPayload{struct1=model.MyStruct{map2=map[string]model.MyString}}
I get this output:
"definitions": {
"model.MyPayload": {
"type": "object",
"properties": {
"map1": {
"type": "object",
"additionalProperties": {
"type": "string"
}
},
"map2": {
"type": "object",
"additionalProperties": {
"$ref": "#/definitions/model.MyString"
}
},
"name": {
"type": "string"
},
"struct1": {
"$ref": "#/definitions/model.MyStruct"
}
}
},
"model.MyString": {
"type": "string"
},
"model.MyStruct": {
"type": "object",
"properties": {
"map1": {
"type": "object",
"additionalProperties": {
"type": "string"
}
},
"map2": {
"type": "object",
"additionalProperties": {
"$ref": "#/definitions/model.MyString"
}
}
}
}
}
I'm running it from within my project directory. I renamed my program to main.go and ran go install
to put my built version of swag in the PATH. The output is the same:
jim@jim-mbp:~/jimprojects/ourroots/server$ swag init
2020/04/19 21:23:20 Generate swagger docs....
2020/04/19 21:23:20 Generate general API Info, search dir:./
2020/04/19 21:23:22 Generating model.MyPayload
2020/04/19 21:23:22 create docs.go at docs/docs.go
2020/04/19 21:23:22 create swagger.json at docs/swagger.json
2020/04/19 21:23:22 create swagger.yaml at docs/swagger.yaml
I use go 1.13.5, works well. What your go version?
I'm using 1.14. Are you sure all your changes are pushed to the refactored
branch?
I'm using 1.14. Are you sure all your changes are pushed to the
refactored
branch?
sure. I pulled from github just right now and still worked well on CentOS.
I installed go 1.13.5 and got the same results.
I installed go 1.13.5 and got the same results.
Seems unbelievable. Try debuging it.
Can you point me to the places in the code where I should be looking? Where does it expand the additional types?
Can you point me to the places in the code where I should be looking? Where does it expand the additional types?
Okay, I figured it out. I was running swag from within the directory containing my server program, which is a subdirectory of the project directory. So parser.getAllGoFileInfo
didn't find the definitions in another package. When I run it from the project directory or with the proper parameters everything works! Thanks for your help!
Should I leave this open until you merge your refactored branch?
Should I leave this open until you merge your refactored branch?
I'll add log prompt for missing type definition in refactored branch
prompt still need to be optimized in master branch
Is there any progress on this? Any ETA on when it will be merged and released? :)
Is your feature request related to a problem? Please describe. Currently if I define a model struct using nested types like this:
When i use it in my service, it generates Swagger like this:
It doesn't generate definitions for
model.MyString
ormodel.MyStruct
.Describe the solution you'd like I'd like Swag to generate definitions for all types referred to by types in my model.
Describe alternatives you've considered
swaggertype
, but it doesn't support non-primitive types.model.MyString
andmodel.MyStruct
in dummy@param
declarations, the proper model definitions are generated.Additional context It looks like calling
operation.registerSchemaType
on all the types referred to by my model types would fix the issue. But I couldn't figure out where to make those calls.