github.com/kaisawind/go-swagger@v0.19.0/docs/use/spec.md (about)

     1  # swagger.json generation
     2  
     3  The toolkit has a command that will let you generate a swagger spec document from your code.
     4  The command integrates with go doc comments, and makes use of structs when it needs to know of
     5  types.
     6  
     7  Based on the work from https://github.com/yvasiyarov/swagger.
     8  
     9  It uses a similar approach but with expanded annotations and it produces a swagger 2.0 spec.
    10  
    11  The goal of the syntax is to make it look as a natural part of the documentation for the application code.
    12  
    13  The generator is passed a main package and it uses that to discover all the code in use.
    14  To do this it makes use of go's loader package. The same package that is used by tools like goimports to discover which files to format.
    15  
    16  Once the parser has encountered a comment that matches on of its known tags, the parser will assume that the rest of the comment block is for swagger.
    17  
    18  ### Usage
    19  
    20  To generate a spec:
    21  
    22  ```
    23  swagger generate spec -o ./swagger.json
    24  ```
    25  
    26  See the full list of available options [here](../generate/spec.md).
    27  
    28  You give it a main file and it will parse all the files that are reachable by that main
    29  package to produce a swagger specification.
    30  
    31  To use you can add a go:generate comment to your main file for example:
    32  
    33  ```
    34  //go:generate swagger generate spec
    35  ```
    36  
    37  The command requires a main package or file and it wants your code to compile. It uses the go tools loader to load an application and then scans all the packages that are in use by the code base.
    38  This means that for something to be discoverable it needs to be reachable by a code path triggered through the main package.
    39  
    40  If an annotation is not yet supported or you want to merge with a pre-existing spec, you can use the -i parameter.
    41  
    42  ```
    43  swagger generate spec -i ./swagger.yml -o ./swagger.json
    44  ```
    45  
    46  The idea is that there are certain things that are more easily expressed by just using yaml
    47  
    48  To generate spec in yaml format, just name the output file with ".yml" or ".yaml" extension. For example:
    49  
    50  ```
    51  swagger generate spec -o ./swagger.yml
    52  ```
    53  
    54  If you don't want to generate Go language specific extensions in the spec file, you can disable them by doing
    55  
    56  ```
    57  SWAGGER_GENERATE_EXTENSION=false && swagger generate spec -o ./swagger.yml
    58  ```
    59  
    60  #### Parsing rules
    61  
    62  ![warning](../warning.png)This command relies heavily on the way godoc works.
    63  
    64  ![warning](../warning.png)This means you should be very aware of all the things godoc supports.
    65  
    66  * [godoc documenting go code](http://blog.golang.org/godoc-documenting-go-code)
    67  * [godoc ToHTML](https://golang.org/pkg/go/doc/#ToHTML)
    68  * [commenting go effectively](https://golang.org/doc/effective_go.html#commentary)
    69  * [godoc documentation](https://godoc.org/golang.org/x/tools/cmd/godoc)
    70  
    71  Single page which documents all the currently supported godoc rules:
    72  
    73  * [godoc tricks](https://godoc.org/github.com/fluhus/godoc-tricks)
    74  
    75  The generated code tries to avoid golint errors.
    76  
    77  * [go lint](https://github.com/golang/lint)
    78  * [go lint style guide](https://github.com/golang/go/wiki/CodeReviewComments)
    79  
    80  When an object has a title and a description field, it will use the go rules to parse those. So the first line of the
    81  comment block will become the title, or a header when rendered as godoc. The rest of the comment block will be treated
    82  as description up to either the end of the comment block, or a line that starts with a known annotation.
    83  
    84  #### Annotation syntax
    85  
    86  If you want to exclude something from the spec generation process you can try with the struct tag: `json:"-"`
    87  
    88  There are several annotations that mark a comment block as a participant for the swagger spec.
    89  
    90  - [swagger:meta](../use/spec/meta.md)
    91  - [swagger:route](../use/spec/route.md)
    92  - [swagger:parameters](../use/spec/params.md)
    93  - [swagger:response](../use/spec/response.md)
    94  - [swagger:operation](../use/spec/operation.md)
    95  - [swagger:model](../use/spec/model.md)
    96  - [swagger:allOf](../use/spec/allOf.md)
    97  - [swagger:strfmt](../use/spec/strfmt.md)
    98  - [swagger:discriminated](../use/spec/discriminated.md)
    99  - [swagger:ignore](../use/spec/ignore.md)
   100  
   101  #### Embedded types
   102  
   103  For the embedded schemas there are a set of rules for the spec generator to vary the definition it generates.
   104  When an embedded type isn't decorated with the `swagger:allOf` annotation, then the properties from the embedded value will be included in the generated definition as if they were defined on the definition. But when the embedded type is decorated with the `swagger:allOf` annotation then the all of element will be defined as a "$ref" property instead. For an annotated type there is also the possibility to specify an argument, the value of this argument will be used as the value for the `x-class` extension. This allows for generators that support the
   105  `x-class` extension to reliably build a serializer for a type with a discriminator
   106  
   107  #### Known vendor extensions
   108  
   109  There are a couple of commonly used vendor extensions that most frameworks support to add functionality to the swagger spec.
   110  
   111  For generating a swagger specification document this toolkit supports:
   112  
   113  Vendor extension | Description
   114  -----------------|-------------
   115  `x-isnullable`   | makes a property value nullable, for go code that means a pointer
   116  `x-nullable`     | makes a property value nullable, for go code that means a pointer
   117  `x-go-name`      | the go name of a type
   118  `x-go-package`   | the go package of a type
   119  `x-class`        | this is used in conjunction with discriminators to give a full type name
   120  `x-omitempty`    | this is used to control presence of omitempty tag to be used by JSON Marshaler. if the field is required, this extension doesn't take effect