Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug: Fixed casing issue #95

Merged
merged 4 commits into from
Feb 14, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
2 changes: 1 addition & 1 deletion end2end_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
5 changes: 3 additions & 2 deletions features/edit-content.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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
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)
}
}