Skip to content

Commit

Permalink
Merge pull request #18 from kaleido-io/pretranslated-description
Browse files Browse the repository at this point in the history
Add PreTranslatedDescription field to API Route
  • Loading branch information
peterbroadhurst authored Jun 13, 2022
2 parents c528557 + eeacb05 commit f598736
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
11 changes: 8 additions & 3 deletions pkg/ffapi/openapi3.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,10 +304,15 @@ func (sg *SwaggerGen) AddParam(ctx context.Context, op *openapi3.Operation, in,
}

func (sg *SwaggerGen) addRoute(ctx context.Context, doc *openapi3.T, route *Route) {
var routeDescription string
pi := sg.getPathItem(doc, route.Path)
routeDescription := i18n.Expand(ctx, route.Description)
if routeDescription == "" && sg.options.PanicOnMissingDescription {
log.Panicf(i18n.NewError(ctx, i18n.MsgRouteDescriptionMissing, route.Name).Error())
if route.PreTranslatedDescription != "" {
routeDescription = route.PreTranslatedDescription
} else {
routeDescription = i18n.Expand(ctx, route.Description)
if routeDescription == "" && sg.options.PanicOnMissingDescription {
log.Panicf(i18n.NewError(ctx, i18n.MsgRouteDescriptionMissing, route.Name).Error())
}
}
op := &openapi3.Operation{
Description: routeDescription,
Expand Down
24 changes: 22 additions & 2 deletions pkg/ffapi/openapi3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,6 @@ type TestStruct2 struct {

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

// var ExampleConf = i18n.FFC(language.AmericanEnglish, "config.conf.key1", "Test Config Key", "string")

var testRoutes = []*Route{
{
Name: "op1",
Expand Down Expand Up @@ -379,3 +377,25 @@ func TestPanicOnMissingRouteDescription(t *testing.T) {
}).Generate(context.Background(), routes)
})
}

func TestPreTranslatedRouteDescription(t *testing.T) {
routes := []*Route{
{
Name: "PostTagTest",
Path: "namespaces/{ns}/example1/test",
Method: http.MethodPost,
JSONInputValue: func() interface{} { return &TestInOutType{} },
JSONOutputValue: func() interface{} { return &TestInOutType{} },
JSONOutputCodes: []int{http.StatusOK},
PreTranslatedDescription: "this is a description",
},
}
swagger := NewSwaggerGen(&Options{
Title: "UnitTest",
Version: "1.0",
BaseURL: "http://localhost:12345/api/v1",
}).Generate(context.Background(), routes)
assert.NotNil(t, swagger.Paths["/namespaces/{ns}/example1/test"].Post.RequestBody.Value)
description := swagger.Paths["/namespaces/{ns}/example1/test"].Post.Description
assert.Equal(t, "this is a description", description)
}
2 changes: 2 additions & 0 deletions pkg/ffapi/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ type Route struct {
Method string
// Description is a message key to a translatable description of the operation
Description i18n.MessageKey
// PreTranslatedDescription is a string describing the operation - used for programmatically generated routes where a built-in string translation is not available
PreTranslatedDescription string
// JSONInputValue is a function that returns a pointer to a structure to take JSON input
JSONInputValue func() interface{}
// JSONInputMask are fields that aren't available for users to supply on input
Expand Down

0 comments on commit f598736

Please sign in to comment.