Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ func (b *Builder) GenerateSwagger(ctx context.Context, callbacksObject shared.Ch
}

// Query parameters
err = b.generateQueryParametersDoc(ctx, &swagger, op, typ)
err = b.generateQueryParametersDoc(ctx, &swagger, op, typ, callbacksObject)
if err != nil {
return nil, err
}
Expand Down
10 changes: 9 additions & 1 deletion builder/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/schmurfy/chipi/shared"
)

func (b *Builder) generateQueryParametersDoc(ctx context.Context, swagger *openapi3.T, op *openapi3.Operation, requestObjectType reflect.Type) error {
func (b *Builder) generateQueryParametersDoc(ctx context.Context, swagger *openapi3.T, op *openapi3.Operation, requestObjectType reflect.Type, callbacksObject shared.ChipiCallbacks) error {
pathField, found := requestObjectType.FieldByName("Query")
if !found {
return nil
Expand Down Expand Up @@ -41,6 +41,14 @@ func (b *Builder) generateQueryParametersDoc(ctx context.Context, swagger *opena
name = shared.ToSnakeCase(field.Name)
}

if parsedTag.CastName != nil {
fieldSchema.Value, _ = callbacksObject.SchemaResolver(shared.AttributeInfo{}, *parsedTag.CastName, field.Type, func(t reflect.Type, fieldInfo shared.AttributeInfo) (*openapi3.SchemaRef, error) {
return fieldSchema, nil
})
op.AddParameter(openapi3.NewQueryParameter(name).WithSchema(fieldSchema.Value))
continue
}

param := openapi3.NewQueryParameter(name)

if (fieldSchema.Ref != "") || (fieldSchema.Value.Type.Includes(openapi3.TypeObject)) {
Expand Down
24 changes: 23 additions & 1 deletion builder/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/franela/goblin"
"github.com/getkin/kin-openapi/openapi3"
"github.com/go-chi/chi/v5"
"github.com/schmurfy/chipi/shared"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand All @@ -21,6 +22,21 @@ type testQueryRequest struct {
PascalCaseWithJsonTag string `json:"PascalCaseWithJsonTag"`
CamelCaseWithJsonTag string `json:"camelCaseWithJsonTag"`
PascalCaseWithNameTag string `name:"PascalCaseWithNameTag"`
WithChipiAs string `json:"with_chipi_as" chipi:"as:namespace"`
}
}

var _ shared.SchemaResolverInterface = (*TestSchemaResolver)(nil)

type TestSchemaResolver struct{}

func (e *TestSchemaResolver) SchemaResolver(fieldInfo shared.AttributeInfo, castName string, fieldTyp reflect.Type, newSchemaCallbackType shared.GenerateSchemaCallbackType) (*openapi3.Schema, bool) {
switch castName {
case "namespace":
namespace := []any{"default", "kube-system", "custom-namespace"}
return openapi3.NewStringSchema().WithEnum(namespace...), false
default:
return nil, false
}
}

Expand Down Expand Up @@ -48,7 +64,7 @@ func TestQueryParams(t *testing.T) {

g.BeforeEach(func() {
tt := reflect.TypeOf(testQueryRequest{})
err := b.generateQueryParametersDoc(ctx, b.swagger, &op, tt)
err := b.generateQueryParametersDoc(ctx, b.swagger, &op, tt, shared.NewChipiCallbacks(&TestSchemaResolver{}))
require.NoError(g, err)
})

Expand All @@ -59,6 +75,7 @@ func TestQueryParams(t *testing.T) {
var paramPascalCaseWithJsonTag *openapi3.Parameter
var paramCamelCaseWithJsonTag *openapi3.Parameter
var paramPascalCaseWithNameTag *openapi3.Parameter
var paramWithChipiAs *openapi3.Parameter

g.It("test parsing json tag", func() {
param = op.Parameters.GetByInAndName("query", "name")
Expand All @@ -84,6 +101,11 @@ func TestQueryParams(t *testing.T) {
paramPascalCaseWithNameTag = op.Parameters.GetByInAndName("query", "pascal_case_with_name_tag")
require.NotNil(g, paramPascalCaseWithNameTag)
require.Equal(g, "pascal_case_with_name_tag", paramPascalCaseWithNameTag.Name)

paramWithChipiAs = op.Parameters.GetByInAndName("query", "with_chipi_as")
require.NotNil(g, paramWithChipiAs)
require.Equal(g, "with_chipi_as", paramWithChipiAs.Name)
assert.Equal(g, []any{"default", "kube-system", "custom-namespace"}, paramWithChipiAs.Schema.Value.Enum)
})

g.It("should extract [required]", func() {
Expand Down
Loading