diff --git a/controllers/rest/middleware_initialize.go b/controllers/rest/middleware_initialize.go index 3b434035..23d0ed81 100644 --- a/controllers/rest/middleware_initialize.go +++ b/controllers/rest/middleware_initialize.go @@ -58,7 +58,7 @@ func newSchema(ref *openapi3.Schema, logger echo.Logger) (ds.Builder, map[string relations := make(map[string]string) for name, p := range ref.Properties { - tagString := `json:"` + utils.SnakeCase(name) + `"` + tagString := `json:"` + name + `"` var gormParts []string for _, req := range ref.Required { if strings.EqualFold(req, name) { @@ -203,7 +203,7 @@ func addRelations(struc ds.Builder, relations map[string]string, structs map[str keystring += strings.Title(name) + strings.Title(k) } - struc.AddField(name, instance, `json:"`+utils.SnakeCase(name)+`" gorm:"foreignKey:`+keystring+`; references `+strings.Join(key, ",")+`"`) + struc.AddField(name, instance, `json:"`+name+`" gorm:"foreignKey:`+keystring+`; references `+strings.Join(key, ",")+`"`) } } return struc, nil diff --git a/end2end_test.go b/end2end_test.go index fd7001f9..6cd95df4 100644 --- a/end2end_test.go +++ b/end2end_test.go @@ -1154,7 +1154,7 @@ func TestBDD(t *testing.T) { Options: &godog.Options{ Format: "pretty", Tags: "~skipped && ~long", - //Tags: "WEOS-1131", + //Tags: "focus1", //Tags: "WEOS-1110 && ~skipped", }, }.Run() diff --git a/features/edit-content.feature b/features/edit-content.feature index 238ddd1c..b77fbc5a 100644 --- a/features/edit-content.feature +++ b/features/edit-content.feature @@ -201,14 +201,15 @@ Feature: Edit content And the "Blog" is updated | title | description | | Some New Title | Some Description | - + + @focus1 Scenario: Update item with invalid data If the content type validation fails then a 422 response code should be returned (the request could have a valid format but the contents are invalid) Given "Sojourner" is on the "Blog" edit screen with id "1234" - And "Sojourner" enters "Some New Title" in the "last_updated" field + And "Sojourner" enters "Some New Title" in the "lastUpdated" field When the "Blog" is submitted Then a 422 response should be returned diff --git a/projections/gorm.go b/projections/gorm.go index 69f7dd41..f3e54525 100644 --- a/projections/gorm.go +++ b/projections/gorm.go @@ -3,13 +3,14 @@ package projections import ( "encoding/json" "fmt" + "strings" + "time" + ds "github.com/ompluscator/dynamic-struct" weos "github.com/wepala/weos/model" "golang.org/x/net/context" "gorm.io/gorm" "gorm.io/gorm/clause" - "strings" - "time" ) //GORMProjection interface struct @@ -77,16 +78,17 @@ func (p *GORMProjection) GetByKey(ctxt context.Context, entityFactory weos.Entit } func (p *GORMProjection) GetByEntityID(ctx context.Context, entityFactory weos.EntityFactory, id string) (map[string]interface{}, error) { - scheme, err := entityFactory.NewEntity(ctx) - if err != nil { - return nil, err - } - result := p.db.Table(entityFactory.Name()).Scopes(ContentQuery()).Find(scheme.Property, "weos_id = ?", id) + //scheme, err := entityFactory.NewEntity(ctx) + tstruct := entityFactory.DynamicStruct(ctx).New() + //if err != nil { + // return nil, err + //} + result := p.db.Table(entityFactory.Name()).Scopes(ContentQuery()).Find(tstruct, "weos_id = ?", id) if result.Error != nil { return nil, result.Error } - data, err := json.Marshal(scheme.Property) + data, err := json.Marshal(tstruct) if err != nil { return nil, err } @@ -216,17 +218,17 @@ func (p *GORMProjection) GetEventHandler() weos.EventHandler { } func (p *GORMProjection) GetContentEntity(ctx context.Context, entityFactory weos.EntityFactory, weosID string) (*weos.ContentEntity, error) { - row := map[string]interface{}{} - result := p.db.Table(entityFactory.TableName()).Find(&row, "weos_id = ? ", weosID) - if result.Error != nil { - p.logger.Errorf("unexpected error retrieving created blog, got: '%s'", result.Error) - } - //set result to entity newEntity, err := entityFactory.NewEntity(ctx) if err != nil { return nil, err } - rowData, err := json.Marshal(row) + + result := p.db.Table(entityFactory.TableName()).Find(newEntity.Property, "weos_id = ? ", weosID) + if result.Error != nil { + p.logger.Errorf("unexpected error retrieving created blog, got: '%s'", result.Error) + } + //set result to entity + rowData, err := json.Marshal(newEntity.Property) if err != nil { return nil, err } diff --git a/projections/projections_test.go b/projections/projections_test.go index 08dd0809..34e612ab 100644 --- a/projections/projections_test.go +++ b/projections/projections_test.go @@ -2750,3 +2750,105 @@ components: } }) } + +func TestProjections_GetContentWithCorrectCasing(t *testing.T) { + openAPI := `openapi: 3.0.3 +info: + title: Blog + description: Blog example + version: 1.0.0 +servers: + - url: https://prod1.weos.sh/blog/dev + description: WeOS Dev + - url: https://prod1.weos.sh/blog/v1 +components: + schemas: + Post: + type: object + properties: + title: + type: string + description: blog title + description: + type: string + Blog: + type: object + properties: + title: + type: string + description: blog title + lastUpdated: + type: string + posts: + type: array + items: + $ref: "#/components/schemas/Post" +` + + api, err := rest.New(openAPI) + if err != nil { + t.Fatalf("error loading api config '%s'", err) + } + schemes := rest.CreateSchema(context.Background(), echo.New(), api.Swagger) + p, err := projections.NewProjection(context.Background(), gormDB, api.EchoInstance().Logger) + if err != nil { + t.Fatal(err) + } + + err = p.Migrate(context.Background(), schemes) + if err != nil { + t.Fatal(err) + } + + if !gormDB.Migrator().HasTable("Blog") { + t.Errorf("expected to get a table 'Blog'") + } + + if !gormDB.Migrator().HasTable("Post") { + t.Errorf("expected to get a table 'Post'") + } + + if !gormDB.Migrator().HasTable("blog_posts") { + t.Errorf("expected to get a table 'blog_posts'") + } + + columns, _ := gormDB.Migrator().ColumnTypes("Blog") + + found := false + for _, c := range columns { + if c.Name() == "last_updated" { + found = true + } + } + + if !found { + t.Fatal("not all fields found") + } + gormDB.Table("Blog").Create(map[string]interface{}{"weos_id": "5678", "sequence_no": 1, "title": "hugs", "last_updated": "Test"}) + + blogEntityFactory := new(weos.DefaultEntityFactory).FromSchemaAndBuilder("Blog", api.Swagger.Components.Schemas["Blog"].Value, schemes["Blog"]) + r, err := p.GetByEntityID(context.Background(), blogEntityFactory, "5678") + if err != nil { + t.Fatalf("error querying '%s' '%s'", "Blog", err) + } + if r["title"] != "hugs" { + t.Errorf("expected the blog title to be %s got %v", "hugs", r["titles"]) + } + + if r["lastUpdated"] != "Test" { + t.Errorf("expected the lastUpdated to be %s got %v", "Test", r["lastUpdated"]) + } + + err = gormDB.Migrator().DropTable("blog_posts") + if err != nil { + t.Errorf("error removing table '%s' '%s'", "blog_posts", err) + } + err = gormDB.Migrator().DropTable("Post") + if err != nil { + t.Errorf("error removing table '%s' '%s'", "Post", err) + } + err = gormDB.Migrator().DropTable("Blog") + if err != nil { + t.Errorf("error removing table '%s' '%s'", "Blog", err) + } +}