mirror of
https://github.com/LukeHagar/libopenapi.git
synced 2025-12-07 04:20:14 +00:00
Fixed JSON rendering for example object
This commit is contained in:
@@ -60,7 +60,9 @@ func (e *Example) MarshalYAML() (interface{}, error) {
|
|||||||
// MarshalJSON will marshal this into a JSON byte slice
|
// MarshalJSON will marshal this into a JSON byte slice
|
||||||
func (e *Example) MarshalJSON() ([]byte, error) {
|
func (e *Example) MarshalJSON() ([]byte, error) {
|
||||||
var g map[string]any
|
var g map[string]any
|
||||||
e.Value.Decode(&g)
|
nb := high.NewNodeBuilder(e, e.low)
|
||||||
|
r := nb.Render()
|
||||||
|
r.Decode(&g)
|
||||||
return json.Marshal(g)
|
return json.Marshal(g)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ package base
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
@@ -53,6 +54,21 @@ x-hack: code`
|
|||||||
// render the example as YAML
|
// render the example as YAML
|
||||||
rendered, _ := highExample.Render()
|
rendered, _ := highExample.Render()
|
||||||
assert.Equal(t, yml, strings.TrimSpace(string(rendered)))
|
assert.Equal(t, yml, strings.TrimSpace(string(rendered)))
|
||||||
|
|
||||||
|
// render the example as JSON
|
||||||
|
var err error
|
||||||
|
rendered, err = json.Marshal(highExample)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
var j map[string]any
|
||||||
|
_ = json.Unmarshal(rendered, &j)
|
||||||
|
|
||||||
|
assert.Equal(t, "an example", j["summary"])
|
||||||
|
assert.Equal(t, "something more", j["description"])
|
||||||
|
assert.Equal(t, "https://pb33f.io", j["externalValue"])
|
||||||
|
assert.Equal(t, "code", j["x-hack"])
|
||||||
|
assert.Equal(t, "a thing", j["value"])
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestExtractExamples(t *testing.T) {
|
func TestExtractExamples(t *testing.T) {
|
||||||
|
|||||||
Reference in New Issue
Block a user