diff --git a/pkg/apic/servicebuilder_test.go b/pkg/apic/servicebuilder_test.go index 4e9464846..bd6f9e63e 100644 --- a/pkg/apic/servicebuilder_test.go +++ b/pkg/apic/servicebuilder_test.go @@ -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) } diff --git a/pkg/apic/specparser.go b/pkg/apic/specparser.go index 82cdf90fd..c9afdc60c 100644 --- a/pkg/apic/specparser.go +++ b/pkg/apic/specparser.go @@ -4,6 +4,7 @@ import ( "bytes" "encoding/json" "errors" + "fmt" "strings" management "github.com/Axway/agent-sdk/pkg/apic/apiserver/models/management/v1alpha1" @@ -20,6 +21,10 @@ const ( mimeApplicationYAML = "application/yaml" ) +const ( + UnknownYamlJson = "unknown yaml or json based specification" +) + // SpecProcessor - type SpecProcessor interface { GetVersion() string @@ -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 { @@ -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 { @@ -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) { diff --git a/pkg/apic/specparser_test.go b/pkg/apic/specparser_test.go index 3764207ed..abe69755a 100644 --- a/pkg/apic/specparser_test.go +++ b/pkg/apic/specparser_test.go @@ -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, }, { @@ -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, }, { @@ -119,6 +123,7 @@ func TestSpecDiscovery(t *testing.T) { { name: "No input type Unstructured", inputFile: "./testdata/multiplication.thrift", + parseErr: true, expectedType: Unstructured, }, } diff --git a/pkg/util/oas/oas.go b/pkg/util/oas/oas.go index e87bfe984..cc841f653 100644 --- a/pkg/util/oas/oas.go +++ b/pkg/util/oas/oas.go @@ -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" ) @@ -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 } @@ -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.") {