Skip to content

Commit

Permalink
derive and encode openapi version in mimetype (#1092)
Browse files Browse the repository at this point in the history
  • Loading branch information
theganyo authored Mar 15, 2023
1 parent 237026a commit 1b9ba77
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
13 changes: 11 additions & 2 deletions cmd/registry/cmd/upload/csv.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ import (
"github.com/apigee/registry/pkg/names"
"github.com/apigee/registry/pkg/visitor"
"github.com/apigee/registry/rpc"
oas2 "github.com/google/gnostic/openapiv2"
oas3 "github.com/google/gnostic/openapiv3"
"github.com/spf13/cobra"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
Expand Down Expand Up @@ -226,6 +228,14 @@ func (t uploadSpecTask) Run(ctx context.Context) error {
return err
}

oasVer := "unknown"
if doc, err := oas3.ParseDocument(contents); err == nil {
oasVer = doc.Openapi
} else if doc, err := oas2.ParseDocument(contents); err == nil {
oasVer = doc.Swagger
}
oasMimeType := mime.OpenAPIMimeType("+gzip", oasVer)

compressed, err := compress.GZippedBytes(contents)
if err != nil {
return err
Expand All @@ -236,8 +246,7 @@ func (t uploadSpecTask) Run(ctx context.Context) error {
Parent: specName.Parent(),
ApiSpecId: specName.SpecID,
ApiSpec: &rpc.ApiSpec{
// TODO: How do we choose a mime type?
MimeType: mime.OpenAPIMimeType("+gzip", "3.0.0"),
MimeType: oasMimeType,
Contents: compressed,
},
})
Expand Down
11 changes: 11 additions & 0 deletions cmd/registry/cmd/upload/csv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
)

const (
gzipOpenAPIv2 = "application/x.openapi+gzip;version=2.0"
gzipOpenAPIv3 = "application/x.openapi+gzip;version=3.0.0"
)

Expand All @@ -56,6 +57,11 @@ func TestUploadCSV(t *testing.T) {
t.Fatalf("Setup: Failed to read spec contents: %s", err)
}

swagger, err := os.ReadFile(filepath.Join("testdata", "openapi", "petstore", "2.0", "swagger.yaml"))
if err != nil {
t.Fatalf("Setup: Failed to read spec contents: %s", err)
}

const testProject = "csv-demo"
const testParent = "projects/" + testProject + "/locations/global"
tests := []struct {
Expand Down Expand Up @@ -90,6 +96,11 @@ func TestUploadCSV(t *testing.T) {
MimeType: gzipOpenAPIv3,
Contents: datastoreGA,
},
{
Name: fmt.Sprintf("projects/%s/locations/global/apis/swagger/versions/v1/specs/openapi", testProject),
MimeType: gzipOpenAPIv2,
Contents: swagger,
},
},
},
{
Expand Down
1 change: 1 addition & 0 deletions cmd/registry/cmd/upload/testdata/csv/multiple-specs.csv
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ cloudtasks,v2beta2,openapi,testdata/csv/cloudtasks/v2beta2/openapi.yaml
cloudtasks,v2,openapi,testdata/csv/cloudtasks/v2/openapi.yaml
datastore,v1beta1,openapi,testdata/csv/datastore/v1beta1/openapi.yaml
datastore,v1,openapi,testdata/csv/datastore/v1/openapi.yaml
swagger,v1,openapi,testdata/openapi/petstore/2.0/swagger.yaml

0 comments on commit 1b9ba77

Please sign in to comment.