Davide Bianchi 651f2d0478 Merge pull request #25 from davidebianchi/dependabot/go_modules/github.com/getkin/kin-openapi-0.53.0
chore(deps): bump github.com/getkin/kin-openapi from 0.48.0 to 0.53.0
2021-04-03 18:57:28 +02:00
2020-10-11 18:56:38 +02:00
2021-02-07 17:26:21 +01:00
2020-10-11 18:46:44 +02:00
2021-02-07 18:19:23 +01:00
2020-10-11 18:46:44 +02:00
2021-01-27 00:29:57 +01:00
2021-01-17 20:34:15 +01:00
2021-02-07 17:44:45 +01:00
2021-02-07 17:26:21 +01:00

Build Status Go Report Card GoDoc

Gorilla Swagger

Generate a swagger dynamically based on the types used to handle request and response.

It works with gorilla-mux and uses kin-openapi to automatically generate and serve a swagger file.

To convert struct to schemas, we use jsonschema library.
The struct must contains the appropriate struct tags to be inserted in json schema to generate the schema dynamically.
It is always possible to add a totally custom swagger schema using kin-openapi

Usage

An example usage of this lib:

context := context.Background()
r := mux.NewRouter()
router, _ := swagger.NewRouter(r, gswagger.Options{
  Context: context,
  Openapi: &openapi3.Swagger{
    Info: &openapi3.Info{
      Title:   "my swagger title",
      Version: "1.0.0",
    },
  },
})

okHandler := func(w http.ResponseWriter, req *http.Request) {
  w.WriteHeader(http.StatusOK)
  w.Write([]byte("OK"))
}

router.AddRoute(http.MethodPost, "/users", okHandler, Definitions{
  RequestBody: &gswagger.ContentValue{
    Content: gswagger.Content{
      "application/json": {Value: User{}},
    },
  },
  Responses: map[int]gswagger.ContentValue{
    201: {
      Content: gswagger.Content{
        "text/html": {Value: ""},
      },
    },
    401: {
      Content: gswagger.Content{
        "application/json": {Value: &errorResponse{}},
      },
      Description: "invalid request",
    },
  },
})

router.AddRoute(http.MethodGet, "/users", okHandler, Definitions{
  Responses: map[int]ContentValue{
    200: {
      Content: Content{
        "application/json": {Value: &Users{}},
      },
    },
  },
})

carSchema := openapi3.NewObjectSchema().WithProperties(map[string]*openapi3.Schema{
  "foo": openapi3.NewStringSchema(),
  "bar": openapi3.NewIntegerSchema().WithMax(15).WithMin(5),
})
requestBody := openapi3.NewRequestBody().WithJSONSchema(carSchema)
operation := swagger.NewOperation()
operation.AddRequestBody(requestBody)

router.AddRawRoute(http.MethodPost, "/cars", okHandler, operation)

This configuration will output the schema shown here

FAQ

  1. How to add format binary? Formats date-time, email, hostname, ipv4, ipv6, uri could be added with tag jsonschema. Others format could be added with tag jsonschema_extra. Not all the formats are supported (see discovered unsupported formats here).

  2. How to add a swagger with allOf? You can create manually a swagger with allOf using the AddRawRoute method.

  3. How to add a swagger with anyOf? You can create manually a swagger with anyOf using the AddRawRoute method.

  4. How to add a swagger with oneOf? You can create manually a swagger with oneOf using the AddRawRoute method, or use the jsonschema struct tag.

Discovered unsupported schema features

Formats:

Versioning

We use [SemVer][semver] for versioning. For the versions available, see the tags on this repository.

Description
No description provided
Readme MIT 544 KiB
Languages
Go 99.3%
Makefile 0.7%