-
Hello, I noticed that every schema is put in Is there some options that can be used so schemas of fields of structs are set in As an example, let's say I currently generated a document which contains :
Is there a way to have instead the following document ?
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hello, this is possible with https://go.dev/play/p/t_QV033Lejd package main
import (
"fmt"
"net/http"
"github.com/swaggest/openapi-go/openapi3"
)
type MyStruct struct {
Field1 *int `json:"field1"`
Field2 *bool `json:"field2"`
}
// ForceRequestBody enforces body on bodyless operations (GET/HEAD/DELETE/TRACE),
// would not be needed for POST/PUT/PATCH.
func (m MyStruct) ForceRequestBody() {}
func main() {
r := openapi3.Reflector{}
// Mark MyStruct as inlinable.
r.InlineDefinition(MyStruct{}) // <----
// Alternatively you can enable such behavior for all types, this may be dangerous if there are cyclic refs.
//
// r.DefaultOptions = append(r.DefaultOptions, /* "github.com/swaggest/jsonschema-go" */ jsonschema.InlineRefs)
getOp := openapi3.Operation{}
_ = r.SetRequest(&getOp, new(MyStruct), http.MethodGet)
_ = r.SpecEns().AddOperation(http.MethodGet, "/route_path", getOp)
schema, _ := r.Spec.MarshalYAML()
fmt.Println(string(schema))
} openapi: 3.0.3
info:
title: ""
version: ""
paths:
/route_path:
get:
requestBody:
content:
application/json:
schema:
properties:
field1:
nullable: true
type: integer
field2:
nullable: true
type: boolean
type: object
responses:
"204":
description: No Content |
Beta Was this translation helpful? Give feedback.
Hello, this is possible with
Reflector.InlineDefinition
.https://go.dev/play/p/t_QV033Lejd