github.com/go-swagger/go-swagger@v0.31.0/docs/generate-spec/spec.md (about)

     1  ---
     2  title: Generating a swagger spec
     3  date: 2023-01-01T01:01:01-08:00
     4  draft: true
     5  weight: 10
     6  ---
     7  # Spec generation
     8  
     9  The toolkit has a command that will let you generate a swagger spec document from your code.
    10  The command integrates with go doc comments, and makes use of structs when it needs to know of
    11  types.
    12  
    13  Based on the work from https://github.com/yvasiyarov/swagger.
    14  
    15  It uses a similar approach but with expanded annotations and it produces a swagger 2.0 spec.
    16  
    17  The goal of the syntax is to make it look as a natural part of the documentation for the application code.
    18  
    19  The generator is passed a main package and it uses that to discover all the code in use.
    20  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.
    21  
    22  Once the parser has encountered a comment that matches one of its known tags, the parser will assume that the rest of the comment block is for swagger.
    23  
    24  If it is your first time using this library for generating swagger 
    25  specification, you can also take a look at 
    26  [this getting started guide](https://medium.com/@pedram.esmaeeli/generate-swagger-specification-from-go-source-code-648615f7b9d9?source=friends_link&sk=b402acc563e8d2bfadd1ac02abddc3bb)
    27  on Medium to get the big picture and then return here to read more 
    28  about all annotations and commands provided by this package.
    29  
    30  ## Usage
    31  
    32  To generate a swagger spec:
    33  
    34  ```sh
    35  swagger generate spec -o ./swagger.json
    36  ```
    37  
    38  You give it a main file and it will parse all the files that are reachable by that main
    39  package to produce a swagger specification.
    40  
    41  You may add a go:generate comment to your main file for example:
    42  
    43  ```sh
    44  //go:generate swagger generate spec -o swagger.json
    45  ```
    46  
    47  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.
    48  This means that for something to be discoverable it needs to be reachable by a code path triggered through the main package.
    49  
    50  If an annotation is not yet supported or you want to merge with a pre-existing spec, you can use the -i parameter.
    51  
    52  ```sh
    53  swagger generate spec -i ./swagger.yml -o ./swagger.json
    54  ```
    55  
    56  The idea is that there are certain things that are more easily expressed by just using yaml
    57  
    58  To generate spec in yaml format, just name the output file with ".yml" or ".yaml" extension. For example:
    59  
    60  ```sh
    61  swagger generate spec -o ./swagger.yml
    62  ```
    63  
    64  If you don't want to generate Go language specific extensions in the spec file, you can disable them by doing
    65  
    66  ```sh
    67  SWAGGER_GENERATE_EXTENSION=false && swagger generate spec -o ./swagger.yml
    68  ```
    69  
    70  ## Parsing rules
    71  
    72  {{< hint warning >}}
    73  This command relies heavily on the way godoc works.
    74  
    75  This means you should be very aware of all the things godoc supports.
    76  {{< /hint >}}
    77  
    78  
    79  * [godoc documenting go code](http://blog.golang.org/godoc-documenting-go-code)
    80  * [godoc ToHTML](https://golang.org/pkg/go/doc/#ToHTML)
    81  * [commenting go effectively](https://golang.org/doc/effective_go.html#commentary)
    82  * [godoc documentation](https://godoc.org/golang.org/x/tools/cmd/godoc)
    83  
    84  Single page which documents all the currently supported godoc rules:
    85  
    86  * [godoc tricks](https://godoc.org/github.com/fluhus/godoc-tricks)
    87  
    88  The generated code tries to avoid golint errors.
    89  
    90  * [go lint](https://github.com/golang/lint)
    91  * [go lint style guide](https://github.com/golang/go/wiki/CodeReviewComments)
    92  
    93  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
    94  comment block will become the title, or a header when rendered as godoc. The rest of the comment block will be treated
    95  as description up to either the end of the comment block, or a line that starts with a known annotation.
    96  
    97  #### Annotation syntax
    98  
    99  If you want to exclude something from the spec generation process you can try with the struct tag: `json:"-"`
   100  
   101  There are several annotations that mark a comment block as a participant for the swagger spec.
   102  
   103  - [swagger:meta](../reference/annotations/meta.md)
   104  - [swagger:route](../reference/annotations/route.md)
   105  - [swagger:parameters](../reference/annotations/params.md)
   106  - [swagger:response](../reference/annotations/response.md)
   107  - [swagger:operation](../reference/annotations/operation.md)
   108  - [swagger:model](../reference/annotations/model.md)
   109  - [swagger:allOf](../reference/annotations/allOf.md)
   110  - [swagger:strfmt](../reference/annotations/strfmt.md)
   111  - [swagger:discriminated](../reference/annotations/discriminated.md)
   112  - [swagger:ignore](../reference/annotations/ignore.md)
   113  
   114  #### Embedded types
   115  
   116  For the embedded schemas there are a set of rules for the spec generator to vary the definition it generates.
   117  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
   118  `x-class` extension to reliably build a serializer for a type with a discriminator
   119  
   120  #### Known vendor extensions
   121  
   122  There are a couple of commonly used vendor extensions that most frameworks support to add functionality to the swagger spec.
   123  
   124  For generating a swagger specification document this toolkit supports:
   125  
   126  Vendor extension | Description