Skip to content

Commit

Permalink
Merge pull request #58 from yury-palyanitsa/fix-arr-obj-len-facets
Browse files Browse the repository at this point in the history
Fix array and object length inheritance restrictions
  • Loading branch information
IKukhta authored Dec 10, 2024
2 parents 009b53c + ce897be commit cd3ecae
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 36 deletions.
8 changes: 4 additions & 4 deletions complex.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,14 @@ func (s *ArrayShape) inherit(source Shape) (Shape, error) {
}
if s.MinItems == nil {
s.MinItems = ss.MinItems
} else if ss.MinItems != nil && *s.MinItems > *ss.MinItems {
} else if ss.MinItems != nil && *s.MinItems < *ss.MinItems {
return nil, StacktraceNew("minItems constraint violation", s.Location,
stacktrace.WithPosition(&s.Position), stacktrace.WithInfo("source", *ss.MinItems),
stacktrace.WithInfo("target", *s.MinItems))
}
if s.MaxItems == nil {
s.MaxItems = ss.MaxItems
} else if ss.MaxItems != nil && *s.MaxItems < *ss.MaxItems {
} else if ss.MaxItems != nil && *s.MaxItems > *ss.MaxItems {
return nil, StacktraceNew("maxItems constraint violation", s.Location,
stacktrace.WithPosition(&s.Position), stacktrace.WithInfo("source", *ss.MaxItems),
stacktrace.WithInfo("target", *s.MaxItems))
Expand Down Expand Up @@ -475,7 +475,7 @@ func (s *ObjectShape) validate(v interface{}, ctxPath string) error {
func (s *ObjectShape) inheritMinProperties(source *ObjectShape) error {
if s.MinProperties == nil {
s.MinProperties = source.MinProperties
} else if source.MinProperties != nil && *s.MinProperties > *source.MinProperties {
} else if source.MinProperties != nil && *s.MinProperties < *source.MinProperties {
return StacktraceNew("minProperties constraint violation", s.Location,
stacktrace.WithPosition(&s.Position),
stacktrace.WithInfo("source", *source.MinProperties),
Expand All @@ -487,7 +487,7 @@ func (s *ObjectShape) inheritMinProperties(source *ObjectShape) error {
func (s *ObjectShape) inheritMaxProperties(source *ObjectShape) error {
if s.MaxProperties == nil {
s.MaxProperties = source.MaxProperties
} else if source.MaxProperties != nil && *s.MaxProperties < *source.MaxProperties {
} else if source.MaxProperties != nil && *s.MaxProperties > *source.MaxProperties {
return StacktraceNew("maxProperties constraint violation", s.Location,
stacktrace.WithPosition(&s.Position),
stacktrace.WithInfo("source", *source.MaxProperties),
Expand Down
48 changes: 24 additions & 24 deletions complex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,11 +344,11 @@ func TestArrayShape_Inherit(t *testing.T) {
},
},
MinItems: func() *uint64 {
i := uint64(3)
i := uint64(1)
return &i
}(),
MaxItems: func() *uint64 {
i := uint64(3)
i := uint64(6)
return &i
}(),
UniqueItems: func() *bool {
Expand Down Expand Up @@ -427,7 +427,7 @@ func TestArrayShape_Inherit(t *testing.T) {
BaseShape: &BaseShape{},
ArrayFacets: ArrayFacets{
MinItems: func() *uint64 {
i := uint64(2)
i := uint64(1)
return &i
}(),
},
Expand All @@ -437,7 +437,7 @@ func TestArrayShape_Inherit(t *testing.T) {
BaseShape: &BaseShape{},
ArrayFacets: ArrayFacets{
MinItems: func() *uint64 {
i := uint64(1)
i := uint64(2)
return &i
}(),
},
Expand All @@ -451,7 +451,7 @@ func TestArrayShape_Inherit(t *testing.T) {
BaseShape: &BaseShape{},
ArrayFacets: ArrayFacets{
MaxItems: func() *uint64 {
i := uint64(1)
i := uint64(2)
return &i
}(),
},
Expand All @@ -461,7 +461,7 @@ func TestArrayShape_Inherit(t *testing.T) {
BaseShape: &BaseShape{},
ArrayFacets: ArrayFacets{
MaxItems: func() *uint64 {
i := uint64(2)
i := uint64(1)
return &i
}(),
},
Expand Down Expand Up @@ -1780,7 +1780,7 @@ func TestObjectShape_inheritMinProperties(t *testing.T) {
},
ObjectFacets: ObjectFacets{
MinProperties: func() *uint64 {
i := uint64(2)
i := uint64(4)
return &i
}(),
},
Expand All @@ -1792,7 +1792,7 @@ func TestObjectShape_inheritMinProperties(t *testing.T) {
},
ObjectFacets: ObjectFacets{
MinProperties: func() *uint64 {
i := uint64(4)
i := uint64(2)
return &i
}(),
},
Expand All @@ -1802,7 +1802,7 @@ func TestObjectShape_inheritMinProperties(t *testing.T) {
if got.MinProperties == nil {
return "MinProperties hasn't been inherited", false
}
if *got.MinProperties != 2 {
if *got.MinProperties != 4 {
return "MinProperties hasn't been inherited correctly", false
}
return "", true
Expand Down Expand Up @@ -1847,7 +1847,7 @@ func TestObjectShape_inheritMinProperties(t *testing.T) {
},
ObjectFacets: ObjectFacets{
MinProperties: func() *uint64 {
i := uint64(4)
i := uint64(2)
return &i
}(),
},
Expand All @@ -1859,7 +1859,7 @@ func TestObjectShape_inheritMinProperties(t *testing.T) {
},
ObjectFacets: ObjectFacets{
MinProperties: func() *uint64 {
i := uint64(2)
i := uint64(4)
return &i
}(),
},
Expand All @@ -1870,7 +1870,7 @@ func TestObjectShape_inheritMinProperties(t *testing.T) {
if got.MinProperties == nil {
return "MinProperties hasn't been inherited", false
}
if *got.MinProperties != 4 {
if *got.MinProperties != 2 {
return "MinProperties hasn't been inherited correctly", false
}
return "", true
Expand Down Expand Up @@ -1918,7 +1918,7 @@ func TestObjectShape_inheritMaxProperties(t *testing.T) {
},
ObjectFacets: ObjectFacets{
MaxProperties: func() *uint64 {
i := uint64(4)
i := uint64(2)
return &i
}(),
},
Expand All @@ -1930,7 +1930,7 @@ func TestObjectShape_inheritMaxProperties(t *testing.T) {
},
ObjectFacets: ObjectFacets{
MaxProperties: func() *uint64 {
i := uint64(2)
i := uint64(4)
return &i
}(),
},
Expand All @@ -1940,7 +1940,7 @@ func TestObjectShape_inheritMaxProperties(t *testing.T) {
if got.MaxProperties == nil {
return "MaxProperties hasn't been inherited", false
}
if *got.MaxProperties != 4 {
if *got.MaxProperties != 2 {
return "MaxProperties hasn't been inherited correctly", false
}
return "", true
Expand Down Expand Up @@ -1985,7 +1985,7 @@ func TestObjectShape_inheritMaxProperties(t *testing.T) {
},
ObjectFacets: ObjectFacets{
MaxProperties: func() *uint64 {
i := uint64(2)
i := uint64(4)
return &i
}(),
},
Expand All @@ -1997,7 +1997,7 @@ func TestObjectShape_inheritMaxProperties(t *testing.T) {
},
ObjectFacets: ObjectFacets{
MaxProperties: func() *uint64 {
i := uint64(4)
i := uint64(2)
return &i
}(),
},
Expand All @@ -2008,7 +2008,7 @@ func TestObjectShape_inheritMaxProperties(t *testing.T) {
if got.MaxProperties == nil {
return "MaxProperties hasn't been inherited", false
}
if *got.MaxProperties != 2 {
if *got.MaxProperties != 4 {
return "MaxProperties hasn't been inherited correctly", false
}
return "", true
Expand Down Expand Up @@ -2535,11 +2535,11 @@ func TestObjectShape_inherit(t *testing.T) {
},
ObjectFacets: ObjectFacets{
MinProperties: func() *uint64 {
i := uint64(4)
i := uint64(1)
return &i
}(),
MaxProperties: func() *uint64 {
i := uint64(2)
i := uint64(5)
return &i
}(),
Properties: func() *orderedmap.OrderedMap[string, Property] {
Expand Down Expand Up @@ -2700,7 +2700,7 @@ func TestObjectShape_inherit(t *testing.T) {
},
ObjectFacets: ObjectFacets{
MinProperties: func() *uint64 {
i := uint64(4)
i := uint64(2)
return &i
}(),
},
Expand All @@ -2712,7 +2712,7 @@ func TestObjectShape_inherit(t *testing.T) {
},
ObjectFacets: ObjectFacets{
MinProperties: func() *uint64 {
i := uint64(2)
i := uint64(4)
return &i
}(),
},
Expand All @@ -2728,7 +2728,7 @@ func TestObjectShape_inherit(t *testing.T) {
},
ObjectFacets: ObjectFacets{
MaxProperties: func() *uint64 {
i := uint64(2)
i := uint64(4)
return &i
}(),
},
Expand All @@ -2740,7 +2740,7 @@ func TestObjectShape_inherit(t *testing.T) {
},
ObjectFacets: ObjectFacets{
MaxProperties: func() *uint64 {
i := uint64(4)
i := uint64(2)
return &i
}(),
},
Expand Down
12 changes: 4 additions & 8 deletions unwrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,19 +128,15 @@ func (r *RAML) UnwrapShapes() error {
r.fragmentAnnotationTypes = make(map[string]map[string]*BaseShape)
r.shapes = make([]*BaseShape, 0, len(r.shapes))
st := r.unwrapFragments()
if st != nil {
return st
}
err := r.markShapeRecursions()
if err != nil {
return fmt.Errorf("mark shape recursions: %w", err)
}
// Links to definedBy must be updated after unwrapping.
se := r.unwrapDomainExtensions()
if se != nil {
if st == nil {
st = se
} else {
st = st.Append(se)
}
}
st = r.unwrapDomainExtensions()
if st != nil {
return st
}
Expand Down

0 comments on commit cd3ecae

Please sign in to comment.