This repository has been archived by the owner on Nov 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 171
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
encoding/openapi: detect cycles when expanding references
Fixes #915 Change-Id: Ie781ee316e8675da66f7ca3bea4c841acaaa8a5b Reviewed-on: https://cue-review.googlesource.com/c/cue/+/9603 Reviewed-by: CUE cueckoo <[email protected]> Reviewed-by: Marcel van Lohuizen <[email protected]>
- Loading branch information
Showing
6 changed files
with
185 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// Copyright 2021 CUE Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package openapi | ||
|
||
import ( | ||
"cuelang.org/go/cue" | ||
"cuelang.org/go/cue/errors" | ||
"cuelang.org/go/cue/token" | ||
"cuelang.org/go/internal/core/dep" | ||
"cuelang.org/go/internal/core/eval" | ||
internalvalue "cuelang.org/go/internal/value" | ||
) | ||
|
||
func (b *builder) pushNode(v cue.Value) { | ||
_, n := internalvalue.ToInternal(v) | ||
b.ctx.cycleNodes = append(b.ctx.cycleNodes, n) | ||
} | ||
|
||
func (b *builder) popNode() { | ||
b.ctx.cycleNodes = b.ctx.cycleNodes[:len(b.ctx.cycleNodes)-1] | ||
} | ||
|
||
func (b *builder) checkCycle(v cue.Value) bool { | ||
if !b.ctx.expandRefs { | ||
return true | ||
} | ||
r, n := internalvalue.ToInternal(v) | ||
ctx := eval.NewContext(r, n) | ||
|
||
err := dep.Visit(ctx, n, func(d dep.Dependency) error { | ||
for _, m := range b.ctx.cycleNodes { | ||
if m == d.Node { | ||
var p token.Pos | ||
if src := d.Node.Source(); src != nil { | ||
p = src.Pos() | ||
} | ||
err := errors.Newf(p, | ||
"cycle in reference at %v: cyclic structures not allowed when reference expansion is requested", v.Path()) | ||
b.ctx.errs = errors.Append(b.ctx.errs, err) | ||
return err | ||
} | ||
} | ||
return nil | ||
}) | ||
|
||
return err == nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// Issue #915 | ||
#Foo: [string]: #Foo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"openapi": "3.0.0", | ||
"info": { | ||
"title": "test", | ||
"version": "v1" | ||
}, | ||
"paths": {}, | ||
"components": { | ||
"schemas": { | ||
"Foo": { | ||
"description": "Issue #915", | ||
"type": "object", | ||
"additionalProperties": { | ||
"$ref": "#/components/schemas/Foo" | ||
} | ||
} | ||
} | ||
} | ||
} |