How to add form field in query? #66
Unanswered
kpoznyakov
asked this question in
Q&A
Replies: 1 comment
-
I'd suggest using JSON for object values in query parameters. It is supported by Swagger UI and automated request decoder in Please check an example. package main
import (
"fmt"
"log"
"net/http"
"github.com/swaggest/openapi-go/openapi3"
)
func main() {
reflector := openapi3.Reflector{}
type jsonFilter struct {
Foo string `json:"foo"`
Bar int `json:"bar"`
}
type req struct {
ID string `path:"id" example:"XXX-XXXXX"`
Locale string `query:"locale" pattern:"^[a-z]{2}-[A-Z]{2}$"`
JSONFilter jsonFilter `query:"json_filter"`
}
getOp := openapi3.Operation{}
_ = reflector.SetRequest(&getOp, new(req), http.MethodGet)
_ = reflector.Spec.AddOperation(http.MethodGet, "/things/{id}", getOp)
schema, err := reflector.Spec.MarshalYAML()
if err != nil {
log.Fatal(err)
}
fmt.Println(string(schema))
} openapi: 3.0.3
info:
title: ""
version: ""
paths:
/things/{id}:
get:
parameters:
- in: query
name: locale
schema:
pattern: ^[a-z]{2}-[A-Z]{2}$
type: string
- content:
application/json:
schema:
$ref: '#/components/schemas/JsonFilter'
in: query
name: json_filter
- in: path
name: id
required: true
schema:
example: XXX-XXXXX
type: string
responses:
"204":
description: No Content
components:
schemas:
JsonFilter:
properties:
bar:
type: integer
foo:
type: string
type: object Another standard alternative is to use
https://go.dev/play/p/D6kJKKSLnhk package main
import (
"fmt"
"log"
"net/http"
"github.com/swaggest/openapi-go/openapi3"
)
func main() {
reflector := openapi3.Reflector{}
type deepObjectFilter struct {
Foo string `query:"foo"`
Bar int `query:"bar"`
}
type req struct {
ID string `path:"id" example:"XXX-XXXXX"`
Locale string `query:"locale" pattern:"^[a-z]{2}-[A-Z]{2}$"`
DeepObjectFilter deepObjectFilter `query:"deep_object_filter"`
}
getOp := openapi3.Operation{}
_ = reflector.SetRequest(&getOp, new(req), http.MethodGet)
_ = reflector.Spec.AddOperation(http.MethodGet, "/things/{id}", getOp)
schema, err := reflector.Spec.MarshalYAML()
if err != nil {
log.Fatal(err)
}
fmt.Println(string(schema))
} openapi: 3.0.3
info:
title: ""
version: ""
paths:
/things/{id}:
get:
parameters:
- in: query
name: locale
schema:
pattern: ^[a-z]{2}-[A-Z]{2}$
type: string
- explode: true
in: query
name: deep_object_filter
schema:
$ref: '#/components/schemas/DeepObjectFilter'
style: deepObject
- in: path
name: id
required: true
schema:
example: XXX-XXXXX
type: string
responses:
"204":
description: No Content
components:
schemas:
DeepObjectFilter:
properties:
bar:
type: integer
foo:
type: string
type: object |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I would like to add something like custom fields with filters, if i made id myself, i made it like
how do i can done this with openapi-go?
Beta Was this translation helpful? Give feedback.
All reactions