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

v0.16.7 #293

Merged
merged 5 commits into from
May 17, 2024
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
8 changes: 6 additions & 2 deletions datamodel/high/node_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,9 @@ func (n *NodeBuilder) add(key string, i int) {
sort.Slice(lines, func(i, j int) bool {
return lines[i] < lines[j]
})
nodeEntry.Line = lines[0]
if len(lines) > 0 {
nodeEntry.Line = lines[0]
}
case reflect.Struct:
y := value.Interface()
nodeEntry.Line = 9999 + i
Expand Down Expand Up @@ -394,7 +396,9 @@ func (n *NodeBuilder) AddYAMLNode(parent *yaml.Node, entry *nodes.NodeEntry) *ya
vn := vnut.GetValueNode()
if vn.Kind == yaml.SequenceNode {
for i := range vn.Content {
rawNode.Content[i].Style = vn.Content[i].Style
if len(rawNode.Content) > i {
rawNode.Content[i].Style = vn.Content[i].Style
}
}
}
}
Expand Down
119 changes: 119 additions & 0 deletions datamodel/high/node_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ func (r valueReferenceStruct) GoLowUntyped() any {
return &r
}

func (r valueReferenceStruct) GetValueNode() *yaml.Node {
n := utils.CreateEmptySequenceNode()
n.Content = append(n.Content, utils.CreateEmptySequenceNode())
return n
}

type plug struct {
Name []string `yaml:"name,omitempty"`
}
Expand Down Expand Up @@ -117,11 +123,103 @@ func (te *test1) GetKeyNode() *yaml.Node {
return kn
}

func (te *test1) GetValueNode() *yaml.Node {
kn := utils.CreateStringNode("meddy")
kn.Line = 20
return kn
}

func (te *test1) GoesLowUntyped() any {
panic("GoesLowUntyped")
return te
}

type test2 struct {
Thrat *valueReferenceStruct `yaml:"throg,omitempty"`
Thrig *orderedmap.Map[string, *plug] `yaml:"thrig,omitempty"`
Thing string `yaml:"thing,omitempty"`
Thong int `yaml:"thong,omitempty"`
Thrum int64 `yaml:"thrum,omitempty"`
Thang float32 `yaml:"thang,omitempty"`
Thung float64 `yaml:"thung,omitempty"`
Thyme bool `yaml:"thyme,omitempty"`
Thurm any `yaml:"thurm,omitempty"`
Thugg *bool `yaml:"thugg,renderZero"`
Thurr *int64 `yaml:"thurr,omitempty"`
Thral *float64 `yaml:"thral,omitempty"`
Throo *float64 `yaml:"throo,renderZero,omitempty"`
Tharg []string `yaml:"tharg,omitempty"`
Type []string `yaml:"type,omitempty"`
Throg []*valueReferenceStruct `yaml:"throg,omitempty"`
Throj *valueReferenceStruct `yaml:"throg,omitempty"`
Thrag []*orderedmap.Map[string, []string] `yaml:"thrag,omitempty"`
Thrug *orderedmap.Map[string, string] `yaml:"thrug,omitempty"`
Thoom []*orderedmap.Map[string, string] `yaml:"thoom,omitempty"`
Thomp *orderedmap.Map[low.KeyReference[string], string] `yaml:"thomp,omitempty"`
Thump valueReferenceStruct `yaml:"thump,omitempty"`
Thane valueReferenceStruct `yaml:"thane,omitempty"`
Thunk valueReferenceStruct `yaml:"thunk,omitempty"`
Thrim *valueReferenceStruct `yaml:"thrim,omitempty"`
Thril *orderedmap.Map[string, *valueReferenceStruct] `yaml:"thril,omitempty"`
Extensions *orderedmap.Map[string, *yaml.Node] `yaml:"-"`
ignoreMe string `yaml:"-"`
IgnoreMe string `yaml:"-"`
}

func (t test2) IsReference() bool {
return true
}

func (t test2) GetReference() string {
return "aggghhh"
}

func (t test2) SetReference(ref string, _ *yaml.Node) {

}

func (t test2) GetReferenceNode() *yaml.Node {
return nil
}

func (t test2) MarshalYAML() (interface{}, error) {
return utils.CreateStringNode("pizza"), nil
}

func (t test2) MarshalYAMLInline() (interface{}, error) {
return utils.CreateStringNode("pizza-inline!"), nil
}

func (t test2) GoLowUntyped() any {
return &t
}

func (t test2) GetValue() *yaml.Node {
return nil
}

func TestNewNodeBuilder_SliceRef_Inline_HasValue(t *testing.T) {
ty := []interface{}{utils.CreateEmptySequenceNode()}
t1 := test1{
Thrat: ty,
}

t2 := test2{
Thrat: &valueReferenceStruct{Value: renderZero},
}

nb := NewNodeBuilder(&t1, &t2)
nb.Resolve = true
node := nb.Render()

data, _ := yaml.Marshal(node)

desired := `thrat:
- []`

assert.Equal(t, desired, strings.TrimSpace(string(data)))
}

func TestNewNodeBuilder(t *testing.T) {
b := true
c := int64(12345)
Expand Down Expand Up @@ -606,6 +704,27 @@ func TestNewNodeBuilder_SliceRef_Inline(t *testing.T) {
assert.Equal(t, desired, strings.TrimSpace(string(data)))
}

func TestNewNodeBuilder_SliceRef_InlineNull(t *testing.T) {
c := valueReferenceStruct{Value: "milky"}
ty := []*valueReferenceStruct{&c}
t1 := test1{
Throg: ty,
}

t2 := test1{
Throg: []*valueReferenceStruct{},
}

nb := NewNodeBuilder(&t1, &t2)
node := nb.Render()

data, _ := yaml.Marshal(node)

desired := "throg:\n - pizza"

assert.Equal(t, desired, strings.TrimSpace(string(data)))
}

type testRender struct{}

func (t testRender) MarshalYAML() (interface{}, error) {
Expand Down
1 change: 1 addition & 0 deletions datamodel/spec_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ func ExtractSpecInfoWithDocumentCheck(spec []byte, bypass bool) (*SpecInfo, erro
case "3.1.0", "3.1":
specInfo.VersionNumeric = 3.1
specInfo.APISchema = OpenAPI31SchemaData
specInfo.SpecFormat = OAS31
default:
specInfo.VersionNumeric = 3.0
specInfo.APISchema = OpenAPI3SchemaData
Expand Down
2 changes: 1 addition & 1 deletion document.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ func (d *document) BuildV3Model() (*DocumentModel[v3high.Document], []error) {
errs = append(errs, fmt.Errorf("unable to build document, no specification has been loaded"))
return nil, errs
}
if d.info.SpecFormat != datamodel.OAS3 {
if d.info.SpecFormat != datamodel.OAS3 && d.info.SpecFormat != datamodel.OAS31 {
errs = append(errs, fmt.Errorf("unable to build openapi document, "+
"supplied spec is a different version (%v). Try 'BuildV2Model()'", d.info.SpecFormat))
return nil, errs
Expand Down
2 changes: 1 addition & 1 deletion index/spec_index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ func TestSpecIndex_DigitalOcean(t *testing.T) {
// get all the files!
files := remoteFS.GetFiles()
fileLen := len(files)
assert.Equal(t, 1654, fileLen)
assert.Equal(t, 1658, fileLen)
assert.Len(t, remoteFS.GetErrors(), 0)

// check circular references
Expand Down
Loading