Skip to content

Commit f598736

Browse files
Merge pull request #18 from kaleido-io/pretranslated-description
Add PreTranslatedDescription field to API Route
2 parents c528557 + eeacb05 commit f598736

File tree

3 files changed

+32
-5
lines changed

3 files changed

+32
-5
lines changed

pkg/ffapi/openapi3.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -304,10 +304,15 @@ func (sg *SwaggerGen) AddParam(ctx context.Context, op *openapi3.Operation, in,
304304
}
305305

306306
func (sg *SwaggerGen) addRoute(ctx context.Context, doc *openapi3.T, route *Route) {
307+
var routeDescription string
307308
pi := sg.getPathItem(doc, route.Path)
308-
routeDescription := i18n.Expand(ctx, route.Description)
309-
if routeDescription == "" && sg.options.PanicOnMissingDescription {
310-
log.Panicf(i18n.NewError(ctx, i18n.MsgRouteDescriptionMissing, route.Name).Error())
309+
if route.PreTranslatedDescription != "" {
310+
routeDescription = route.PreTranslatedDescription
311+
} else {
312+
routeDescription = i18n.Expand(ctx, route.Description)
313+
if routeDescription == "" && sg.options.PanicOnMissingDescription {
314+
log.Panicf(i18n.NewError(ctx, i18n.MsgRouteDescriptionMissing, route.Name).Error())
315+
}
311316
}
312317
op := &openapi3.Operation{
313318
Description: routeDescription,

pkg/ffapi/openapi3_test.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,6 @@ type TestStruct2 struct {
6464

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

67-
// var ExampleConf = i18n.FFC(language.AmericanEnglish, "config.conf.key1", "Test Config Key", "string")
68-
6967
var testRoutes = []*Route{
7068
{
7169
Name: "op1",
@@ -379,3 +377,25 @@ func TestPanicOnMissingRouteDescription(t *testing.T) {
379377
}).Generate(context.Background(), routes)
380378
})
381379
}
380+
381+
func TestPreTranslatedRouteDescription(t *testing.T) {
382+
routes := []*Route{
383+
{
384+
Name: "PostTagTest",
385+
Path: "namespaces/{ns}/example1/test",
386+
Method: http.MethodPost,
387+
JSONInputValue: func() interface{} { return &TestInOutType{} },
388+
JSONOutputValue: func() interface{} { return &TestInOutType{} },
389+
JSONOutputCodes: []int{http.StatusOK},
390+
PreTranslatedDescription: "this is a description",
391+
},
392+
}
393+
swagger := NewSwaggerGen(&Options{
394+
Title: "UnitTest",
395+
Version: "1.0",
396+
BaseURL: "http://localhost:12345/api/v1",
397+
}).Generate(context.Background(), routes)
398+
assert.NotNil(t, swagger.Paths["/namespaces/{ns}/example1/test"].Post.RequestBody.Value)
399+
description := swagger.Paths["/namespaces/{ns}/example1/test"].Post.Description
400+
assert.Equal(t, "this is a description", description)
401+
}

pkg/ffapi/routes.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ type Route struct {
4242
Method string
4343
// Description is a message key to a translatable description of the operation
4444
Description i18n.MessageKey
45+
// PreTranslatedDescription is a string describing the operation - used for programmatically generated routes where a built-in string translation is not available
46+
PreTranslatedDescription string
4547
// JSONInputValue is a function that returns a pointer to a structure to take JSON input
4648
JSONInputValue func() interface{}
4749
// JSONInputMask are fields that aren't available for users to supply on input

0 commit comments

Comments
 (0)