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

feat(gen): improve generation cycle error reporting #852

Merged
merged 2 commits into from
Apr 10, 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
4 changes: 4 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ issues:
- linters: [revive, golint]
text: "(unused-parameter|if-return)"

- linters: [gocritic]
text: ptrToRefParam
source: handleSchemaDepth

# Intended in commands:
# G307: Deferring unsafe method "Close" on type "*os.File"
# G304: Potential file inclusion via variable
Expand Down
8 changes: 6 additions & 2 deletions gen/gen_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ func saveSchemaTypes(ctx *genctx, gen *schemaGen) error {
return nil
}

func (g *Generator) generateSchema(ctx *genctx, name string, schema *jsonschema.Schema, optional bool) (*ir.Type, error) {
func (g *Generator) generateSchema(ctx *genctx, name string, schema *jsonschema.Schema, optional bool) (_ *ir.Type, rerr error) {
defer handleSchemaDepth(schema, &rerr)

gen := newSchemaGen(ctx.lookupRef)
gen.log = g.log.Named("schemagen")
gen.fail = g.fail
Expand Down Expand Up @@ -92,7 +94,9 @@ func (o *GenerateSchemaOptions) setDefaults() {
}

// GenerateSchema generates type, validation and JSON encoding for given schema.
func GenerateSchema(schema *jsonschema.Schema, fs FileSystem, opts GenerateSchemaOptions) error {
func GenerateSchema(schema *jsonschema.Schema, fs FileSystem, opts GenerateSchemaOptions) (rerr error) {
defer handleSchemaDepth(schema, &rerr)

opts.setDefaults()

ctx := &genctx{
Expand Down
41 changes: 40 additions & 1 deletion gen/schema_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,49 @@ func variantFieldName(t *ir.Type) string {
return naming.Capitalize(t.NamePostfix())
}

type schemaDepthError struct {
limit int
}

func (e *schemaDepthError) Error() string {
return fmt.Sprintf("schema depth limit (%d) exceeded", e.limit)
}

func handleSchemaDepth(s *jsonschema.Schema, rerr *error) {
r := recover()
if r == nil {
return
}

e, ok := r.(*schemaDepthError)
if !ok {
panic(r)
}
*rerr = e

// Ensure that schema is not nil.
if s == nil {
return
}
ptr := s.Pointer

// Try to use location.Error.
pos, ok := ptr.Position()
if !ok {
return
}
*rerr = &location.Error{
File: ptr.File(),
Pos: pos,
Err: e,
}
}

func (g *schemaGen) generate(name string, schema *jsonschema.Schema, optional bool) (*ir.Type, error) {
g.depthCount++
if g.depthCount > g.depthLimit {
return nil, errors.Errorf("schema depth limit (%d) exceeded", g.depthLimit)
// Panicing is not cool, but is better rather than wrap the error N = depthLimit times.
panic(&schemaDepthError{limit: g.depthLimit})
}
defer func() {
g.depthCount--
Expand Down
19 changes: 15 additions & 4 deletions gen/schema_gen_sum.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"golang.org/x/exp/slices"

"github.com/ogen-go/ogen/gen/ir"
"github.com/ogen-go/ogen/internal/location"
"github.com/ogen-go/ogen/internal/xmaps"
"github.com/ogen-go/ogen/internal/xslices"
"github.com/ogen-go/ogen/jsonschema"
Expand Down Expand Up @@ -92,22 +93,32 @@ func ensureNoInfiniteRecursion(parent *jsonschema.Schema) error {
}
if ref := s.Ref; !ref.IsZero() {
if _, ok := ctx[ref]; ok {
return errors.Errorf("reference %q [%d] leads to infinite recursion", ref, i)
err := errors.Errorf("reference %q [%d] leads to infinite recursion", ref, i)

pos, ok := s.Pointer.Position()
if !ok {
return err
}
return &location.Error{
File: s.File(),
Pos: pos,
Err: err,
}
}
ctx[ref] = struct{}{}
}
switch {
case len(s.OneOf) > 0:
if err := do(ctx, s.OneOf); err != nil {
return errors.Wrapf(err, "oneOf %q [%d]", s.Ref, i)
return err
}
case len(s.AllOf) > 0:
if err := do(ctx, s.AllOf); err != nil {
return errors.Wrapf(err, "allOf %q [%d]", s.Ref, i)
return err
}
case len(s.AnyOf) > 0:
if err := do(ctx, s.AnyOf); err != nil {
return errors.Wrapf(err, "anyOf %q [%d]", s.Ref, i)
return err
}
}
delete(ctx, s.Ref)
Expand Down