openapi: reference shared parameters

This commit is contained in:
Dr. Stefan Schimanski 2023-05-23 15:57:15 +02:00
parent 900237fada
commit 169abcc039
No known key found for this signature in database
GPG Key ID: 4C68E0F19F95EC33
3 changed files with 22 additions and 9 deletions

View File

@ -520,7 +520,7 @@ func TestCRDRouteParameterBuilder(t *testing.T) {
hasNamespaceParam := false hasNamespaceParam := false
hasNameParam := false hasNameParam := false
for _, param := range path.Parameters { for _, param := range path.Parameters {
if param.In == "path" && param.Name == "namespace" { if strings.HasPrefix(param.Ref.String(), "#/parameters/namespace-") {
hasNamespaceParam = true hasNamespaceParam = true
} }
if param.In == "path" && param.Name == "name" { if param.In == "path" && param.Name == "name" {

View File

@ -35,14 +35,20 @@ func MergeSpecs(staticSpec *spec.Swagger, crdSpecs ...*spec.Swagger) (*spec.Swag
// create shallow copy of staticSpec, but replace paths and definitions because we modify them. // create shallow copy of staticSpec, but replace paths and definitions because we modify them.
specToReturn := *staticSpec specToReturn := *staticSpec
if staticSpec.Definitions != nil { if staticSpec.Definitions != nil {
specToReturn.Definitions = spec.Definitions{} specToReturn.Definitions = make(spec.Definitions, len(staticSpec.Definitions))
for k, s := range staticSpec.Definitions { for k, s := range staticSpec.Definitions {
specToReturn.Definitions[k] = s specToReturn.Definitions[k] = s
} }
} }
if staticSpec.Parameters != nil {
specToReturn.Parameters = make(map[string]spec.Parameter, len(staticSpec.Parameters))
for k, s := range staticSpec.Parameters {
specToReturn.Parameters[k] = s
}
}
if staticSpec.Paths != nil { if staticSpec.Paths != nil {
specToReturn.Paths = &spec.Paths{ specToReturn.Paths = &spec.Paths{
Paths: map[string]spec.PathItem{}, Paths: make(map[string]spec.PathItem, len(staticSpec.Paths.Paths)),
} }
for k, p := range staticSpec.Paths.Paths { for k, p := range staticSpec.Paths.Paths {
specToReturn.Paths.Paths[k] = p specToReturn.Paths.Paths[k] = p
@ -58,13 +64,13 @@ func MergeSpecs(staticSpec *spec.Swagger, crdSpecs ...*spec.Swagger) (*spec.Swag
// The static spec has the highest priority. Resolve conflicts to prevent user-defined // The static spec has the highest priority. Resolve conflicts to prevent user-defined
// CRDs potentially overlapping the built-in apiextensions API // CRDs potentially overlapping the built-in apiextensions API
if err := aggregator.MergeSpecsIgnorePathConflict(&specToReturn, crdSpec); err != nil { if err := aggregator.MergeSpecsIgnorePathConflictRenamingDefinitionsAndParameters(&specToReturn, crdSpec); err != nil {
return nil, err return nil, err
} }
return &specToReturn, nil return &specToReturn, nil
} }
// mergeSpec copies paths and definitions from source to dest, mutating dest, but not source. // mergeSpec copies paths, parameters and definitions from source to dest, mutating dest, but not source.
// We assume that conflicts do not matter. // We assume that conflicts do not matter.
func mergeSpec(dest, source *spec.Swagger) { func mergeSpec(dest, source *spec.Swagger) {
if source == nil || source.Paths == nil { if source == nil || source.Paths == nil {
@ -75,13 +81,19 @@ func mergeSpec(dest, source *spec.Swagger) {
} }
for k, v := range source.Definitions { for k, v := range source.Definitions {
if dest.Definitions == nil { if dest.Definitions == nil {
dest.Definitions = spec.Definitions{} dest.Definitions = make(spec.Definitions, len(source.Definitions))
} }
dest.Definitions[k] = v dest.Definitions[k] = v
} }
for k, v := range source.Parameters {
if dest.Parameters == nil {
dest.Parameters = make(map[string]spec.Parameter, len(source.Parameters))
}
dest.Parameters[k] = v
}
for k, v := range source.Paths.Paths { for k, v := range source.Paths.Paths {
if dest.Paths.Paths == nil { if dest.Paths.Paths == nil {
dest.Paths.Paths = map[string]spec.PathItem{} dest.Paths.Paths = make(map[string]spec.PathItem, len(source.Paths.Paths))
} }
dest.Paths.Paths[k] = v dest.Paths.Paths[k] = v
} }

View File

@ -191,11 +191,12 @@ func (s *specAggregator) buildOpenAPISpec() (specToReturn *spec.Swagger, err err
if specToReturn == nil { if specToReturn == nil {
specToReturn = &spec.Swagger{} specToReturn = &spec.Swagger{}
*specToReturn = *specInfo.spec *specToReturn = *specInfo.spec
// Paths and Definitions are set by MergeSpecsIgnorePathConflict // Paths, Definitions and parameters are set by MergeSpecsIgnorePathConflict
specToReturn.Paths = nil specToReturn.Paths = nil
specToReturn.Definitions = nil specToReturn.Definitions = nil
specToReturn.Parameters = nil
} }
if err := aggregator.MergeSpecsIgnorePathConflict(specToReturn, specInfo.spec); err != nil { if err := aggregator.MergeSpecsIgnorePathConflictRenamingDefinitionsAndParameters(specToReturn, specInfo.spec); err != nil {
return nil, err return nil, err
} }
} }