hypnoglow / oas2 Goto Github PK
View Code? Open in Web Editor NEWOpenAPI 2.0 (aka Swagger) utils for Golang.
License: MIT License
OpenAPI 2.0 (aka Swagger) utils for Golang.
License: MIT License
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.
The package should support validating form params and provide a function to decode data from form params to the supplied struct. Same thing as query parameters.
It should support both application/x-www-form-urlencoded
and multipart/form-data
.
Ref: https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#parameterObject
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.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.