Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(gen): response comparison #828

Merged
merged 3 commits into from
Mar 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions _testdata/negative/convenient_errors/different_response.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
{
"openapi": "3.0.3",
"info": {
"title": "Negative test: one operation has different response",
"version": "0.1.0"
},
"paths": {
"/data": {
"get": {
"description": "Retrieve data",
"operationId": "dataGet",
"responses": {
"default": {
"description": "Data",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Data"
}
}
}
}
}
},
"post": {
"description": "Creates data",
"operationId": "dataCreate",
"responses": {
"default": {
"description": "General Error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
}
}
}
}
},
"components": {
"schemas": {
"Data": {
"description": "Some data",
"type": "object",
"required": [
"name"
],
"properties": {
"name": {
"type": "string"
}
}
},
"Error": {
"description": "Represents error object",
"type": "object",
"properties": {
"code": {
"type": "integer",
"format": "int64"
},
"message": {
"type": "string"
}
},
"required": [
"code",
"message"
]
}
}
}
}
86 changes: 86 additions & 0 deletions _testdata/negative/convenient_errors/no_default.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
{
"openapi": "3.0.3",
"info": {
"title": "Negative test: one operation has no default",
"version": "0.1.0"
},
"paths": {
"/data": {
"get": {
"description": "Retrieve data",
"operationId": "dataGet",
"responses": {
"200": {
"description": "Data",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Data"
}
}
}
}
}
},
"post": {
"description": "Creates data",
"operationId": "dataCreate",
"responses": {
"200": {
"description": "Data",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Data"
}
}
}
},
"default": {
"description": "General Error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
}
}
}
}
},
"components": {
"schemas": {
"Data": {
"description": "Some data",
"type": "object",
"required": [
"name"
],
"properties": {
"name": {
"type": "string"
}
}
},
"Error": {
"description": "Represents error object",
"type": "object",
"properties": {
"code": {
"type": "integer",
"format": "int64"
},
"message": {
"type": "string"
}
},
"required": [
"code",
"message"
]
}
}
}
}
76 changes: 76 additions & 0 deletions _testdata/negative/convenient_errors/unsupported_ct.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
{
"openapi": "3.0.3",
"info": {
"title": "Negative test: unsupported content-type",
"version": "0.1.0"
},
"paths": {
"/data": {
"get": {
"description": "Retrieve data",
"operationId": "dataGet",
"responses": {
"default": {
"description": "General Error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
}
}
},
"post": {
"description": "Creates data",
"operationId": "dataCreate",
"responses": {
"default": {
"description": "General Error",
"content": {
"application/xml": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
}
}
}
}
},
"components": {
"schemas": {
"Data": {
"description": "Some data",
"type": "object",
"required": [
"name"
],
"properties": {
"name": {
"type": "string"
}
}
},
"Error": {
"description": "Represents error object",
"type": "object",
"properties": {
"code": {
"type": "integer",
"format": "int64"
},
"message": {
"type": "string"
}
},
"required": [
"code",
"message"
]
}
}
}
}
20 changes: 17 additions & 3 deletions gen/reduce.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/go-faster/errors"
"go.uber.org/zap"

"github.com/ogen-go/ogen/gen/ir"
"github.com/ogen-go/ogen/internal/location"
ogenjson "github.com/ogen-go/ogen/json"
"github.com/ogen-go/ogen/openapi"
Expand Down Expand Up @@ -61,6 +62,19 @@ func (g *Generator) reduceDefault(ops []*openapi.Operation) error {
// TODO(tdakkota): point to "content", not to the entire response
return reduceFailed(`response is multi-content`, d)
}
{
var ct ir.Encoding
for key := range d.Content {
ct = ir.Encoding(key)
break
}
if override, ok := g.opt.ContentTypeAliases[string(ct)]; ok {
ct = override
}
if !ct.JSON() {
return reduceFailed(`response content must be JSON`, d)
}
}

compareResponses := func(a, b *openapi.Response) bool {
// Compile time check to not forget to update compareResponses.
Expand Down Expand Up @@ -101,9 +115,9 @@ func (g *Generator) reduceDefault(ops []*openapi.Operation) error {
}

y, err := json.Marshal(compare{
a.Ref,
a.Headers,
a.Content,
b.Ref,
b.Headers,
b.Content,
})
if err != nil {
return false
Expand Down
15 changes: 12 additions & 3 deletions gen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,10 @@ func TestGenerate(t *testing.T) {

func TestNegative(t *testing.T) {
walkTestdata(t, "_testdata/negative", func(t *testing.T, file string, data []byte) {
log := zaptest.NewLogger(t)

a := require.New(t)
_, name := path.Split(file)
dir, name := path.Split(file)

spec, err := ogen.Parse(data)
a.NoError(err)
Expand All @@ -215,10 +217,17 @@ func TestNegative(t *testing.T) {
})
a.NoError(err, "If the error is related to parser, move this test to parser package testdata")

_, err = gen.NewGenerator(spec, gen.Options{
opt := gen.Options{
InferSchemaType: true,
File: f,
})
Logger: log,
}
t.Logf("Dir: %q, file: %q", dir, name)
if strings.Contains(dir, "convenient_errors") {
require.NoError(t, opt.ConvenientErrors.Set("on"))
}

_, err = gen.NewGenerator(spec, opt)
a.Error(err)

var buf strings.Builder
Expand Down