Skip to content

Commit cd3ecae

Browse files
authored
Merge pull request #58 from yury-palyanitsa/fix-arr-obj-len-facets
Fix array and object length inheritance restrictions
2 parents 009b53c + ce897be commit cd3ecae

File tree

3 files changed

+32
-36
lines changed

3 files changed

+32
-36
lines changed

complex.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -120,14 +120,14 @@ func (s *ArrayShape) inherit(source Shape) (Shape, error) {
120120
}
121121
if s.MinItems == nil {
122122
s.MinItems = ss.MinItems
123-
} else if ss.MinItems != nil && *s.MinItems > *ss.MinItems {
123+
} else if ss.MinItems != nil && *s.MinItems < *ss.MinItems {
124124
return nil, StacktraceNew("minItems constraint violation", s.Location,
125125
stacktrace.WithPosition(&s.Position), stacktrace.WithInfo("source", *ss.MinItems),
126126
stacktrace.WithInfo("target", *s.MinItems))
127127
}
128128
if s.MaxItems == nil {
129129
s.MaxItems = ss.MaxItems
130-
} else if ss.MaxItems != nil && *s.MaxItems < *ss.MaxItems {
130+
} else if ss.MaxItems != nil && *s.MaxItems > *ss.MaxItems {
131131
return nil, StacktraceNew("maxItems constraint violation", s.Location,
132132
stacktrace.WithPosition(&s.Position), stacktrace.WithInfo("source", *ss.MaxItems),
133133
stacktrace.WithInfo("target", *s.MaxItems))
@@ -475,7 +475,7 @@ func (s *ObjectShape) validate(v interface{}, ctxPath string) error {
475475
func (s *ObjectShape) inheritMinProperties(source *ObjectShape) error {
476476
if s.MinProperties == nil {
477477
s.MinProperties = source.MinProperties
478-
} else if source.MinProperties != nil && *s.MinProperties > *source.MinProperties {
478+
} else if source.MinProperties != nil && *s.MinProperties < *source.MinProperties {
479479
return StacktraceNew("minProperties constraint violation", s.Location,
480480
stacktrace.WithPosition(&s.Position),
481481
stacktrace.WithInfo("source", *source.MinProperties),
@@ -487,7 +487,7 @@ func (s *ObjectShape) inheritMinProperties(source *ObjectShape) error {
487487
func (s *ObjectShape) inheritMaxProperties(source *ObjectShape) error {
488488
if s.MaxProperties == nil {
489489
s.MaxProperties = source.MaxProperties
490-
} else if source.MaxProperties != nil && *s.MaxProperties < *source.MaxProperties {
490+
} else if source.MaxProperties != nil && *s.MaxProperties > *source.MaxProperties {
491491
return StacktraceNew("maxProperties constraint violation", s.Location,
492492
stacktrace.WithPosition(&s.Position),
493493
stacktrace.WithInfo("source", *source.MaxProperties),

complex_test.go

+24-24
Original file line numberDiff line numberDiff line change
@@ -344,11 +344,11 @@ func TestArrayShape_Inherit(t *testing.T) {
344344
},
345345
},
346346
MinItems: func() *uint64 {
347-
i := uint64(3)
347+
i := uint64(1)
348348
return &i
349349
}(),
350350
MaxItems: func() *uint64 {
351-
i := uint64(3)
351+
i := uint64(6)
352352
return &i
353353
}(),
354354
UniqueItems: func() *bool {
@@ -427,7 +427,7 @@ func TestArrayShape_Inherit(t *testing.T) {
427427
BaseShape: &BaseShape{},
428428
ArrayFacets: ArrayFacets{
429429
MinItems: func() *uint64 {
430-
i := uint64(2)
430+
i := uint64(1)
431431
return &i
432432
}(),
433433
},
@@ -437,7 +437,7 @@ func TestArrayShape_Inherit(t *testing.T) {
437437
BaseShape: &BaseShape{},
438438
ArrayFacets: ArrayFacets{
439439
MinItems: func() *uint64 {
440-
i := uint64(1)
440+
i := uint64(2)
441441
return &i
442442
}(),
443443
},
@@ -451,7 +451,7 @@ func TestArrayShape_Inherit(t *testing.T) {
451451
BaseShape: &BaseShape{},
452452
ArrayFacets: ArrayFacets{
453453
MaxItems: func() *uint64 {
454-
i := uint64(1)
454+
i := uint64(2)
455455
return &i
456456
}(),
457457
},
@@ -461,7 +461,7 @@ func TestArrayShape_Inherit(t *testing.T) {
461461
BaseShape: &BaseShape{},
462462
ArrayFacets: ArrayFacets{
463463
MaxItems: func() *uint64 {
464-
i := uint64(2)
464+
i := uint64(1)
465465
return &i
466466
}(),
467467
},
@@ -1780,7 +1780,7 @@ func TestObjectShape_inheritMinProperties(t *testing.T) {
17801780
},
17811781
ObjectFacets: ObjectFacets{
17821782
MinProperties: func() *uint64 {
1783-
i := uint64(2)
1783+
i := uint64(4)
17841784
return &i
17851785
}(),
17861786
},
@@ -1792,7 +1792,7 @@ func TestObjectShape_inheritMinProperties(t *testing.T) {
17921792
},
17931793
ObjectFacets: ObjectFacets{
17941794
MinProperties: func() *uint64 {
1795-
i := uint64(4)
1795+
i := uint64(2)
17961796
return &i
17971797
}(),
17981798
},
@@ -1802,7 +1802,7 @@ func TestObjectShape_inheritMinProperties(t *testing.T) {
18021802
if got.MinProperties == nil {
18031803
return "MinProperties hasn't been inherited", false
18041804
}
1805-
if *got.MinProperties != 2 {
1805+
if *got.MinProperties != 4 {
18061806
return "MinProperties hasn't been inherited correctly", false
18071807
}
18081808
return "", true
@@ -1847,7 +1847,7 @@ func TestObjectShape_inheritMinProperties(t *testing.T) {
18471847
},
18481848
ObjectFacets: ObjectFacets{
18491849
MinProperties: func() *uint64 {
1850-
i := uint64(4)
1850+
i := uint64(2)
18511851
return &i
18521852
}(),
18531853
},
@@ -1859,7 +1859,7 @@ func TestObjectShape_inheritMinProperties(t *testing.T) {
18591859
},
18601860
ObjectFacets: ObjectFacets{
18611861
MinProperties: func() *uint64 {
1862-
i := uint64(2)
1862+
i := uint64(4)
18631863
return &i
18641864
}(),
18651865
},
@@ -1870,7 +1870,7 @@ func TestObjectShape_inheritMinProperties(t *testing.T) {
18701870
if got.MinProperties == nil {
18711871
return "MinProperties hasn't been inherited", false
18721872
}
1873-
if *got.MinProperties != 4 {
1873+
if *got.MinProperties != 2 {
18741874
return "MinProperties hasn't been inherited correctly", false
18751875
}
18761876
return "", true
@@ -1918,7 +1918,7 @@ func TestObjectShape_inheritMaxProperties(t *testing.T) {
19181918
},
19191919
ObjectFacets: ObjectFacets{
19201920
MaxProperties: func() *uint64 {
1921-
i := uint64(4)
1921+
i := uint64(2)
19221922
return &i
19231923
}(),
19241924
},
@@ -1930,7 +1930,7 @@ func TestObjectShape_inheritMaxProperties(t *testing.T) {
19301930
},
19311931
ObjectFacets: ObjectFacets{
19321932
MaxProperties: func() *uint64 {
1933-
i := uint64(2)
1933+
i := uint64(4)
19341934
return &i
19351935
}(),
19361936
},
@@ -1940,7 +1940,7 @@ func TestObjectShape_inheritMaxProperties(t *testing.T) {
19401940
if got.MaxProperties == nil {
19411941
return "MaxProperties hasn't been inherited", false
19421942
}
1943-
if *got.MaxProperties != 4 {
1943+
if *got.MaxProperties != 2 {
19441944
return "MaxProperties hasn't been inherited correctly", false
19451945
}
19461946
return "", true
@@ -1985,7 +1985,7 @@ func TestObjectShape_inheritMaxProperties(t *testing.T) {
19851985
},
19861986
ObjectFacets: ObjectFacets{
19871987
MaxProperties: func() *uint64 {
1988-
i := uint64(2)
1988+
i := uint64(4)
19891989
return &i
19901990
}(),
19911991
},
@@ -1997,7 +1997,7 @@ func TestObjectShape_inheritMaxProperties(t *testing.T) {
19971997
},
19981998
ObjectFacets: ObjectFacets{
19991999
MaxProperties: func() *uint64 {
2000-
i := uint64(4)
2000+
i := uint64(2)
20012001
return &i
20022002
}(),
20032003
},
@@ -2008,7 +2008,7 @@ func TestObjectShape_inheritMaxProperties(t *testing.T) {
20082008
if got.MaxProperties == nil {
20092009
return "MaxProperties hasn't been inherited", false
20102010
}
2011-
if *got.MaxProperties != 2 {
2011+
if *got.MaxProperties != 4 {
20122012
return "MaxProperties hasn't been inherited correctly", false
20132013
}
20142014
return "", true
@@ -2535,11 +2535,11 @@ func TestObjectShape_inherit(t *testing.T) {
25352535
},
25362536
ObjectFacets: ObjectFacets{
25372537
MinProperties: func() *uint64 {
2538-
i := uint64(4)
2538+
i := uint64(1)
25392539
return &i
25402540
}(),
25412541
MaxProperties: func() *uint64 {
2542-
i := uint64(2)
2542+
i := uint64(5)
25432543
return &i
25442544
}(),
25452545
Properties: func() *orderedmap.OrderedMap[string, Property] {
@@ -2700,7 +2700,7 @@ func TestObjectShape_inherit(t *testing.T) {
27002700
},
27012701
ObjectFacets: ObjectFacets{
27022702
MinProperties: func() *uint64 {
2703-
i := uint64(4)
2703+
i := uint64(2)
27042704
return &i
27052705
}(),
27062706
},
@@ -2712,7 +2712,7 @@ func TestObjectShape_inherit(t *testing.T) {
27122712
},
27132713
ObjectFacets: ObjectFacets{
27142714
MinProperties: func() *uint64 {
2715-
i := uint64(2)
2715+
i := uint64(4)
27162716
return &i
27172717
}(),
27182718
},
@@ -2728,7 +2728,7 @@ func TestObjectShape_inherit(t *testing.T) {
27282728
},
27292729
ObjectFacets: ObjectFacets{
27302730
MaxProperties: func() *uint64 {
2731-
i := uint64(2)
2731+
i := uint64(4)
27322732
return &i
27332733
}(),
27342734
},
@@ -2740,7 +2740,7 @@ func TestObjectShape_inherit(t *testing.T) {
27402740
},
27412741
ObjectFacets: ObjectFacets{
27422742
MaxProperties: func() *uint64 {
2743-
i := uint64(4)
2743+
i := uint64(2)
27442744
return &i
27452745
}(),
27462746
},

unwrap.go

+4-8
Original file line numberDiff line numberDiff line change
@@ -128,19 +128,15 @@ func (r *RAML) UnwrapShapes() error {
128128
r.fragmentAnnotationTypes = make(map[string]map[string]*BaseShape)
129129
r.shapes = make([]*BaseShape, 0, len(r.shapes))
130130
st := r.unwrapFragments()
131+
if st != nil {
132+
return st
133+
}
131134
err := r.markShapeRecursions()
132135
if err != nil {
133136
return fmt.Errorf("mark shape recursions: %w", err)
134137
}
135138
// Links to definedBy must be updated after unwrapping.
136-
se := r.unwrapDomainExtensions()
137-
if se != nil {
138-
if st == nil {
139-
st = se
140-
} else {
141-
st = st.Append(se)
142-
}
143-
}
139+
st = r.unwrapDomainExtensions()
144140
if st != nil {
145141
return st
146142
}

0 commit comments

Comments
 (0)