Skip to content

Commit

Permalink
chore: add parseStructTags tests to ensure warning messages are logged
Browse files Browse the repository at this point in the history
  • Loading branch information
dylanhitt committed Dec 29, 2024
1 parent 20b8c4b commit ce494eb
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion openapi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@ package fuego

import (
"bytes"
"encoding/xml"
"fmt"
"io"
"log/slog"
"net/http"
"net/http/httptest"
"os"
"testing"

"github.com/getkin/kin-openapi/openapi3"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gotest.tools/v3/assert"
"github.com/thejerf/slogassert"
)

type MyStruct struct {
Expand All @@ -36,6 +39,11 @@ type MyOutputStruct struct {
Quantity int `json:"quantity"`
}

type InvalidExample struct {
XMLName xml.Name `xml:"TestStruct"`
MyInt int `json:"e" example:"isString" validate:"min=isString,max=isString" `
}

type testCaseForTagType[V any] struct {
name string
description string
Expand Down Expand Up @@ -248,6 +256,19 @@ func Test_tagFromType(t *testing.T) {
assert.Equal(t, float64(3), *c.Value.Min)
assert.Equal(t, float64(10), *c.Value.Max)
})

t.Run("ensure warnings", func(t *testing.T) {
handler := slogassert.New(t, slog.LevelWarn, nil)
s := NewServer(
WithLogHandler(handler),
)

SchemaTagFromType(s.OpenAPI, InvalidExample{})
handler.AssertMessage("Property not found in schema")
handler.AssertMessage("Example might be incorrect (should be integer)")
handler.AssertMessage("Max might be incorrect (should be integer)")
handler.AssertMessage("Min might be incorrect (should be integer)")
})
}

func TestServer_generateOpenAPI(t *testing.T) {
Expand Down

0 comments on commit ce494eb

Please sign in to comment.