Skip to content

Commit

Permalink
move operation's RequestBody into a named element of Components.Schemas
Browse files Browse the repository at this point in the history
  • Loading branch information
Pablo Perez Schroder committed Nov 24, 2020
1 parent 91f9fae commit 40eab12
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 24 deletions.
17 changes: 17 additions & 0 deletions fizz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,11 @@ type testInputModel struct {
QueryParam string `query:"q"`
}

type testInputModel2 struct {
C string `path:"c"`
Message string `json:"message" description:"A short message"`
}

// TestSpecHandler tests that the OpenAPI handler
// return the spec properly marshaled in JSON.
func TestSpecHandler(t *testing.T) {
Expand Down Expand Up @@ -272,6 +277,18 @@ func TestSpecHandler(t *testing.T) {
Description: `This is a test server.`,
Version: "1.0.0",
}

fizz.POST("/test/:c",
[]OperationOption{
ID("PostTest"),
StatusDescription("201"),
StatusDescription("Created"),
},
tonic.Handler(func(c *gin.Context, in *testInputModel2) error {
return nil
}, 201),
)

servers := []*openapi.Server{
&openapi.Server{
URL: "https://foo.bar/{basePath}",
Expand Down
35 changes: 28 additions & 7 deletions openapi/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ import (
)

const (
version = "3.0.1"
anyMediaType = "*/*"
formatTag = "format"
deprecatedTag = "deprecated"
descriptionTag = "description"
version = "3.0.1"
anyMediaType = "*/*"
formatTag = "format"
deprecatedTag = "deprecated"
descriptionTag = "description"
componentsSchemaPath = "#/components/schemas/"
)

var (
Expand Down Expand Up @@ -408,6 +409,26 @@ func (g *Generator) setOperationParams(op *Operation, t, parent reflect.Type, al
if err := g.buildParamsRecursive(op, t, parent, allowBody); err != nil {
return err
}
// Input fields that are neither path- nor query-bound
// have been extracted into the operation's RequestBody
// If the RequestBody is not nil, give it a name and
// move it to the openapi spec's components/schemas section
// Replace the RequestBody's schema with a reference
// to the named schema in components/schemas
if op.RequestBody != nil {
mt := tonic.MediaType()
if mt == "" {
mt = anyMediaType
}
sch := op.RequestBody.Content[mt].Schema
if sch != nil {
name := strings.Title(op.ID) + "Input"
g.api.Components.Schemas[name] = sch
op.RequestBody.Content[mt].Schema = &SchemaOrRef{Reference: &Reference{
Ref: componentsSchemaPath + name,
}}
}
}
// Extract all the path parameter names.
matches := paramsInPathRe.FindAllStringSubmatch(path, -1)
var pathParams []string
Expand Down Expand Up @@ -901,7 +922,7 @@ func (g *Generator) newSchemaFromStruct(t reflect.Type) *SchemaOrRef {
// because there is no guarantee the generation is complete yet.
if _, ok := g.schemaTypes[t]; ok {
return &SchemaOrRef{Reference: &Reference{
Ref: "#/components/schemas/" + name,
Ref: componentsSchemaPath + name,
}}
}
schema := &Schema{
Expand All @@ -925,7 +946,7 @@ func (g *Generator) newSchemaFromStruct(t reflect.Type) *SchemaOrRef {
g.api.Components.Schemas[name] = sor

return &SchemaOrRef{Reference: &Reference{
Ref: "#/components/schemas/" + name,
Ref: componentsSchemaPath + name,
}}
}
// Return an inlined schema for types with no name.
Expand Down
16 changes: 1 addition & 15 deletions testdata/schemas/path-item.json
Original file line number Diff line number Diff line change
Expand Up @@ -101,21 +101,7 @@
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"G": {
"type": "string",
"format": "byte"
},
"f": {
"type": "string",
"description": "This is F",
"nullable": true
}
},
"required": [
"G"
]
"$ref": "#/components/schemas/CreateTestInput"
}
}
}
Expand Down
43 changes: 42 additions & 1 deletion testdata/spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,48 @@
}
}
}
},
"/test/{c}": {
"post": {
"operationId": "PostTest",
"parameters": [
{
"name": "c",
"in": "path",
"required": true,
"schema": {
"type": "string"
}
}
],
"requestBody": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/PostTestInput"
}
}
}
},
"responses": {
"201": {
"description": "Created"
}
}
}
}
},
"components": {}
"components": {
"schemas": {
"PostTestInput": {
"type": "object",
"properties": {
"message": {
"type": "string",
"description": "A short message"
}
}
}
}
}
}
26 changes: 25 additions & 1 deletion testdata/spec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,28 @@ paths:
responses:
'200':
description: OK
components: {}
/test/{c}:
post:
operationId: PostTest
parameters:
- name: c
in: path
required: true
schema:
type: string
requestBody:
content:
application/json:
schema:
$ref: "#/components/schemas/PostTestInput"
responses:
"201":
description: Created
components:
schemas:
PostTestInput:
type: object
properties:
message:
type: string
description: "A short message"

0 comments on commit 40eab12

Please sign in to comment.