Skip to content

Commit

Permalink
bug: Fixed casing issue
Browse files Browse the repository at this point in the history
  • Loading branch information
akeemphilbert committed Feb 11, 2022
1 parent bbe2e4a commit cc1f393
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 8 deletions.
4 changes: 2 additions & 2 deletions controllers/rest/middleware_initialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down
13 changes: 7 additions & 6 deletions projections/gorm.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,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
}
Expand Down
102 changes: 102 additions & 0 deletions projections/projections_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit cc1f393

Please sign in to comment.