Skip to content

Commit

Permalink
Merge pull request #439 from ryantxu/add-v3-post-process
Browse files Browse the repository at this point in the history
Support PostProcessSpec hook in V3
  • Loading branch information
k8s-ci-robot authored Dec 14, 2023
2 parents bf4651e + 63d04c0 commit ab13479
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
3 changes: 3 additions & 0 deletions pkg/builder3/openapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,9 @@ func BuildOpenAPISpecFromRoutes(webServices []common.RouteContainer, config *com
if err != nil {
return nil, err
}
if config.PostProcessSpec != nil {
return config.PostProcessSpec(a.spec)
}
return a.spec, nil
}

Expand Down
11 changes: 11 additions & 0 deletions pkg/builder3/openapi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,11 @@ func TestBuildOpenAPISpec(t *testing.T) {
Description: "Test API",
Version: "unversioned",
},
VendorExtensible: spec.VendorExtensible{
Extensions: map[string]any{
"hello": "world", // set from PostProcessSpec callback
},
},
},
Version: "3.0.0",
Paths: &spec3.Paths{
Expand All @@ -451,6 +456,12 @@ func TestBuildOpenAPISpec(t *testing.T) {
},
},
}
config.PostProcessSpec = func(s *spec3.OpenAPI) (*spec3.OpenAPI, error) {
s.Info.Extensions = map[string]any{
"hello": "world",
}
return s, nil
}
swagger, err := BuildOpenAPISpec(container.RegisteredWebServices(), config)
if !assert.NoError(err) {
return
Expand Down
3 changes: 3 additions & 0 deletions pkg/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,9 @@ type OpenAPIV3Config struct {
// It is an optional function to customize model names.
GetDefinitionName func(name string) (string, spec.Extensions)

// PostProcessSpec runs after the spec is ready to serve. It allows a final modification to the spec before serving.
PostProcessSpec func(*spec3.OpenAPI) (*spec3.OpenAPI, error)

// SecuritySchemes is list of all security schemes for OpenAPI service.
SecuritySchemes spec3.SecuritySchemes

Expand Down

0 comments on commit ab13479

Please sign in to comment.