Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
33 changes: 32 additions & 1 deletion pkg/ffapi/openapi3.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,10 @@
Description string
}

var customRegexRemoval = regexp.MustCompile(`{(\w+)\:[^}]+}`)
var (
customRegexRemoval = regexp.MustCompile(`{(\w+)\:[^}]+}`)

Check failure on line 78 in pkg/ffapi/openapi3.go

View workflow job for this annotation

GitHub Actions / build

File is not properly formatted (gofmt)
ffExtensionKeyRegexp = regexp.MustCompile(`^x-.+$`)
)

type SwaggerGen struct {
options *SwaggerGenOptions
Expand Down Expand Up @@ -184,10 +187,38 @@
return sg.ffTagHandler(ctx, route, name, tag, schema)
}

func (sg *SwaggerGen) applyFFExtensionsTag(ctx context.Context, schema *openapi3.Schema, tag string) error {
if tag == "" {
return nil
}
for _, extension := range strings.Split(tag, ",") {
kv := strings.SplitN(strings.TrimSpace(extension), "=", 2)
if len(kv) != 2 {
return i18n.NewError(ctx, i18n.MsgFFExtensionsInvalid, extension)
}
key := strings.TrimSpace(kv[0])
if !ffExtensionKeyRegexp.MatchString(key) {
return i18n.NewError(ctx, i18n.MsgFFExtensionsKeyInvalid, key)
}
val := strings.TrimSpace(kv[1])
if schema.Extensions == nil {
schema.Extensions = make(map[string]interface{})
}
schema.Extensions[key] = val
}
return nil
}


func (sg *SwaggerGen) ffTagHandler(ctx context.Context, route *Route, name string, tag reflect.StructTag, schema *openapi3.Schema) error {
if ffEnum := tag.Get("ffenum"); ffEnum != "" {
schema.Enum = fftypes.FFEnumValues(ffEnum)
}
if ffExtensions := tag.Get("ffextensions"); ffExtensions != "" {
if err := sg.applyFFExtensionsTag(ctx, schema, ffExtensions); err != nil {
return err
}
}
if sg.isTrue(tag.Get("ffexclude")) {
return &openapi3gen.ExcludeSchemaSentinel{}
}
Expand Down
93 changes: 93 additions & 0 deletions pkg/ffapi/openapi3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,24 @@ type TestStruct2 struct {
JSONAny1 *fftypes.JSONAny `ffstruct:"ut1" json:"jsonAny1,omitempty"`
}

type TestExtensions struct {
String1 string `ffstruct:"ut1" json:"string1" ffextensions:"x-key1=value1"`
String2 string `ffstruct:"ut1" json:"string2" ffextensions:"x-key1=value1,x-key2=value2"`
String3 string `ffstruct:"ut1" json:"string3" ffextensions:""`
}

type TestExtensionsBad1 struct {
String1 string `ffstruct:"ut1" json:"string1" ffextensions:"x-key1"`
}

type TestExtensionsBad2 struct {
String1 string `ffstruct:"ut1" json:"string1" ffextensions:"key1=value1,key2=value2"`
}

type TestExtensionsBad3 struct {
String1 string `ffstruct:"ut1" json:"string1" ffextensions:"x-=value1"`
}

var ExampleDesc = i18n.FFM(language.AmericanEnglish, "TestKey", "Test Description")

var example2TagName = "Example 2"
Expand Down Expand Up @@ -193,6 +211,18 @@ var testRoutes = []*Route{
},
Tag: example2TagName,
},
{
Name: "op8",
Path: "example8",
Method: http.MethodGet,
PathParams: nil,
QueryParams: nil,
Description: ExampleDesc,
JSONInputValue: func() interface{} { return nil},
JSONOutputValue: func() interface{} { return &TestExtensions{} },
JSONOutputCodes: []int{http.StatusOK},
},

}

type TestInOutType struct {
Expand Down Expand Up @@ -578,3 +608,66 @@ func TestExcludeFromOpenAPI(t *testing.T) {
err := doc.Validate(context.Background())
assert.NoError(t, err)
}

func TestExtensionsBad1Fail(t *testing.T) {
routes := []*Route{
{
Name: "bad1",
Path: "extensions",
Method: http.MethodGet,
JSONInputValue: func() interface{} { return nil },
JSONOutputValue: func() interface{} { return &TestExtensionsBad1{} },
JSONOutputCodes: []int{http.StatusOK},
},
}

assert.PanicsWithValue(t, "invalid schema: FF00258: Invalid extension 'x-key1' - extensions must follow the format 'x-<name>=<value>'", func() {
_ = NewSwaggerGen(&SwaggerGenOptions{
Title: "UnitTest",
Version: "1.0",
BaseURL: "http://localhost:12345/api/v1",
}).Generate(context.Background(), routes)
})
}

func TestExtensionsBad2Fail(t *testing.T) {
routes := []*Route{
{
Name: "bad2",
Path: "extensions",
Method: http.MethodGet,
JSONInputValue: func() interface{} { return nil },
JSONOutputValue: func() interface{} { return &TestExtensionsBad2{} },
JSONOutputCodes: []int{http.StatusOK},
},
}

assert.PanicsWithValue(t, "invalid schema: FF00259: Invalid extension key 'key1' - extension keys must follow the format 'x-<name>'", func() {
_ = NewSwaggerGen(&SwaggerGenOptions{
Title: "UnitTest",
Version: "1.0",
BaseURL: "http://localhost:12345/api/v1",
}).Generate(context.Background(), routes)
})
}

func TestExtensionsBad3Fail(t *testing.T) {
routes := []*Route{
{
Name: "bad3",
Path: "extensions",
Method: http.MethodGet,
JSONInputValue: func() interface{} { return nil },
JSONOutputValue: func() interface{} { return &TestExtensionsBad3{} },
JSONOutputCodes: []int{http.StatusOK},
},
}

assert.PanicsWithValue(t, "invalid schema: FF00259: Invalid extension key 'x-' - extension keys must follow the format 'x-<name>'", func() {
_ = NewSwaggerGen(&SwaggerGenOptions{
Title: "UnitTest",
Version: "1.0",
BaseURL: "http://localhost:12345/api/v1",
}).Generate(context.Background(), routes)
})
}
2 changes: 2 additions & 0 deletions pkg/i18n/en_base_error_messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,4 +192,6 @@ var (
MsgRoutePathNotStartWithSlash = ffe("FF00255", "Route path '%s' must not start with '/'")
MsgMethodNotAllowed = ffe("FF00256", "Method not allowed", http.StatusMethodNotAllowed)
MsgInvalidLogLevel = ffe("FF00257", "Invalid log level: '%s'", http.StatusBadRequest)
MsgFFExtensionsInvalid = ffe("FF00258", "Invalid extension '%s' - extensions must follow the format 'x-<name>=<value>'", http.StatusBadRequest)
MsgFFExtensionsKeyInvalid = ffe("FF00259", "Invalid extension key '%s' - extension keys must follow the format 'x-<name>'", http.StatusBadRequest)
)
Loading