Skip to content

Commit

Permalink
make isResourceInstanceEndPoint only return true if the path ends wit…
Browse files Browse the repository at this point in the history
…h path param or path param with trailing slash

- removed TestResourceInstanceRegex and use resourceInstanceRegex directly. If the
regex does not compile the unit tests will capture it. no need for separate
wrapper method for this.
  • Loading branch information
dikhan committed Oct 30, 2019
1 parent 221a337 commit 944abca
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 34 deletions.
21 changes: 2 additions & 19 deletions openapi/openapi_v2_spec_analyser.go
Original file line number Diff line number Diff line change
Expand Up @@ -448,23 +448,9 @@ func (specAnalyser *specV2Analyser) getBodyParameterBodySchema(resourceRootPostO
return nil, fmt.Errorf("POST operation contains an schema with no properties")
}

// resourceInstanceRegex loads up the regex specified in const resourceInstanceRegex
// If the regex is not able to compile the regular expression the function exists calling os.Exit(1) as
// there is the regex is completely busted
func (specAnalyser *specV2Analyser) resourceInstanceRegex() (*regexp.Regexp, error) {
r, err := regexp.Compile(resourceInstanceRegex)
if err != nil {
return nil, fmt.Errorf("an error occurred while compiling the resourceInstanceRegex regex '%s': %s", resourceInstanceRegex, err)
}
return r, nil
}

// isResourceInstanceEndPoint checks if the given path is of form /resource/{id}
func (specAnalyser *specV2Analyser) isResourceInstanceEndPoint(p string) (bool, error) {
r, err := specAnalyser.resourceInstanceRegex()
if err != nil {
return false, err
}
r, _ := regexp.Compile("^.*{.+}[\\/]?$")
return r.MatchString(p), nil
}

Expand All @@ -473,10 +459,7 @@ func (specAnalyser *specV2Analyser) isResourceInstanceEndPoint(p string) (bool,
// how the POST operation (resourceRootPath) of the given resource is defined in swagger.
// If there is no match the returned string will be empty
func (specAnalyser *specV2Analyser) findMatchingResourceRootPath(resourceInstancePath string) (string, error) {
r, err := specAnalyser.resourceInstanceRegex()
if err != nil {
return "", err
}
r, _ := regexp.Compile(resourceInstanceRegex)
result := r.FindStringSubmatch(resourceInstancePath)
log.Printf("[DEBUG] resource '%s' root path match: %s", resourceInstancePath, result)
if len(result) != 2 {
Expand Down
33 changes: 18 additions & 15 deletions openapi/openapi_v2_spec_analyser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1149,21 +1149,6 @@ func TestIsMultiRegionResource(t *testing.T) {
})
}

func TestResourceInstanceRegex(t *testing.T) {
Convey("Given an specV2Analyser", t, func() {
a := specV2Analyser{}
Convey("When resourceInstanceRegex method is called", func() {
regex, err := a.resourceInstanceRegex()
Convey("Then the error returned should be nil", func() {
So(err, ShouldBeNil)
})
Convey("And the regex should not be nil", func() {
So(regex, ShouldNotBeNil)
})
})
})
}

func TestResourceInstanceEndPoint(t *testing.T) {
Convey("Given an specV2Analyser", t, func() {
a := specV2Analyser{}
Expand Down Expand Up @@ -1194,6 +1179,24 @@ func TestResourceInstanceEndPoint(t *testing.T) {
So(resourceInstance, ShouldBeTrue)
})
})
Convey("When isResourceInstanceEndPoint method is called with a path that has path parameters and ends with trailing slash '/resource/{name}/subresource/{id}/'", func() {
resourceInstance, err := a.isResourceInstanceEndPoint("/resource/{name}/subresource/{id}/")
Convey("Then the error returned should be nil", func() {
So(err, ShouldBeNil)
})
Convey("And the value returned should be true", func() {
So(resourceInstance, ShouldBeTrue)
})
})
Convey("When isResourceInstanceEndPoint method is called with a path that is a root path of a subresource '/resource/{name}/subresource'", func() {
resourceInstance, err := a.isResourceInstanceEndPoint("/resource/{name}/subresource")
Convey("Then the error returned should be nil", func() {
So(err, ShouldBeNil)
})
Convey("And the value returned should be false since it's the sub-resource root endpoint", func() {
So(resourceInstance, ShouldBeFalse)
})
})
Convey("When isResourceInstanceEndPoint method is called with an invalid resource path such as '/resource/not/instance/path' not conforming with the expected pattern '/resource/{id}'", func() {
resourceInstance, err := a.isResourceInstanceEndPoint("/resource/not/valid/instance/path")
Convey("Then the error returned should be nil", func() {
Expand Down

0 comments on commit 944abca

Please sign in to comment.