github.com/ffalor/go-swagger@v0.0.0-20231011000038-9f25265ac351/README.md (about) 1 # Swagger 2.0 [![Run CI](https://github.com/go-swagger/go-swagger/actions/workflows/test.yaml/badge.svg)](https://github.com/go-swagger/go-swagger/actions/workflows/test.yaml) [![codecov](https://codecov.io/gh/go-swagger/go-swagger/branch/master/graph/badge.svg)](https://codecov.io/gh/go-swagger/go-swagger) [![GitHub version](https://badge.fury.io/gh/go-swagger%2Fgo-swagger.svg)](https://badge.fury.io/gh/go-swagger%2Fgo-swagger) 2 3 [![Slack Status](https://slackin.goswagger.io/badge.svg)](https://slackin.goswagger.io) 4 [![license](http://img.shields.io/badge/license-Apache%20v2-orange.svg)](https://raw.githubusercontent.com/swagger-api/swagger-spec/master/LICENSE) 5 [![GoDoc](https://godoc.org/github.com/go-swagger/go-swagger?status.svg)](http://godoc.org/github.com/go-swagger/go-swagger) 6 [![Docker Repository on Quay](https://quay.io/repository/goswagger/swagger/status "Docker Repository on Quay")](https://quay.io/repository/goswagger/swagger) 7 [![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2Fgo-swagger%2Fgo-swagger.svg?type=shield)](https://app.fossa.io/projects/git%2Bgithub.com%2Fgo-swagger%2Fgo-swagger?ref=badge_shield) 8 [![GolangCI](https://golangci.com/badges/github.com/go-swagger/go-swagger.svg)](https://golangci.com) 9 [![Go Report Card](https://goreportcard.com/badge/github.com/go-swagger/go-swagger)](https://goreportcard.com/report/github.com/go-swagger/go-swagger) 10 11 This package contains a golang implementation of Swagger 2.0 (aka [OpenAPI 2.0](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md)): 12 it knows how to serialize and deserialize swagger specifications. 13 14 [Swagger](https://swagger.io/) is a simple yet powerful representation of your RESTful API.<br> 15 16 > ![swagger](https://raw.githubusercontent.com/go-swagger/go-swagger/master/docs/favicon-16x16.png) **Swagger in a nutshell** 17 > 18 > With the largest ecosystem of API tooling on the planet, thousands of developers are supporting Swagger in almost every modern programming language and deployment environment. 19 > 20 > With a Swagger-enabled API, you get interactive documentation, client SDK generation and discoverability. We created Swagger to help fulfill the promise of APIs. 21 > 22 > Swagger helps companies like Apigee, Getty Images, Intuit, LivingSocial, McKesson, Microsoft, Morningstar, and PayPal build the best possible services with RESTful APIs. Now in version 2.0, Swagger is more enabling than ever. And it's 100% open source software. 23 24 ## Features 25 `go-swagger` brings to the go community a complete suite of fully-featured, high-performance, API components to work with a Swagger API: server, client and data model. 26 27 * Generates a server from a swagger specification 28 * Generates a client from a swagger specification 29 * Generates a CLI (command line tool) from a swagger specification (alpha stage) 30 * Supports most features offered by jsonschema and swagger, including polymorphism 31 * Generates a swagger specification from annotated go code 32 * Additional tools to work with a swagger spec 33 * Great customization features, with vendor extensions and customizable templates 34 35 Our focus with code generation is to produce idiomatic, fast go code, which plays nice with golint, go vet etc. 36 37 ## Project status 38 `go-swagger` is now feature complete and has stabilized its API. 39 40 Most features and building blocks are now in a stable state, with a rich set of CI tests. 41 42 The go-openapi community actively continues bringing fixes and enhancements to this code base. 43 44 There is still much room for improvement: contributors and PR's are welcome. You may also get in touch with maintainers on [our slack channel](https://slackin.goswagger.io). 45 46 ## Documentation 47 <https://goswagger.io> 48 49 ## FAQ 50 Q&A contributed by the community: 51 52 <https://goswagger.io/faq/> 53 54 ## How is this different from go generator in swagger-codegen? 55 **tl;dr** The main difference at this moment is that this one actually works... 56 57 The swagger-codegen project only generates a _workable_ go client and even there it will only support flat models. 58 Further, the go server generated by swagger-codegen is mostly a stub. 59 60 > **Motivation** 61 > Why is this not done as a part of the swagger-codegen project? Because: 62 > 63 > * I don't really know java very well and so I'd be learning both java and the object model of the codegen which was in heavy flux as opposed to doing go and I really wanted to go experience of designing a large codebase with it. 64 > * Go's super limited type system makes it so that it doesn't fit well in the model of swagger-codegen 65 > * Go's idea of polymorphism doesn't reconcile very well with a solution designed for languages that actually have inheritance and so forth. 66 > * For supporting types like `[][][]map[string][][]int64` I don't think it's possible with mustache 67 > 68 > I gravely underestimated the amount of work that would be involved in making something useful out of it. 69 > My personal mission: I want the jvm to go away, it was great way back when now it's just silly (vm in container on vm in vm in container) 70 71 ## What's inside? 72 73 Here is an outline of available features (see the full list [here](https://goswagger.io/features.html)): 74 75 - An object model that serializes swagger-compliant yaml or json 76 - A tool to work with swagger 77 - Serve swagger UI for any swagger spec file 78 - Flexible code generation, with customizable templates 79 - Generate go API server based on swagger spec 80 - Generate go API client from a swagger spec 81 - Validate a swagger spec document, with extra rules outlined [here](https://github.com/apigee-127/sway/blob/master/docs/README.md#semantic-validation) 82 - Generate a spec document based on annotated code 83 - A runtime to work with Rest API and middlewares 84 - Serve spec 85 - Routing 86 - Validation 87 - Authorization 88 - Swagger docs UI 89 - A Diff tool which will cause a build to fail if a change in the spec breaks backwards compatibility 90 91 There is more to that... 92 93 - A [typed JSON Schema implementation](https://goswagger.io/use/model.html), supporting most Draft 4 features 94 - Extended string and numeric formats: [strfmt](https://github.com/go-openapi/strfmt) 95 - Utilities to work with JSON, convert data types and pointers: [swag](https://github.com/go-openapi/swag) 96 - A jsonschema (Draft 4) validator, with full $ref support: [validate](https://github.com/go-openapi/validate) 97 - Custom validation interface 98 99 ## Installing 100 `go-swagger` is available as binary or docker releases as well as from source: [more details](https://goswagger.io/install.html). 101 102 ## Use-cases 103 The main package of the toolkit, go-swagger/go-swagger, provides command line tools to help working with swagger. 104 105 The toolkit is highly customizable and allows endless possibilities to work with OpenAPI2.0 specifications. 106 107 Beside the go-swagger CLI tool and generator, the [go-openapi packages](https://github.com/go-openapi) provide modular functionality to build custom solutions on top of OpenAPI. 108 109 The CLI supports shell autocompletion utilities: see [here](https://goswagger.io/cli_helpers.html). 110 111 ### Serve specification UI 112 Most basic use-case: serve a UI for your spec: 113 114 ``` 115 swagger serve https://raw.githubusercontent.com/swagger-api/swagger-spec/master/examples/v2.0/json/petstore-expanded.json 116 ``` 117 118 ### Validate a specification 119 To [validate](https://goswagger.io/usage/validate.html) a Swagger specification: 120 121 ``` 122 swagger validate https://raw.githubusercontent.com/swagger-api/swagger-spec/master/examples/v2.0/json/petstore-expanded.json 123 ``` 124 125 ### Generate an API server 126 To generate a [server for a swagger spec](https://goswagger.io/generate/server.html) document: 127 128 ``` 129 swagger generate server [-f ./swagger.json] -A [application-name [--principal [principal-name]] 130 ``` 131 132 ### Generate an API client 133 To generate a [client for a swagger spec](https://goswagger.io/generate/client.html) document: 134 135 ``` 136 swagger generate client [-f ./swagger.json] -A [application-name [--principal [principal-name]] 137 ``` 138 ### Generate an CLI (Command line tool) 139 To generate a [CLI for a swagger spec](https://github.com/go-swagger/go-swagger/tree/master/examples/cli) document: 140 ``` 141 swagger generate cli [-f ./swagger.json] -A [application-name [--principal [principal-name]] 142 ``` 143 ### Generate a spec from source 144 To generate a [swagger spec document for a go application](https://goswagger.io/generate/spec.html): 145 146 ``` 147 swagger generate spec -o ./swagger.json 148 ``` 149 150 ### Generate a data model 151 To generate model structures and validators exposed by the API: 152 153 ``` 154 swagger generate model --spec={spec} 155 ``` 156 157 ### Transform specs 158 159 There are [several commands](https://goswagger.io/use/transform.html) allowing you to transform your spec. 160 161 Resolve and expand $ref's in your spec as inline definitions: 162 ``` 163 swagger expand {spec} 164 ``` 165 166 Flatten your spec: all external $ref's are imported into the main document and inline schemas reorganized as definitions. 167 ``` 168 swagger flatten {spec} 169 ``` 170 171 Merge specifications (composition): 172 ``` 173 swagger mixin {spec1} {spec2} 174 ``` 175 176 ### Compare specs 177 178 The diff command allows you to check backwards compatibility. 179 Type ```swagger diff --help``` for info. 180 181 ``` 182 swagger diff {spec1} {spec2} 183 ``` 184 185 ### Generate spec markdown spec 186 187 ``` 188 swagger generate markdown -f {spec} --output swagger.mode 189 ``` 190 191 ## Try it 192 193 Try `go-swagger` in a free online workspace using Gitpod: 194 195 [![Open in Gitpod](https://gitpod.io/button/open-in-gitpod.svg)](https://gitpod.io#https://github.com/go-swagger/go-swagger) 196 197 ## Licensing 198 199 The toolkit itself is licensed as Apache Software License 2.0. Just like swagger, this does not cover code generated by the toolkit. That code is entirely yours to license however you see fit. 200 201 202 [![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2Fgo-swagger%2Fgo-swagger.svg?type=large)](https://app.fossa.io/projects/git%2Bgithub.com%2Fgo-swagger%2Fgo-swagger?ref=badge_large) 203 204 ## Who is using this project? 205 206 To name but a few... (feel free to sign in there if you are using this project): 207 208 > In the list below, we tried to figure out the public repos where you'll find examples on how to use `go-swagger` and `go-openapi`: 209 210 [3DSIM](https://github.com/3DSIM) 211 [Alibaba PouchAPI](https://github.com/alibaba/pouch) 212 [CheckR](https://github.com/checkr/flagr) 213 [Cilium](https://github.com/cilium/cilium) 214 [CoreOS](https://github.com/coreos/go-quay) 215 [NetBox Community](https://github.com/netbox-community/go-netbox) 216 [EVE Central](https://github.com/evecentral) 217 Iron.io 218 [JaegerTracing](https://github.com/jaegertracing/jaeger) 219 [Kubernetes-Helm](https://github.com/kubernetes-helm/monocular) 220 [Kubernetes](https://godoc.org/k8s.io/apiextensions-apiserver/pkg/apiserver) 221 [ManifoldCo](https://github.com/manifoldco) 222 [Metaparticle.io](https://github.com/metaparticle-io/metaparticle-ast) 223 [Netlify](https://github.com/netlify/open-api) 224 [Nutanix](https://github.com/nutanix) 225 [OAS2](https://github.com/hypnoglow/oas2) 226 [OVH API](https://github.com/appscode/go-ovh) 227 [RackHD](https://github.com/RackHD/RackHD) 228 [ScaleFT](https://github.com/authclub/billforward) 229 [StratoScale](https://github.com/Stratoscale/swagger) 230 [Terraform Provider OpenAPI](https://github.com/dikhan/terraform-provider-openapi) 231 [VMware](https://github.com/vmware/dispatch) 232 [Sunbird RC](https://github.com/Sunbird-RC/sunbird-rc-core/tree/main/services/notification-service) 233 [DIVOC](https://github.com/egovernments/DIVOC/tree/main/backend/portal_api) 234 ... 235 236 ## Note to users migrating from older releases 237 238 ### Migrating from 0.25 to [master] 239 240 Changes in the behavior of the generated client regarding defaults in parameters and response headers: 241 242 * default values for parameters are no more hydrated by default and sent over the wire 243 (assuming the server uses defaults). 244 * the previous behavior (explicitly sending defaults over the wire) can be obtained 245 with the SetDefaults() and WithDefaults() parameter methods. 246 * the body parameter is not pre-hydrated with the default from it schema 247 * default values for response headers are hydrated when the header is not received 248 (previously, headers remained with their zero value) 249 250 ### Migrating from 0.24 to 0.25 251 252 The options for `generate model --all-definitions` and `--skip-struct` are marked for deprecation. 253 254 For now, the CLI continues to accept these options. They will be removed in a future version. 255 256 Generating all definitions is now the default behavior when no other option filters the generation scope. 257 The `--skip-struct` option had no effect. 258 259 ### Migrating from 0.14 to 0.15 260 261 Generated servers no more import the following package (replaced by go1.8 native functionality): 262 ``` 263 github.com/tylerb/graceful 264 ``` 265 266 Spec flattening now defaults to minimal changes to models and should be workable for 0.12 users. 267 268 Users who prefer to stick to 0.13 and 0.14 default flattening mode may now use the `--with-flatten=full` option. 269 270 Note that the `--skip-flatten` option has been phased out and replaced by the more explicit `--with-expand` option. 271 272 ### Migrating from 0.12 to 0.13 273 274 Spec flattening and $ref resolution brought breaking changes in model generation, since all complex things generate their own definitions. 275 276 ### Migrating from 0.5.0 to 0.6.0 277 278 You will have to rename some imports: 279 280 ``` 281 github.com/go-swagger/go-swagger/httpkit/validate to github.com/go-openapi/validate 282 github.com/go-swagger/go-swagger/httpkit to github.com/go-openapi/runtime 283 github.com/naoina/denco to github.com/go-openapi/runtime/middleware/denco 284 github.com/go-swagger/go-swagger to github.com/go-openapi 285 ``` 286 287 ### Using 0.5.0 288 289 Because 0.5.0 and master have diverged significantly, you should checkout the tag 0.5.0 for go-swagger when you use the currently released version.