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

Automatic Generation of Swagger Documentation in Types #10933

Merged
merged 4 commits into from
Aug 24, 2015
Merged

Automatic Generation of Swagger Documentation in Types #10933

merged 4 commits into from
Aug 24, 2015

Conversation

andronat
Copy link
Contributor

@andronat andronat commented Jul 8, 2015

This is a proposal on how we can handle and export our documentation from api/*/types.go to Swagger API. Refs #3060

The basic idea

  1. Export a description for the structs themselves
  2. We have a lot of interesting information as comments in api/types.go, api/v1/types.go, api/v1beta3/types.go that we might want to export in Swagger API.
  3. We make sure that we don't want to maintain documentation in multiple places.

Problems

Ideally we would like go-restful to be able to parse our source files and export struct and fields comments in the Swagger API. Unfortunately this is not possible (or just very hard) as Swagger's JSON is builded dynamically on runtime and parsing the source files requires 1) having the files in the deployment environment 2) pass the file paths to go-restful.

Currently go-restful is able to export field descriptions of a struct by parsing struct tags (using the description tag) and lately, struct description is also supported through the modelDescription tag. The issue with this method is how can we maintain a source-reader-friendly documentation in our files and keep swagger-friendly tags up to date (these are 1 line long descriptions).

Solution

In this PR: emicklei/go-restful#215 we suggest an alternative way of keeping struct documentation through special methods attached on structs.

This PR is about creating a generator that parses the documentation from the Type files (this is done by using go/doc and go/ast) and automatically generates the aforementioned special documentation methods for each struct.

A smaller issue on TODOs and various other comments

We want this parser to ignore TODO lists and various other comments in our source code.

The suggested solution is:

  1. Ignore one line TODO comments
  2. Use --- in a line and everything after it will be not placed in swagger descriptions

TODO:

  1. wait for Models can generate documentation through a struct method emicklei/go-restful#215 to be merged
  2. ignore TODOs (and maybe also other information) -> Use --- and ignore everything after it
  3. polish output (keep paragraphs, keep indentation, keep newline chars in indented lines)
  4. ignore json",inline" tags
  5. Add a comment here that thoroughly explains what this file is, what it's for, how it's generated, etc
  6. Generator should not print keys for empty documentation (empty values overwrite struct tags)
  7. Refactor verify-descriptions
  8. Fixed missing ptr description emicklei/go-restful#219 needs merging
  9. fix description parsing on recursion emicklei/go-restful#222 needs merging
  10. change QueryParameter description in api_installer.go
  11. Move all description tags into comments
  12. Depends on updated go-restful #11406

@k8s-bot
Copy link

k8s-bot commented Jul 8, 2015

GCE e2e build/test passed for commit 36055bd3287b31233611820cb572e9817d6716f8.


package api

// AUTO-GENERATED FUNCTIONS START HERE
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a comment here that thoroughly explains what this file is, what it's for, how it's generated, etc.

@k8s-bot
Copy link

k8s-bot commented Jul 9, 2015

GCE e2e build/test passed for commit afb8f124a8a8c2e1cb723ea0100ea5649f74d333.

return map[string]string{
"": "",
"name": "Each container in a pod must have a unique name.",
"state": "TODO(dchen1107): Should we rename PodStatus to a more generic name or have a separate states defined for container?",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We will need a way to keep these TODOs out of here :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, I have it already in my todo list on my PR. Working on it

@nikhiljindal
Copy link
Contributor

This is exciting.
@thockin is going to be very happy to see this.
We have discussed having such a pass over comments in types.go, before they are passed to go-restful.

@ghodss
Copy link
Contributor

ghodss commented Jul 9, 2015

@bgrant0607 This is the method @andronat and I came up with to retrieve API documentation out of the comments and make it available to swagger, as per our earlier convo.

@k8s-bot
Copy link

k8s-bot commented Jul 10, 2015

GCE e2e build/test passed for commit 6f3d3828f62c0b4e37b9759006dddcdbf165ef9a.

@bgrant0607
Copy link
Member

I am opposed to any solution that moves documentation out of our interface-definition files, currently api/*/types.go.

@andronat
Copy link
Contributor Author

@bgrant0607 it doesn't move any documentation from api/*/types.go. On the contrary we found a way to keep the documentation only in the comments of the api/*/types.go files and not comments + struct tags that currently is.

@ghodss
Copy link
Contributor

ghodss commented Jul 14, 2015

@bgrant0607 It's clear that this PR does not move documentation out of the current interface-definition files, which is the express purpose of this design, yes?

@bgrant0607
Copy link
Member

Sorry, I don't have time to look at this in detail right now.

How would this support additional properties, such as those in the following PR? Or would those remain in tags?
emicklei/go-restful#198

@andronat
Copy link
Contributor Author

The current PR is only for the descriptions. Most definitely, we could make the appropriate changes to support also additional properties but I am not sure if we really want to do that.

The reason that makes me reluctant for something like this (supporting model attributes as comments) is that when a user/developer reads the source code he/she expects to see a description as a comment but "functional properties" as part of the code (e.g. max number, min number etc). I don't know if that makes any sense to you also :/ (I don't have a strong opinion though on that)

