All Projects → hypnoglow → oas2

hypnoglow / oas2

Licence: MIT License
OpenAPI 2.0 (aka Swagger) utils for Golang.

Programming Languages

go
31211 projects - #10 most used programming language

Projects that are alternatives of or similar to oas2

Http Router
🎉 Release 2.0 is released! Very fast HTTP router for PHP 7.1+ (incl. PHP8 with attributes) based on PSR-7 and PSR-15 with support for annotations and OpenApi (Swagger)
Stars: ✭ 124 (+552.63%)
Mutual labels:  router, swagger, openapi
Openapi Backend
Build, Validate, Route, Authenticate and Mock using OpenAPI
Stars: ✭ 216 (+1036.84%)
Mutual labels:  router, swagger, openapi
gin-swagger
DRY templates for go-swagger
Stars: ✭ 79 (+315.79%)
Mutual labels:  swagger, openapi, go-swagger
openapi-assert
Asserting data against OpenAPI docs.
Stars: ✭ 17 (-10.53%)
Mutual labels:  swagger, openapi
Api
The Up Banking API Specification
Stars: ✭ 248 (+1205.26%)
Mutual labels:  swagger, openapi
Shins
Shins development continues at
Stars: ✭ 250 (+1215.79%)
Mutual labels:  swagger, openapi
Php Crud Api
Single file PHP script that adds a REST API to a SQL database
Stars: ✭ 2,904 (+15184.21%)
Mutual labels:  swagger, openapi
Dorado
基于Netty4开发的简单、轻量级、高性能的的Http restful api server
Stars: ✭ 65 (+242.11%)
Mutual labels:  router, swagger
Go Tgbot
Golang telegram bot API wrapper, session-based router and middleware
Stars: ✭ 90 (+373.68%)
Mutual labels:  router, swagger
api
🚀 Automatic SDK generation from an OpenAPI definition
Stars: ✭ 127 (+568.42%)
Mutual labels:  swagger, openapi
fhir-fuel.github.io
Place to prepare proposal to FHIR about JSON, JSON-Schema, Swagger/OpenAPI, JSON native databases and other JSON-frendly formats (yaml, edn, avro, protobuf etc) and technologies
Stars: ✭ 20 (+5.26%)
Mutual labels:  swagger, openapi
shipengine-openapi
The official OpenAPI 3.0 definitions for ShipEngine™
Stars: ✭ 13 (-31.58%)
Mutual labels:  swagger, openapi
Full Stack Fastapi Couchbase
Full stack, modern web application generator. Using FastAPI, Couchbase as database, Docker, automatic HTTPS and more.
Stars: ✭ 243 (+1178.95%)
Mutual labels:  swagger, openapi
Grpc Swagger
Debugging gRPC application with swagger-ui.
Stars: ✭ 242 (+1173.68%)
Mutual labels:  swagger, openapi
fastify-openapi-glue
A plugin for Fastify to autogenerate a configuration based on a OpenApi(v2/v3) specification.
Stars: ✭ 94 (+394.74%)
Mutual labels:  swagger, openapi
Mockoon
Mockoon is the easiest and quickest way to run mock APIs locally. No remote deployment, no account required, open source.
Stars: ✭ 3,448 (+18047.37%)
Mutual labels:  swagger, openapi
Spot
Spot is a concise, developer-friendly way to describe your API contract.
Stars: ✭ 230 (+1110.53%)
Mutual labels:  swagger, openapi
Openapi Codegen
OpenAPI 3.0 CodeGen plus Node.js minus the Java and emojis
Stars: ✭ 224 (+1078.95%)
Mutual labels:  swagger, openapi
Flasgger
Easy OpenAPI specs and Swagger UI for your Flask API
Stars: ✭ 2,825 (+14768.42%)
Mutual labels:  swagger, openapi
kompendium
Ktor OpenAPI Spec Generator
Stars: ✭ 46 (+142.11%)
Mutual labels:  swagger, openapi

