Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLOUDP-225849: add merging logic #10

Merged
merged 10 commits into from Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions tools/cli/go.mod
Expand Up @@ -9,6 +9,7 @@ require (
github.com/stretchr/testify v1.9.0
github.com/tufin/oasdiff v1.10.11
go.uber.org/mock v0.4.0
golang.org/x/exp v0.0.0-20240119083558-1b970713d09a
)

require (
Expand Down
25 changes: 25 additions & 0 deletions tools/cli/internal/openapi/errors/param_conflict_error.go
andreaangiolillo marked this conversation as resolved.
Show resolved Hide resolved
@@ -0,0 +1,25 @@
// Copyright 2024 MongoDB Inc
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package errors

import "fmt"

type ParamConflictError struct {
Entry string
}

func (e ParamConflictError) Error() string {
return fmt.Sprintf("there was a conflict with the Param component: %s", e.Entry)
}
25 changes: 25 additions & 0 deletions tools/cli/internal/openapi/errors/response_conflict_error.go
@@ -0,0 +1,25 @@
// Copyright 2024 MongoDB Inc
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package errors

import "fmt"

type ResponseConflictError struct {
Entry string
}

func (e ResponseConflictError) Error() string {
return fmt.Sprintf("there was a conflict on a the Response component: %s", e.Entry)
}
25 changes: 25 additions & 0 deletions tools/cli/internal/openapi/errors/schema_conflict_error.go
@@ -0,0 +1,25 @@
// Copyright 2024 MongoDB Inc
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package errors

import "fmt"

type SchemaConflictError struct {
Entry string
}

func (e SchemaConflictError) Error() string {
return fmt.Sprintf("there was a conflict on a Schema component: %s", e.Entry)
}
26 changes: 26 additions & 0 deletions tools/cli/internal/openapi/errors/tag_conflict_error.go
@@ -0,0 +1,26 @@
// Copyright 2024 MongoDB Inc
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package errors

import "fmt"

type TagConflictError struct {
Entry string
Description string
}

func (e TagConflictError) Error() string {
return fmt.Sprintf("there was a conflict with the Tag '%s' with the description: '%s'", e.Entry, e.Description)
andreaangiolillo marked this conversation as resolved.
Show resolved Hide resolved
}
197 changes: 196 additions & 1 deletion tools/cli/internal/openapi/oasdiff.go
Expand Up @@ -15,6 +15,8 @@
package openapi

import (
"log"

"github.com/mongodb/openapi/tools/cli/internal/openapi/errors"
"github.com/tufin/oasdiff/diff"
"github.com/tufin/oasdiff/load"
Expand All @@ -29,12 +31,38 @@ type OasDiff struct {
}

func (o OasDiff) mergeSpecIntoBase() error {
return o.mergePaths()
if o.external == nil || o.external.Spec == nil {
return nil
}

if o.base == nil || o.base.Spec == nil {
*o.base = *o.external
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of pointer here make the receiver of the method a pointer, this is the preferred practice, also it's a code smell to mix pointer and non pointer receivers so they all should be pointers then

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

return nil
}

if err := o.mergePaths(); err != nil {
return err
}

if err := o.mergeTags(); err != nil {
return err
}

return o.mergeComponents()
}

func (o OasDiff) mergePaths() error {
pathsToMerge := o.external.Spec.Paths
if pathsToMerge == nil || pathsToMerge.Len() == 0 {
return nil
}

basePaths := o.base.Spec.Paths
if basePaths == nil || basePaths.Len() == 0 {
o.base.Spec.Paths = pathsToMerge
return nil
}

for k, v := range pathsToMerge.Map() {
if ok := basePaths.Value(k); ok == nil {
basePaths.Set(k, v)
Expand All @@ -48,3 +76,170 @@ func (o OasDiff) mergePaths() error {
o.base.Spec.Paths = basePaths
return nil
}

func (o OasDiff) mergeTags() error {
tagsToMerge := o.external.Spec.Tags
if len(tagsToMerge) == 0 {
return nil
}

baseTags := o.base.Spec.Tags
if len(baseTags) == 0 {
o.base.Spec.Tags = tagsToMerge
return nil
}

tagsSet := make(map[string]bool)
andreaangiolillo marked this conversation as resolved.
Show resolved Hide resolved
for _, v := range baseTags {
tagsSet[v.Name] = true
}

for _, v := range tagsToMerge {
if _, ok := tagsSet[v.Name]; !ok {
baseTags = append(baseTags, v)
} else {
return errors.TagConflictError{
Entry: v.Name,
Description: v.Description,
}
}
}

o.base.Spec.Tags = baseTags
return nil
}

func (o OasDiff) mergeComponents() error {
if o.external.Spec.Components == nil {
return nil
}

if o.base.Spec.Components == nil {
o.base.Spec.Components = o.external.Spec.Components
return nil
}

if err := o.mergeParameters(); err != nil {
return err
}

if err := o.mergeResponses(); err != nil {
return err
}

return o.mergeSchemas()
}

func (o OasDiff) mergeParameters() error {
externalSpecParams := o.external.Spec.Components.Parameters
if len(externalSpecParams) == 0 {
return nil
}

baseParams := o.base.Spec.Components.Parameters
if len(baseParams) == 0 {
o.base.Spec.Components.Parameters = externalSpecParams
return nil
}
for k, v := range externalSpecParams {
if _, ok := baseParams[k]; !ok {
baseParams[k] = v
} else {
if o.areParamsIdentical(k) {
// if the responses are the same, we skip
log.Printf("\nWe silently resolved the conflict with the response '%s' because the definition was identical.\n", k)
andreaangiolillo marked this conversation as resolved.
Show resolved Hide resolved
continue
}

// The params have the same name but different definitions
return errors.ParamConflictError{
Entry: k,
}
}
}

o.base.Spec.Components.Parameters = baseParams
return nil
}

func (o OasDiff) mergeResponses() error {
extResponses := o.external.Spec.Components.Responses
if len(extResponses) == 0 {
return nil
}

baseResponses := o.base.Spec.Components.Responses
if len(baseResponses) == 0 {
o.base.Spec.Components.Responses = extResponses
return nil
}

for k, v := range extResponses {
if _, ok := baseResponses[k]; !ok {
baseResponses[k] = v
} else {
if o.areResponsesIdentical(k) {
// if the params are the same, we skip
log.Printf("\nWe silently resolved the conflict with the params '%s' because the definition was identical.\n", k)
continue
}

// The responses have the same name but different definitions
return errors.ResponseConflictError{
Entry: k,
}
}
}

o.base.Spec.Components.Responses = baseResponses
return nil
}

func (o OasDiff) mergeSchemas() error {
extSchemas := o.external.Spec.Components.Schemas
baseSchemas := o.base.Spec.Components.Schemas

for k, schemaToMerge := range extSchemas {
if _, ok := baseSchemas[k]; !ok {
baseSchemas[k] = schemaToMerge
} else {
if o.areSchemaIdentical(k) {
// if the schemas are the same, we skip
log.Printf("\nWe silently resolved the conflict with the schemas '%s' because the definition was identical.\n", k)
continue
}

// The schemas have the same name but different definitions
return errors.SchemaConflictError{
Entry: k,
}
}
}

o.base.Spec.Components.Schemas = baseSchemas
return nil
}

func (o OasDiff) areParamsIdentical(paramName string) bool {
if _, ok := o.specDiff.ParametersDiff.Modified[paramName]; !ok {
return true
}

return false
andreaangiolillo marked this conversation as resolved.
Show resolved Hide resolved
}

func (o OasDiff) areResponsesIdentical(name string) bool {
if _, ok := o.specDiff.ResponsesDiff.Modified[name]; !ok {
return true
}

return false
}

func (o OasDiff) areSchemaIdentical(name string) bool {
if _, ok := o.specDiff.SchemasDiff.Modified[name]; !ok {
return true
}

return false
}