@thockin
Copy link
Member

thockin commented Jul 15, 2015

I like the idea. I loathe the triple-line-wrapped description tags - I think they discourage good descriptions. Moving all of that into comments is a WAY better answer. Let's define a set of standard (for us) comment structures and enforce that every field and every struct in our API is documented fully.

@k8s-bot
Copy link

k8s-bot commented Jul 15, 2015

GCE e2e build/test failed for commit afff8f4cb82b00c555a42999c4bc3c91e9020bf7.

@k8s-bot
Copy link

k8s-bot commented Jul 15, 2015

GCE e2e build/test passed for commit 9823d915f4116e11e8b581c9636bb3228781a353.

@andronat andronat mentioned this pull request Jul 16, 2015
@k8s-bot
Copy link

k8s-bot commented Jul 18, 2015

GCE e2e build/test passed for commit 443527908004385f71e4b8a4785ae87f5ef3fbd9.

@k8s-bot
Copy link

k8s-bot commented Jul 19, 2015

GCE e2e build/test passed for commit eef905a1b6493111ae099473dde9ef5c8d674cd5.

@andronat
Copy link
Contributor Author

Can someone please take a look on that f51274ed591906371cee4698747aca2f26b7bfc7. I am not 100% sure that I am doing it correctly and it also takes a lot of time. Some input would be appreciated.

/cc @ghodss @bgrant0607 @thockin

@bgrant0607
Copy link
Member

Have you thought about how to implement verify-descriptions?

@andronat
Copy link
Contributor Author

Hmm, I missed that part.

How about using my generator's code to find if there are missing docs from fields and structs? The generator is parsing the AST of the source code and returns pairs of (field, doc). I can count the number of returned pairs with the number of fields and structs and return OK if none is missing.

@bgrant0607
Copy link
Member

It would need to report which fields lack documentation

@andronat
Copy link
Contributor Author

How about this?

Also, the command exits with the number of the missing doc definitions.

Missing documentation for the struct itself: ComponentStatusList
In struct: ComponentStatusList, field documentation is missing: metadata
In struct: ComponentStatusList, field documentation is missing: items
Missing documentation for the struct itself: ConditionStatus
In struct: Container, field documentation is missing: ports
In struct: Container, field documentation is missing: env

# echo $?
6

source "${KUBE_ROOT}/hack/lib/init.sh"

kube::golang::setup_env
"${KUBE_ROOT}/hack/build-go.sh" cmd/genswaggertypedocs
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similarly could you split this script as well? Thank you.

@andronat
Copy link
Contributor Author

fixed omitempty. working on other issues.

@k8s-bot
Copy link

k8s-bot commented Aug 21, 2015

GCE e2e build/test passed for commit 6ed3ee3d11f178da1c1ff0f630127451e7758e3d.

@k8s-bot
Copy link

k8s-bot commented Aug 21, 2015

GCE e2e build/test failed for commit 9cb4c486b2aa8e2a04945810c85d3fae6e12a4d5.

@caesarxuchao
Copy link
Member

Thank you @andronat. I don't have extra comments. Ping me when you address all of the comments.

@andronat
Copy link
Contributor Author

ok just done with the after-build scripts

@andronat
Copy link
Contributor Author

Tell me @caesarxuchao when to start squashing

@bgrant0607
Copy link
Member

Go ahead and squash, @andronat. It's already not easy to determine what you changed relative to the previous push.

@andronat
Copy link
Contributor Author

Yea sorry for that. I should had put everything as a new commit at the end :/

@k8s-bot
Copy link

k8s-bot commented Aug 22, 2015

GCE e2e build/test failed for commit 04ed44abad7da107448e87e46a05e92a8029e6cd.

@caesarxuchao
Copy link
Member

@andronat, sorry just saw your message. Please add back the "omitempty" for nodeport. #10933 (comment)

@k8s-bot
Copy link

k8s-bot commented Aug 22, 2015

GCE e2e build/test passed for commit a44b3b5d961efca18d45720e52e9254efb46d479.

@andronat
Copy link
Contributor Author

found the problem: in test-cmd.sh we are grep-ing for sentences in the old documentation. fixing it right now. (also fixed the omitempty)

@k8s-bot
Copy link

k8s-bot commented Aug 22, 2015

GCE e2e build/test passed for commit 1074784.

@andronat
Copy link
Contributor Author

It is all green!

@bgrant0607 bgrant0607 added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Aug 22, 2015
@bgrant0607
Copy link
Member

Thanks for all the work on this. LGTM.

@caesarxuchao
Copy link
Member

Thank you!

@andronat
Copy link
Contributor Author

Thank you too for your time and effort!

nikhiljindal added a commit that referenced this pull request Aug 24, 2015
Automatic Generation of Swagger Documentation in Types
@nikhiljindal nikhiljindal merged commit a09425d into kubernetes:master Aug 24, 2015
@andronat andronat deleted the swagger_auto_type_docs branch August 25, 2015 00:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lgtm "Looks good to me", indicates that a PR is ready to be merged.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants