mirror of
https://github.com/LukeHagar/libopenapi.git
synced 2025-12-06 12:37:49 +00:00
@@ -4,6 +4,7 @@
|
||||
package high
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/pb33f/libopenapi/datamodel/low"
|
||||
"github.com/pb33f/libopenapi/utils"
|
||||
"gopkg.in/yaml.v3"
|
||||
@@ -19,6 +20,7 @@ type NodeEntry struct {
|
||||
Tag string
|
||||
Key string
|
||||
Value any
|
||||
StringValue string
|
||||
Line int
|
||||
RenderZero bool
|
||||
}
|
||||
@@ -131,8 +133,11 @@ func (n *NodeBuilder) add(key string, i int) {
|
||||
switch value.Kind() {
|
||||
case reflect.Float64, reflect.Float32:
|
||||
nodeEntry.Value = value.Float()
|
||||
x := float64(int(value.Float()*100)) / 100 // trim this down
|
||||
nodeEntry.StringValue = strconv.FormatFloat(x, 'f', -1, 64)
|
||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
||||
nodeEntry.Value = value.Int()
|
||||
nodeEntry.StringValue = value.String()
|
||||
case reflect.String:
|
||||
nodeEntry.Value = value.String()
|
||||
case reflect.Bool:
|
||||
@@ -316,7 +321,13 @@ func (n *NodeBuilder) AddYAMLNode(parent *yaml.Node, entry *NodeEntry) *yaml.Nod
|
||||
break
|
||||
|
||||
case reflect.Float64:
|
||||
val := strconv.FormatFloat(value.(float64), 'f', -1, 64)
|
||||
|
||||
precision := -1
|
||||
if entry.StringValue != "" && strings.Contains(entry.StringValue, ".") {
|
||||
precision = len(strings.Split(fmt.Sprint(entry.StringValue), ".")[1])
|
||||
}
|
||||
|
||||
val := strconv.FormatFloat(value.(float64), 'f', precision, 64)
|
||||
valueNode = utils.CreateFloatNode(val)
|
||||
valueNode.Line = line
|
||||
break
|
||||
|
||||
@@ -212,10 +212,10 @@ func TestNewNodeBuilder(t *testing.T) {
|
||||
data, _ := yaml.Marshal(node)
|
||||
|
||||
desired := `thing: ding
|
||||
thong: "1"
|
||||
thrum: "1234567"
|
||||
thang: 2.20
|
||||
thung: 3.33333
|
||||
thong: 1
|
||||
thrum: 1234567
|
||||
thang: 2.2
|
||||
thung: 3.33
|
||||
thyme: true
|
||||
thugg: true
|
||||
thurr: 12345
|
||||
@@ -289,7 +289,7 @@ func TestNewNodeBuilder_Extensions(t *testing.T) {
|
||||
node := nb.Render()
|
||||
|
||||
data, _ := yaml.Marshal(node)
|
||||
assert.Len(t, data, 51)
|
||||
assert.Len(t, data, 49)
|
||||
}
|
||||
|
||||
func TestNewNodeBuilder_LowValueNode(t *testing.T) {
|
||||
@@ -308,7 +308,7 @@ func TestNewNodeBuilder_LowValueNode(t *testing.T) {
|
||||
|
||||
data, _ := yaml.Marshal(node)
|
||||
|
||||
assert.Len(t, data, 51)
|
||||
assert.Len(t, data, 49)
|
||||
}
|
||||
|
||||
func TestNewNodeBuilder_NoValue(t *testing.T) {
|
||||
@@ -396,7 +396,7 @@ func TestNewNodeBuilder_Float64(t *testing.T) {
|
||||
t1 := new(test1)
|
||||
nb := NewNodeBuilder(t1, t1)
|
||||
p := utils.CreateEmptyMapNode()
|
||||
nodeEnty := NodeEntry{Tag: "p", Value: 1234.232323, Key: "p"}
|
||||
nodeEnty := NodeEntry{Tag: "p", Value: 1234.232323, Key: "p", StringValue: "1234.232323"}
|
||||
node := nb.AddYAMLNode(p, &nodeEnty)
|
||||
assert.NotNil(t, node)
|
||||
assert.Len(t, node.Content, 2)
|
||||
@@ -611,7 +611,7 @@ func TestNewNodeBuilder_ExtensionMap(t *testing.T) {
|
||||
|
||||
data, _ := yaml.Marshal(node)
|
||||
|
||||
assert.Len(t, data, 62)
|
||||
assert.Len(t, data, 60)
|
||||
}
|
||||
|
||||
func TestNewNodeBuilder_MapKeyHasValueThatHasValueMismatch(t *testing.T) {
|
||||
|
||||
@@ -170,7 +170,7 @@ func TestExtractSpecInfo_OpenAPI31(t *testing.T) {
|
||||
assert.Nil(t, e)
|
||||
assert.Equal(t, OpenApi3, r.SpecType)
|
||||
assert.Equal(t, "3.1", r.Version)
|
||||
assert.Contains(t, r.APISchema, "https://spec.openapis.org/oas/3.1/schema/2022-02-27")
|
||||
assert.Contains(t, r.APISchema, "https://spec.openapis.org/oas/3.1/schema/2022-10-07")
|
||||
}
|
||||
|
||||
func TestExtractSpecInfo_OpenAPIFalse(t *testing.T) {
|
||||
|
||||
@@ -665,5 +665,5 @@ func ExampleNewDocument_modifyAndReRender() {
|
||||
fmt.Printf("There were %d original paths. There are now %d paths in the document\n", originalPaths, newPaths)
|
||||
fmt.Printf("The original spec had %d bytes, the new one has %d\n", len(petstore), len(rawBytes))
|
||||
// Output: There were 13 original paths. There are now 14 paths in the document
|
||||
//The original spec had 31143 bytes, the new one has 27857
|
||||
//The original spec had 31143 bytes, the new one has 27841
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user