mirror of
https://github.com/LukeHagar/libopenapi.git
synced 2025-12-09 12:37:49 +00:00
Added mutate command to low level API
This simple method gives the low API a super powerful and simple way to mutate the value of any node, which is then reflected in the root node, can than be serialized again and, voila! now our spec is editable.
This commit is contained in:
@@ -106,3 +106,63 @@ paths:
|
||||
assert.Len(t, docErr, 1)
|
||||
assert.Nil(t, v3Doc)
|
||||
}
|
||||
|
||||
func TestDocument_Serialize_Error(t *testing.T) {
|
||||
doc := new(Document) // not how this should be instantiated.
|
||||
_, err := doc.Serialize()
|
||||
assert.Error(t, err)
|
||||
}
|
||||
|
||||
func TestDocument_Serialize(t *testing.T) {
|
||||
|
||||
yml := `openapi: 3.0
|
||||
info:
|
||||
title: The magic API
|
||||
`
|
||||
doc, _ := NewDocument([]byte(yml))
|
||||
serial, err := doc.Serialize()
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, yml, string(serial))
|
||||
}
|
||||
|
||||
func TestDocument_Serialize_Modified(t *testing.T) {
|
||||
|
||||
yml := `openapi: 3.0
|
||||
info:
|
||||
title: The magic API
|
||||
`
|
||||
|
||||
ymlModified := `openapi: 3.0
|
||||
info:
|
||||
title: The magic API - but now, altered!
|
||||
`
|
||||
doc, _ := NewDocument([]byte(yml))
|
||||
|
||||
v3Doc, _ := doc.BuildV3Document()
|
||||
|
||||
v3Doc.Model.Info.GoLow().Title.Mutate("The magic API - but now, altered!")
|
||||
|
||||
serial, err := doc.Serialize()
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, ymlModified, string(serial))
|
||||
}
|
||||
|
||||
func TestDocument_Serialize_JSON_Modified(t *testing.T) {
|
||||
|
||||
json := `{ 'openapi': '3.0',
|
||||
'info': {
|
||||
'title': 'The magic API'
|
||||
}
|
||||
}
|
||||
`
|
||||
jsonModified := `{"info":{"title":"The magic API - but now, altered!"},"openapi":"3.0"}`
|
||||
doc, _ := NewDocument([]byte(json))
|
||||
|
||||
v3Doc, _ := doc.BuildV3Document()
|
||||
|
||||
v3Doc.Model.Info.GoLow().Title.Mutate("The magic API - but now, altered!")
|
||||
|
||||
serial, err := doc.Serialize()
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, jsonModified, string(serial))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user