Giter Site home page Giter Site logo

hypnoglow / oas2 Goto Github PK

View Code? Open in Web Editor NEW
19.0 4.0 5.0 224 KB

OpenAPI 2.0 (aka Swagger) utils for Golang.

License: MIT License

Makefile 0.16% Go 99.64% Shell 0.20%
openapi swagger router swagger-spec golang-middleware golang-router go-swagger request-validation go-toolkit

oas2's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

oas2's Issues

collectionFormat property is missing

Looks like collectionFormat is missing from parsing parameters process. Docs are here.

Here is a code to reproduce a bug I've faced

Swagger spec

swagger: "2.0"
info:
  version: debug
  title: Title
basePath: /api
schemes: [http]
produces: [text/plain]

paths:
  /test:
    get:
      description: Description
      operationId: test
      parameters:
        - name: param
          in: query
          type: array
          collectionFormat: csv
          items:
            type: string
      responses:
        "200":
          description: Response
          schema:
            type: string

Code

package main

import (
	"fmt"
	"net/http"
	"github.com/hypnoglow/oas2"
)

func main() {
	spec, err := oas.LoadFile("./swagger.yaml")
	if err != nil {
		panic(err)
	}
	r, err := oas.NewRouter(
		spec,
		oas.OperationHandlers{"test": http.HandlerFunc(handle)},
	)

	fmt.Println("Listening on :8080...")
	if err := http.ListenAndServe(":8080", r); err != nil {
		panic(err)
	}
}

func handle(w http.ResponseWriter, req *http.Request) {
	var q query
	if err := oas.DecodeQuery(req, &q); err != nil {
		panic(err)
	}

	// Return results of parsing to client
	resp := ""
	for i, v := range q.Param {
		resp += fmt.Sprintf("%d:%s;", i, v)
	}
	w.Write([]byte(resp + "\n"))
}

type query struct {
	Param []string `oas:"param"`
}

Run and check

$ curl 'http://localhost:8080/api/test?param=x,y'
0:x,y;

As you can see param string is not split into []string{"x", "y"} array, but instead stayed []string{"x,y"}.

I'll provide pr shortly.

Enable custom validators

Currently, custom validators like partial time are not enabled.

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

We should enable custom validator support as pluggable extensions, so functions like validate.Query can be instructed to validate certain custom formats.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.