Thanks to visit codestin.com
Credit goes to github.com

Skip to content

ihezebin/openapi

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

OPENAPI

Document a REST API with an OpenAPI 3.0 specification.

  • Code, not configuration.
  • No magic comments, tags, or decorators.
  • Use with or without a Go web framework.
  • Populates schema automatically using reflection.

Why would I want to use this?

  • Add OpenAPI documentation to an API.
  • Create a swagger.json or swagger.yaml file.
  • Serve the Swagger UI to customers.

Examples

See the ./examples directory for complete examples.

Create an OpenAPI 3.0 (swagger) file

// Configure the models.
api := openapi.NewAPI("messages")
api.StripPkgPaths = []string{"github.com/ihezebin/openapi/example", "github.com/a-h/respond"}

api.Get("/topic/{id}").
  HasPathParameter("id", openapi.PathParam{
   Description: "id of the topic",
   Regexp:      `\d+`,
  }).
  HasResponseModel(http.StatusOK, openapi.ModelOf[models.Body[models.Topic]]()).
  HasResponseModel(http.StatusInternalServerError, openapi.ModelOf[models.Body[map[string]string]]()).
  HasTags([]string{"Topic"}).
  HasDescription("Get one topic by id").
  HasSummary("getOneTopic").
  HasHeaderParameter("Authorization", openapi.HeaderParam{
   Description: "Bearer token",
   Required:    true,
   Type:        openapi.PrimitiveTypeString,
  }).
  HasQueryParameter("limit", openapi.QueryParam{
   Description: "limit",
   Required:    true,
  }).
  HasResponseHeader(http.StatusOK, "Token", openapi.HeaderParam{
   Description: "token",
   Required:    true,
   Type:        openapi.PrimitiveTypeString,
  }).HasDeprecated(true)

api.Json()

Tasks

test

go test ./...

run-example

Dir: ./examples/stdlib

go run main.go

Copyright

This project is based on https://github.com/a-h/rest by Adrian Hesketh.

Copyright (c) [2025] [Korbin]

This software is licensed under the MIT License. See the LICENSE file for details.

Differences

  • Optimized anonymous struct Model binding
  • Removed comment reading functionality to resolve generation failures in non-Go environments
  • Added common openapi3 properties

About

Document a REST API with an OpenAPI 3.0 specification.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages