55 lines
1.7 KiB
Go
55 lines
1.7 KiB
Go
package openapi
|
|
|
|
import (
|
|
"encoding/json"
|
|
"reflect"
|
|
"strings"
|
|
"testing"
|
|
|
|
"git.yuntuo.hk/k2safe/go-omni/httpx"
|
|
)
|
|
|
|
func TestSchemaAdditionalPropertiesAndExactNumbers(t *testing.T) {
|
|
for _, text := range []string{
|
|
`{"type":"object","additionalProperties":false}`,
|
|
`{"type":"object","additionalProperties":true}`,
|
|
`{"type":"object","additionalProperties":{"type":"string"}}`,
|
|
`{"type":"integer","default":18446744073709551615,"enum":[18446744073709551615]}`,
|
|
`{"$ref":"#/components/schemas/Node"}`,
|
|
`{"description":"Node","allOf":[{"anyOf":[{"$ref":"#/components/schemas/Node"},{"type":"object","nullable":true,"enum":[null]}]}]}`,
|
|
} {
|
|
var s Schema
|
|
if err := json.Unmarshal([]byte(text), &s); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
b, err := json.Marshal(s)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if strings.Contains(text, "18446744073709551615") && !strings.Contains(string(b), "18446744073709551615") {
|
|
t.Fatalf("lost precision: %s", b)
|
|
}
|
|
var roundTrip Schema
|
|
if err = json.Unmarshal(b, &roundTrip); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !reflect.DeepEqual(s, roundTrip) {
|
|
t.Fatalf("schema round trip changed: %s", b)
|
|
}
|
|
}
|
|
api, _ := New("Exact numbers", "1")
|
|
s := &Schema{Type: "integer", Enum: []any{json.Number("18446744073709551615")}}
|
|
err := api.Handle(discardSchemaRoutes{}, Operation{Method: "get", Path: "/id", OperationID: "readId", Responses: map[int]Response{200: {Description: "ok", Schema: s}}}, func(*httpx.Context) error { return nil })
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
data, _ := api.JSON()
|
|
if !strings.Contains(string(data), "18446744073709551615") {
|
|
t.Fatal(string(data))
|
|
}
|
|
}
|
|
|
|
type discardSchemaRoutes struct{}
|
|
|
|
func (discardSchemaRoutes) Handle(string, string, httpx.Handler, ...httpx.Middleware) {}
|