oas2

GoDoc CircleCI codecov Go Report Card GitHub release License MIT

Note that this is not stable yet. In accordance with semantic versioning, the API can change between any minor versions. Use a vendoring tool of your preference to lock an exact release version.

Package oas2 provides utilities for building APIs using the OpenAPI 2.0 specification (aka Swagger) in Go idiomatic way on top of net/http.

You don't need to learn any special framework or write net/http-incompatible code - just delegate request validation, request parameters decoding and other routines to this library - and focus on your application logic.

This package is built on top of OpenAPI Initiative golang toolkit.

Should I have an OpenAPI specification for my API?

If you don't have a spec for your API yet - it's definitely worth it to create one. The specification itself provides many useful things, such as documentation, usage examples, and others. Learn more about OpenAPI and its purposes. The great thing is that it is compatible with many tools for developers and consumers; Swagger Toolkit is the most popular set of utilities for OpenAPI.

This package offers an integration of the spec with your code. And tightly coupling your code with the spec is a good thing - you create a strong contract for API consumers, and any changes to your API will be clearly reflected in the spec. You will see many benefits, such as distinctly recognize the situation when you need to increase the major version of your API because of incompatible changes.

Features

Router from a spec

This package provides an easy way to automatically create a router supporting all resources from your OpenAPI specification file. The underlying router is only your choice - you can use gorilla/mux, chi or any other.

Let's dive into a simple example.

Given a spec: petstore.yaml

First of all, load your spec in your app (note that though package import path ends in oas2, the package namespace is actually oas):

import "github.com/hypnoglow/oas2"

// ...

// specPath is a path to your spec file.
doc, _ := oas.LoadFile(specPath)

Next, create an operation handler. Let's define a handler for findPetsByStatus operation:

type FindPetsByStatusHandler struct {
	storage PetStorage
}

func (h FindPetsByStatusHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
	statuses := req.URL.Query()["status"]

	pets := h.storage.FindByStatus(statuses)

	_ = json.NewEncoder(w).Encode(pets)
}
handlers := oas.OperationHandlers{
    "findPetsByStatus":    findPetsByStatus{},
}

Define what options (logger, middleware) you will use:

logger := logrus.New()
logger.SetLevel(logrus.DebugLevel)
queryValidator := oas.QueryValidator(errHandler)

Create a router:

router, _ := oas.NewRouter(
    doc, 
    handlers, 
    oas.DebugLog(logger.Debugf), 
    oas.Use(queryValidator)
)

Then you can use your router as an argument for http.ListenAndServe or even as a subrouter for the other router.

http.ListenAndServe(":8080", router)

Now the server handles requests based on the paths defined in the given spec. It validates request query parameters against the spec and runs errHandler func if any error occured during validation. The router also sets the operation identifier to each request's context, so it can be used in a handler or any custom middleware.

See the full example for the complete code.

Decode query parameters to a struct

Given request query parameters: ?name=John&age=27

Given OpenAPI v2 (swagger) spec:

...
parameters:
- name: name
  type: string
- name: age
  type: integer
  format: int32
- name: loves_apples
  type: bool
  default: true
...

In your Go code you create a struct:

type Member struct {
	Name        string `oas:"name"`
	Age         int32  `oas:"age"`
	LovesApples bool   `oas:"loves_apples"`
}

And populate it:

var m Member 
oas.DecodeQuery(req, &m)

fmt.Printf("%#v", m) // Member{Name:"John", Age:27, LovesApples:true}

Note that it works only with oas router, because it needs to extract operation spec from the request. To use custom parameters spec, use oas.DecodeQueryParams(). See godoc example for details.

Pluggable formats & validators

The specification allows to have custom formats and to validate against them.

This package provides the following custom formats and validators:

You can also implement your custom format and validator for it, and then register it:

validate.RegisterFormat("myformat", &MyCustomFormat{}, ValidateMyCustomFormat)

License

MIT.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].