Skip to content

Instantly share code, notes, and snippets.

@podhmo
Created July 16, 2022 17:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save podhmo/a2e492c2ed576ff3233e8524bf65a38b to your computer and use it in GitHub Desktop.
Save podhmo/a2e492c2ed576ff3233e8524bf65a38b to your computer and use it in GitHub Desktop.
package main
import (
"context"
_ "embed"
"encoding/json"
"fmt"
"log"
"net/http"
"net/http/httptest"
"reflect"
"strings"
"github.com/getkin/kin-openapi/openapi3"
"github.com/getkin/kin-openapi/openapi3filter"
"github.com/getkin/kin-openapi/routers"
"github.com/getkin/kin-openapi/routers/gorillamux"
)
//go:embed openapi.json
var spec []byte
func main() {
log.SetFlags(0)
if err := run(); err != nil {
log.Fatalf("!! %+v", err)
}
}
func run() error {
ctx := context.Background()
baseURL := "http://localhost:8080"
doc, err := openapi3.NewLoader().LoadFromData(spec)
if err != nil {
return fmt.Errorf("load doc: %w", err)
}
if doc.Validate(ctx); err != nil {
return fmt.Errorf("validate doc: %w", err)
}
router, err := gorillamux.NewRouter(doc)
if err != nil {
return fmt.Errorf("new router: %w", err)
}
{
log.Println("ng request ----------------------------------------")
req, err := http.NewRequest("POST", baseURL+"/pets", strings.NewReader(`{}`))
if err != nil {
return fmt.Errorf("new request: %w", err)
}
req.Header.Set("Content-Type", "application/json")
if err := doRequest(ctx, router, req); err != nil {
log.Println(err)
}
}
{
log.Println("ng response ----------------------------------------")
req, err := http.NewRequest("POST", baseURL+"/pets", strings.NewReader(`{"name": "foo"}`))
if err != nil {
return fmt.Errorf("new request: %w", err)
}
req.Header.Set("Content-Type", "application/json")
if err := doRequest(ctx, router, req); err != nil {
log.Println(err)
}
}
return nil
}
func doRequest(ctx context.Context, router routers.Router, req *http.Request) error {
route, pathParams, err := router.FindRoute(req)
if err != nil {
return fmt.Errorf("find route: %w", err)
}
log.Println("find route:", route.Path, route.Method, pathParams)
reqInput := &openapi3filter.RequestValidationInput{
Request: req,
PathParams: pathParams,
QueryParams: req.URL.Query(),
Route: route,
// Options: nil, // ?
// ParamDecoder: nil, // ?
}
if err := openapi3filter.ValidateRequest(ctx, reqInput); err != nil {
log.Printf("validate request is failed: %T", err)
return fmt.Errorf("validate request: %w", err)
}
log.Println("request is ok")
rec := httptest.NewRecorder()
func(w http.ResponseWriter, req *http.Request) {
w.Header().Add("Content-Type", "application/json")
json.NewEncoder(w).Encode(map[string]interface{}{})
}(rec, req)
res := rec.Result()
resInput := &openapi3filter.ResponseValidationInput{
RequestValidationInput: reqInput,
Status: 200,
Header: res.Header,
Body: res.Body,
Options: nil, // ?
}
if err := openapi3filter.ValidateResponse(ctx, resInput); err != nil {
log.Printf("valicate response is failed: %T", err)
return fmt.Errorf("validate response: %w", err)
}
return nil
}
func dumpRoutes(doc *openapi3.T) {
expectType := reflect.TypeOf(&openapi3.Operation{})
for k, path := range doc.Paths {
rv := reflect.ValueOf(path).Elem()
rt := reflect.TypeOf(path).Elem()
for i := 0; i < rt.NumField(); i++ {
rf := rt.Field(i)
if !rf.Type.AssignableTo(expectType) {
continue
}
rfv := rv.Field(i)
if rfv.IsNil() {
continue
}
op := rfv.Interface().(*openapi3.Operation)
fmt.Printf("%-10s\t%-10s\t%s\n", k, rf.Name, op.OperationID)
}
}
}
{
"components": {
"schemas": {
"Empty": {
"type": "object"
},
"Error": {
"additionalProperties": false,
"properties": {
"code": {
"type": "integer"
},
"message": {
"type": "string"
}
},
"required": [
"code"
],
"title": "Error",
"type": "object"
},
"Pet": {
"additionalProperties": false,
"properties": {
"id": {
"type": "string"
},
"name": {
"type": "string"
},
"tag": {
"type": "string"
}
},
"required": [
"id",
"name"
],
"type": "object"
}
}
},
"info": {
"description": "-",
"title": "Swagger Petstore",
"version": "1.0.0"
},
"openapi": "3.0.3",
"paths": {
"/pets": {
"get": {
"description": "Returns all pets from the system that the user has access to\n\tNam sed condimentum est. Maecenas tempor sagittis sapien, nec rhoncus sem sagittis sit amet. Aenean at gravida augue, ac iaculis sem. Curabitur odio lorem, ornare eget elementum nec, cursus id lectus. Duis mi turpis, pulvinar ac eros ac, tincidunt varius justo. In hac habitasse platea dictumst. Integer at adipiscing ante, a sagittis ligula. Aenean pharetra tempor ante molestie imperdiet. Vivamus id aliquam diam. Cras quis velit non tortor eleifend sagittis. Praesent at enim pharetra urna volutpat venenatis eget eget mauris. In eleifend fermentum facilisis. Praesent enim enim, gravida ac sodales sed, placerat id erat. Suspendisse lacus dolor, consectetur non augue vel, vehicula interdum libero. Morbi euismod sagittis libero sed lacinia.\n\tSed tempus felis lobortis leo pulvinar rutrum. Nam mattis velit nisl, eu condimentum ligula luctus nec. Phasellus semper velit eget aliquet faucibus. In a mattis elit. Phasellus vel urna viverra, condimentum lorem id, rhoncus nibh. Ut pellentesque posuere elementum. Sed a varius odio. Morbi rhoncus ligula libero, vel eleifend nunc tristique vitae. Fusce et sem dui. Aenean nec scelerisque tortor. Fusce malesuada accumsan magna vel tempus. Quisque mollis felis eu dolor tristique, sit amet auctor felis gravida. Sed libero lorem, molestie sed nisl in, accumsan tempor nisi. Fusce sollicitudin massa ut lacinia mattis. Sed vel eleifend lorem. Pellentesque vitae felis pretium, pulvinar elit eu, euismod sapien.",
"operationId": "main.FindPets",
"parameters": [
{
"in": "query",
"name": "Tags",
"schema": {
"items": {
"type": "string"
},
"type": "array"
}
},
{
"in": "query",
"name": "Limit",
"schema": {
"type": "integer"
}
}
],
"responses": {
"200": {
"content": {
"application/json": {
"schema": {
"additionalProperties": false,
"properties": {
"items": {
"items": {
"$ref": "#/components/schemas/Pet"
},
"type": "array"
}
},
"required": [
"items"
],
"type": "object"
}
}
},
"description": ""
},
"default": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
},
"description": "default error"
}
},
"summary": "returns all pets"
},
"post": {
"description": "Creates a new pet in the store. Duplicates are allowed",
"operationId": "main.AddPet",
"requestBody": {
"content": {
"application/json": {
"schema": {
"additionalProperties": false,
"properties": {
"name": {
"type": "string"
},
"tag": {
"type": "string"
}
},
"required": [
"name"
],
"type": "object"
}
}
}
},
"responses": {
"200": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Pet"
}
}
},
"description": ""
},
"400": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
},
"description": "validation error"
},
"default": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
},
"description": "default error"
}
},
"summary": "creates a new pet in the store. Duplicates are allowed"
}
},
"/pets/{id}": {
"delete": {
"description": "delete a single pet based on the ID supplied",
"operationId": "main.DeletePet",
"parameters": [
{
"in": "path",
"name": "id",
"required": true,
"schema": {
"type": "integer"
}
}
],
"responses": {
"204": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Empty"
}
}
},
"description": ""
},
"default": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
},
"description": "default error"
}
},
"summary": "deletes a pet by ID"
},
"get": {
"description": "Returns a pet based on a single ID",
"operationId": "main.FindPetByID",
"parameters": [
{
"in": "path",
"name": "id",
"required": true,
"schema": {
"type": "integer"
}
}
],
"responses": {
"200": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Pet"
}
}
},
"description": ""
},
"default": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
},
"description": "default error"
}
},
"summary": "returns a pet based on a single ID"
}
}
},
"servers": [
{
"url": "http://petstore.swagger.io/api"
},
{
"description": "local development",
"url": "http://localhost:8080"
}
]
}
ng request ----------------------------------------
find route: /pets POST map[]
validate request is failed: *openapi3filter.RequestError
validate request: request body has an error: doesn't match the schema: Error at "/name": property "name" is missing
Schema:
{
"additionalProperties": false,
"properties": {
"name": {
"type": "string"
},
"tag": {
"type": "string"
}
},
"required": [
"name"
],
"type": "object"
}
Value:
{}
ng response ----------------------------------------
find route: /pets POST map[]
request is ok
valicate response is failed: *openapi3filter.ResponseError
validate response: response body doesn't match the schema: Error at "/id": property "id" is missing
Schema:
{
"additionalProperties": false,
"properties": {
"id": {
"type": "string"
},
"name": {
"type": "string"
},
"tag": {
"type": "string"
}
},
"required": [
"id",
"name"
],
"type": "object"
}
Value:
{}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment