Skip to content

Commit

Permalink
Add Document Type Safety Check (#4103)
Browse files Browse the repository at this point in the history
  • Loading branch information
skmcgrail authored Sep 21, 2021
1 parent 4b455a6 commit f3fff59
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 0 deletions.
18 changes: 18 additions & 0 deletions private/model/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1020,6 +1020,24 @@ func (a *API) addHeaderMapDocumentation() {
}
}

func (a *API) validateNoDocumentShapes() error {
var shapes []string
for name, shape := range a.Shapes {
if shape.Type != "structure" {
continue
}
if shape.Document {
shapes = append(shapes, name)
}
}

if len(shapes) == 0 {
return nil
}

return fmt.Errorf("model contains document shapes: %s", strings.Join(shapes, ", "))
}

func getDeprecatedMessage(msg string, name string) string {
if len(msg) == 0 {
return name + " has been deprecated"
Expand Down
21 changes: 21 additions & 0 deletions private/model/api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package api

import (
"strings"
"testing"
)

Expand Down Expand Up @@ -72,3 +73,23 @@ func TestAPI_StructName(t *testing.T) {
})
}
}

func TestAPI_Setup_documentShapes(t *testing.T) {
api := API{
Shapes: map[string]*Shape{
"Document": {
Type: "structure",
Document: true,
},
},
}

err := api.Setup()
if err == nil {
t.Fatalf("expect error, but got nil")
}
expect := "model contains document shapes"
if !strings.Contains(err.Error(), expect) {
t.Errorf("expect %s, got %v", expect, err)
}
}
3 changes: 3 additions & 0 deletions private/model/api/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,9 @@ func (a *API) AttachString(str string) error {

// Setup initializes the API.
func (a *API) Setup() error {
if err := a.validateNoDocumentShapes(); err != nil {
return err
}
a.setServiceAliaseName()
a.setMetadataEndpointsKey()
a.writeShapeNames()
Expand Down
3 changes: 3 additions & 0 deletions private/model/api/shape.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,9 @@ type Shape struct {

// Indicates the Shape is used as an operation output
UsedAsOutput bool

// Indicates a structure shape is a document type
Document bool `json:"document"`
}

// CanBeEmpty returns if the shape value can sent request as an empty value.
Expand Down

0 comments on commit f3fff59

Please sign in to comment.