github.com/Axway/agent-sdk@v1.1.101/pkg/util/oas/oas.go (about)

     1  package oas
     2  
     3  import (
     4  	"encoding/json"
     5  	"errors"
     6  	"fmt"
     7  	"net/url"
     8  	"strings"
     9  
    10  	"github.com/Axway/agent-sdk/pkg/util/log"
    11  	"github.com/getkin/kin-openapi/openapi2"
    12  	"github.com/getkin/kin-openapi/openapi3"
    13  )
    14  
    15  // ParseOAS2 converts a JSON spec into an OpenAPI2 object.
    16  func ParseOAS2(spec []byte) (*openapi2.T, error) {
    17  	swaggerObj := &openapi2.T{}
    18  	err := json.Unmarshal(spec, swaggerObj)
    19  	if err != nil {
    20  		log.Error("unable to parse OAS2 specification")
    21  		return nil, err
    22  	}
    23  
    24  	if !strings.Contains(swaggerObj.Swagger, "2.") {
    25  		return nil, errors.New(oasParseError("2.0", "'swagger' must be version '2.0'."))
    26  	}
    27  	if swaggerObj.Info.Title == "" {
    28  		return nil, errors.New(oasParseError("2.0", "'info.title' key not found."))
    29  	}
    30  	if swaggerObj.Paths == nil {
    31  		return nil, errors.New(oasParseError("2.0", "'paths' key not found."))
    32  	}
    33  	return swaggerObj, nil
    34  }
    35  
    36  // ParseOAS3 converts a JSON or YAML spec into an OpenAPI3 object.
    37  func ParseOAS3(spec []byte) (*openapi3.T, error) {
    38  	oas3Obj, err := openapi3.NewLoader().LoadFromData(spec)
    39  	if err != nil {
    40  		log.Error("unable to parse OAS3 specification")
    41  		return nil, err
    42  	}
    43  	if !strings.Contains(oas3Obj.OpenAPI, "3.") {
    44  		return nil, fmt.Errorf(oasParseError("3", ("'openapi' key is invalid.")))
    45  	}
    46  	if oas3Obj.Paths == nil {
    47  		return nil, fmt.Errorf(oasParseError("3", "'paths' key not found."))
    48  	}
    49  	if oas3Obj.Info == nil {
    50  		return nil, fmt.Errorf(oasParseError("3", "'info' key not found."))
    51  	}
    52  	if oas3Obj.Info.Title == "" {
    53  		return nil, fmt.Errorf(oasParseError("3", "'info.title' key not found."))
    54  	}
    55  	return oas3Obj, nil
    56  }
    57  
    58  // SetOAS2HostDetails Updates the Host, BasePath, and Schemes fields on an OpenAPI2 object.
    59  func SetOAS2HostDetails(spec *openapi2.T, endpointURL string) error {
    60  	endpoint, err := url.Parse(endpointURL)
    61  	if err != nil {
    62  		return err
    63  	}
    64  
    65  	basePath := ""
    66  	if endpoint.Path == "" {
    67  		basePath = "/"
    68  	} else {
    69  		basePath = endpoint.Path
    70  	}
    71  
    72  	host := endpoint.Host
    73  	schemes := []string{endpoint.Scheme}
    74  	spec.Host = host
    75  	spec.BasePath = basePath
    76  	spec.Schemes = schemes
    77  	return nil
    78  }
    79  
    80  // SetOAS3Servers replaces the servers array on the OpenAPI3 object.
    81  func SetOAS3Servers(hosts []string, spec *openapi3.T) {
    82  	var oas3Servers []*openapi3.Server
    83  	for _, s := range hosts {
    84  		oas3Servers = append(oas3Servers, &openapi3.Server{
    85  			URL: s,
    86  		})
    87  	}
    88  	if len(oas3Servers) > 0 {
    89  		spec.Servers = oas3Servers
    90  	}
    91  }
    92  
    93  func oasParseError(version string, msg string) string {
    94  	return fmt.Sprintf("invalid openapi %s specification. %s", version, msg)
    95  }