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

APIGOV-28627 - bubble any error when discovering spec type #823

Merged
merged 8 commits into from
Sep 12, 2024
3 changes: 1 addition & 2 deletions pkg/apic/servicebuilder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,11 @@ Some useful links:

func TestNewServiceBodyBuilder(t *testing.T) {
svcBody, err := NewServiceBodyBuilder().Build()
assert.Nil(t, err)
assert.NotNil(t, err) // service body does not build any spec for json or yaml, and no resource type
assert.NotNil(t, svcBody)

// test all the default values
assert.Equal(t, Passthrough, svcBody.AuthPolicy)
assert.Equal(t, Unstructured, svcBody.ResourceType)
assert.Equal(t, PublishedState, svcBody.State)
assert.Equal(t, PublishedStatus, svcBody.Status)
}
Expand Down
45 changes: 39 additions & 6 deletions pkg/apic/specparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"encoding/json"
"errors"
"fmt"
"strings"

management "github.com/Axway/agent-sdk/pkg/apic/apiserver/models/management/v1alpha1"
Expand All @@ -20,6 +21,10 @@ const (
mimeApplicationYAML = "application/yaml"
)

const (
UnknownYamlJson = "unknown yaml or json based specification"
)

// SpecProcessor -
type SpecProcessor interface {
GetVersion() string
Expand Down Expand Up @@ -68,7 +73,10 @@ func NewSpecResourceParser(resourceSpec []byte, resourceSpecType string) SpecRes
// Parse -
func (s *SpecResourceParser) Parse() error {
if s.resourceSpecType == "" {
s.discoverSpecTypeAndCreateProcessor()
err := s.discoverSpecTypeAndCreateProcessor()
if err != nil {
return err
}
} else {
err := s.createProcessorWithResourceType()
if err != nil {
Expand All @@ -86,14 +94,39 @@ func (s *SpecResourceParser) getResourceContentType() string {
return s.resourceContentType
}

func (s *SpecResourceParser) discoverSpecTypeAndCreateProcessor() {
s.specProcessor, _ = s.discoverYAMLAndJSONSpec()
func (s *SpecResourceParser) discoverSpecTypeAndCreateProcessor() error {
errs := []error{}
var err error
s.specProcessor, err = s.discoverYAMLAndJSONSpec()
if err == nil {
return nil
}
errs = append(errs, err)

if s.specProcessor == nil {
s.specProcessor, _ = s.parseWSDLSpec()
s.specProcessor, err = s.parseWSDLSpec()
if err == nil {
return nil
}
errs = append(errs, err)
}
if s.specProcessor == nil {
s.specProcessor, _ = s.parseProtobufSpec()
s.specProcessor, err = s.parseProtobufSpec()
if err == nil {
return nil
}
errs = append(errs, err)
}

errString := ""
for i, err := range errs {
if i > 0 {
errString += ": "
}
errString += err.Error()
}
return fmt.Errorf("could not determine spec type from file: %s", errString)

}

func (s *SpecResourceParser) createProcessorWithResourceType() error {
Expand Down Expand Up @@ -178,7 +211,7 @@ func (s *SpecResourceParser) discoverYAMLAndJSONSpec() (SpecProcessor, error) {
return newRamlProcessor(specDef, s.resourceSpec), nil
}

return nil, errors.New("unknown yaml or json based specification")
return nil, errors.New(UnknownYamlJson)
}

func (s *SpecResourceParser) parseWSDLSpec() (SpecProcessor, error) {
Expand Down
5 changes: 5 additions & 0 deletions pkg/apic/specparser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,13 @@ func TestSpecDiscovery(t *testing.T) {
{
name: "No input type bad OAS version creates Unstructured",
inputFile: "./testdata/petstore-openapi-bad-version.json",
parseErr: true,
expectedType: Unstructured,
},
{
name: "No input type bad Swagger version creates Unstructured",
inputFile: "./testdata/petstore-swagger-bad-version.json",
parseErr: true,
expectedType: Unstructured,
},
{
Expand All @@ -94,11 +96,13 @@ func TestSpecDiscovery(t *testing.T) {
{
name: "No input type WSDL Spec",
inputFile: "./testdata/weather.xml",
parseErr: false,
expectedType: Wsdl,
},
{
name: "No input type Protobuf Spec",
inputFile: "./testdata/petstore.proto",
parseErr: false,
expectedType: Protobuf,
},
{
Expand All @@ -119,6 +123,7 @@ func TestSpecDiscovery(t *testing.T) {
{
name: "No input type Unstructured",
inputFile: "./testdata/multiplication.thrift",
parseErr: true,
expectedType: Unstructured,
},
}
Expand Down
3 changes: 3 additions & 0 deletions pkg/util/oas/oas.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/url"
"strings"

"github.com/Axway/agent-sdk/pkg/util/log"
"github.com/getkin/kin-openapi/openapi2"
"github.com/getkin/kin-openapi/openapi3"
)
Expand All @@ -16,6 +17,7 @@ func ParseOAS2(spec []byte) (*openapi2.T, error) {
swaggerObj := &openapi2.T{}
err := json.Unmarshal(spec, swaggerObj)
if err != nil {
log.Error("unable to parse OAS2 specification")
return nil, err
}

Expand All @@ -35,6 +37,7 @@ func ParseOAS2(spec []byte) (*openapi2.T, error) {
func ParseOAS3(spec []byte) (*openapi3.T, error) {
oas3Obj, err := openapi3.NewLoader().LoadFromData(spec)
if err != nil {
log.Error("unable to parse OAS3 specification")
return nil, err
}
if !strings.Contains(oas3Obj.OpenAPI, "3.") {
Expand Down