From 4b2f6671a58f427e1443375614adbd649f71e3e3 Mon Sep 17 00:00:00 2001 From: tdakkota Date: Sun, 3 Apr 2022 07:22:57 +0300 Subject: [PATCH 1/2] fix: check field Spec before dereference --- gen/gen_parameters.go | 3 +-- internal/ir/field.go | 2 +- internal/ir/json.go | 4 ++-- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/gen/gen_parameters.go b/gen/gen_parameters.go index 2a3f46fa7..8a9c118af 100644 --- a/gen/gen_parameters.go +++ b/gen/gen_parameters.go @@ -126,8 +126,7 @@ func isParamAllowed(t *ir.Type, root bool, visited map[*ir.Type]struct{}) error } for _, field := range t.Fields { if err := isParamAllowed(field.Type, false, visited); err != nil { - // TODO: Check field.Spec existence. - return errors.Wrapf(err, "field %q", field.Spec.Name) + return errors.Wrapf(err, "field %q", field.Name) } } return nil diff --git a/internal/ir/field.go b/internal/ir/field.go index 3ed02d249..51f8a9e98 100644 --- a/internal/ir/field.go +++ b/internal/ir/field.go @@ -35,7 +35,7 @@ type Field struct { Tag Tag // Whether field is inlined map (i.e. additionalProperties, patternProperties). Inline InlineField - // Spec is property schema. May be empty. + // Spec is property schema. May be nil. Spec *jsonschema.Property } diff --git a/internal/ir/json.go b/internal/ir/json.go index dd4460730..a44484fb3 100644 --- a/internal/ir/json.go +++ b/internal/ir/json.go @@ -47,7 +47,7 @@ func (j JSONFields) FirstRequiredIndex() int { // HasRequired whether object has required fields func (j JSONFields) HasRequired() bool { for _, f := range j { - if f.Spec.Required { + if f.Spec != nil && f.Spec.Required { return true } } @@ -66,7 +66,7 @@ func (j JSONFields) RequiredMask() (r []uint8) { bitIdx := i % 8 set := uint8(0) - if f.Spec.Required { + if f.Spec != nil && f.Spec.Required { set = 1 } r[maskIdx] |= set << uint8(bitIdx) From 2a60c434eba2141d91a9679df211b4c59f35b264 Mon Sep 17 00:00:00 2001 From: tdakkota Date: Sun, 3 Apr 2022 07:23:23 +0300 Subject: [PATCH 2/2] chore: move .yaml schema to .yml --- .../{petstore-expanded.yaml => petstore-expanded.yml} | 0 _testdata/positive/{petstore.yaml => petstore.yml} | 0 ..._empty_property_name.yaml => test_empty_property_name.yml} | 0 examples/generate.go | 4 ++-- 4 files changed, 2 insertions(+), 2 deletions(-) rename _testdata/positive/{petstore-expanded.yaml => petstore-expanded.yml} (100%) rename _testdata/positive/{petstore.yaml => petstore.yml} (100%) rename _testdata/positive/{test_empty_property_name.yaml => test_empty_property_name.yml} (100%) diff --git a/_testdata/positive/petstore-expanded.yaml b/_testdata/positive/petstore-expanded.yml similarity index 100% rename from _testdata/positive/petstore-expanded.yaml rename to _testdata/positive/petstore-expanded.yml diff --git a/_testdata/positive/petstore.yaml b/_testdata/positive/petstore.yml similarity index 100% rename from _testdata/positive/petstore.yaml rename to _testdata/positive/petstore.yml diff --git a/_testdata/positive/test_empty_property_name.yaml b/_testdata/positive/test_empty_property_name.yml similarity index 100% rename from _testdata/positive/test_empty_property_name.yaml rename to _testdata/positive/test_empty_property_name.yml diff --git a/examples/generate.go b/examples/generate.go index 74d6c1397..ff1c1c4cc 100644 --- a/examples/generate.go +++ b/examples/generate.go @@ -14,7 +14,7 @@ import ( //go:generate go run github.com/ogen-go/ogen/cmd/ogen --schema ../_testdata/positive/test_format.json --target ex_test_format --clean --generate-tests -//go:generate go run github.com/ogen-go/ogen/cmd/ogen --schema ../_testdata/positive/petstore.yaml --target ex_petstore --clean --generate-tests +//go:generate go run github.com/ogen-go/ogen/cmd/ogen --schema ../_testdata/positive/petstore.yml --target ex_petstore --clean --generate-tests //go:generate go run github.com/ogen-go/ogen/cmd/ogen --schema ../_testdata/positive/firecracker.json --target ex_firecracker --clean --generate-tests //go:generate go run github.com/ogen-go/ogen/cmd/ogen --schema ../_testdata/positive/gotd_bot_api.json --target ex_gotd --clean --generate-tests //go:generate go run github.com/ogen-go/ogen/cmd/ogen --schema ../_testdata/positive/ent.json --target ex_ent --clean --generate-tests @@ -23,7 +23,7 @@ import ( // Partially supported: //go:generate go run github.com/ogen-go/ogen/cmd/ogen --schema ../_testdata/positive/manga.json --target ex_manga --clean --debug.noerr "unsupported content types" --generate-tests -//go:generate go run github.com/ogen-go/ogen/cmd/ogen --schema ../_testdata/positive/petstore-expanded.yaml --target ex_petstore_expanded --clean --debug.noerr --generate-tests +//go:generate go run github.com/ogen-go/ogen/cmd/ogen --schema ../_testdata/positive/petstore-expanded.yml --target ex_petstore_expanded --clean --debug.noerr --generate-tests //go:generate go run github.com/ogen-go/ogen/cmd/ogen --schema ../_testdata/positive/k8s.json --target ex_k8s --clean --debug.noerr --generate-tests //go:generate go run github.com/ogen-go/ogen/cmd/ogen --schema ../_testdata/positive/api.github.com.json --target ex_github --clean --infer-types --debug.noerr --generate-tests //go:generate go run github.com/ogen-go/ogen/cmd/ogen --schema ../_testdata/positive/telegram_bot_api.json --target ex_telegram --clean --debug.noerr --generate-tests