Skip to content

Commit

Permalink
fix RESOURCE serialization bug, test /export and ?export
Browse files Browse the repository at this point in the history
Signed-off-by: Doug Davis <[email protected]>
  • Loading branch information
duglin committed Jan 13, 2025
1 parent 12ef36f commit dbca54b
Show file tree
Hide file tree
Showing 8 changed files with 1,476 additions and 17 deletions.
14 changes: 14 additions & 0 deletions misc/sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/bash

# Usage: sql [ dbName: registry | testreg ] SQL_CMD
# Don't forget to escape things, e.g. sql select \* from Props

registry=registry
if [[ "$1" == "registry" || "$1" == "testreg" ]]; then
registry=$1
shift
fi

docker run -ti --rm --network host --name mysql-client-cmd mysql mysql \
--host 127.0.0.1 --user root --password=password --protocol tcp \
$registry -e "$*"
35 changes: 19 additions & 16 deletions registry/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,19 @@ func (e *Entity) GetAsInt(path string) int {
}

func (e *Entity) GetPP(pp *PropPath) any {
name := pp.DB()
// See if we have an updated value in NewObject, if not grab from Object
var val any
if e.NewObject != nil {
var ok bool
val, ok, _ = ObjectGetProp(e.NewObject, pp)
if !ok {
// TODO: DUG - we should not need this
// val, _, _ = ObjectGetProp(e.Object, pp)
}
} else {
val, _, _ = ObjectGetProp(e.Object, pp)
}

if pp.Len() == 1 && pp.Top() == "#resource" {
results, err := Query(e.tx, `
SELECT Content
Expand Down Expand Up @@ -149,19 +161,6 @@ func (e *Entity) GetPP(pp *PropPath) any {
return (*(row[0])).([]byte)
}

// See if we have an updated value in NewObject, if not grab from Object
var val any
if e.NewObject != nil {
var ok bool
val, ok, _ = ObjectGetProp(e.NewObject, pp)
if !ok {
// TODO: DUG - we should not need this
// val, _, _ = ObjectGetProp(e.Object, pp)
}
} else {
val, _, _ = ObjectGetProp(e.Object, pp)
}

// We used to just grab from Object, not NewObject
/*
// An error from ObjectGetProp is ignored because if they tried to
Expand All @@ -170,7 +169,8 @@ func (e *Entity) GetPP(pp *PropPath) any {
// should return the 'error'
val, _ , _ := ObjectGetProp(e.Object, pp)
*/
log.VPrintf(4, "%s(%s).Get(%s) -> %v", e.Plural, e.UID, name, val)

log.VPrintf(4, "%s(%s).Get(%s) -> %v", e.Plural, e.UID, pp.DB(), val)
return val
}

Expand Down Expand Up @@ -1709,7 +1709,10 @@ func (e *Entity) Save() error {

e.RemoveCollections(newObj)

err := Do(e.tx, `DELETE FROM Props WHERE EntitySID=?`, e.DbSID)
// We exclude #resource because we need to leave it around until
// a user action causes us to explictly delete it
err := Do(e.tx, "DELETE FROM Props WHERE EntitySID=? "+
"AND PropName!='"+NewPPP("#resource").DB()+"'", e.DbSID)
if err != nil {
return fmt.Errorf("Error deleting all prop: %s", err)
}
Expand Down
16 changes: 16 additions & 0 deletions registry/httpStuff.go
Original file line number Diff line number Diff line change
Expand Up @@ -1172,6 +1172,12 @@ func HTTPGet(info *RequestInfo) error {
return SerializeQuery(info, nil, "Registry", info.Filters)
}

// Might need to check other flags, but if we're exporting, it makes
// no sense to show just the resource contents
if info.HasFlag("export") {
info.ShowStructure = true
}

// 'metaInBody' tells us whether xReg metadata should be in the http
// response body or not (meaning, the hasDoc doc)
metaInBody := (info.ResourceModel == nil) ||
Expand Down Expand Up @@ -1255,6 +1261,16 @@ func SerializeQuery(info *RequestInfo, paths []string, what string,
}
}

// Another special case... .../rID/versions?export when rID has xref set
if jw.Entity == nil && info.HasFlag("export") && len(info.Parts) == 5 && info.Parts[4] == "versions" {
// Should be our case since "versions" can never be empty except
// when xref is set. If this is not longer true then we'll need to
// check this Resource's xref to see if it's set.
// Can copy the RawEntityFromPath... stuff above
info.StatusCode = http.StatusNotFound
return fmt.Errorf("Not found")
}

info.AddHeader("Content-Type", "application/json")
if what == "Coll" {
_, err = jw.WriteCollection()
Expand Down
10 changes: 10 additions & 0 deletions registry/jsonWriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,16 @@ func SerializeResourceContents(jw *JsonWriter, e *Entity, info *RequestInfo, ext
_, rm := jw.Entity.GetModels()
singular := rm.Singular

// If the #resource* props aren't there then just exit.
// This will happen when "export" is enabled because the
// props won't show up in the Resorce but will on the default version
// TODO really should do this check in entity.SerializeProps
if IsNil(jw.Entity.Object["#resourceURL"]) &&
IsNil(jw.Entity.Object["#resource"]) &&
IsNil(jw.Entity.Object["#resourceProxyURL"]) {
return nil
}

if url := jw.Entity.GetAsString("#resourceURL"); url != "" {
jw.Printf("%s\n%s%q: %q", *extra, jw.indent, singular+"url", url)
*extra = ","
Expand Down
57 changes: 57 additions & 0 deletions tests/content_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,63 @@ func TestResourceContents(t *testing.T) {
},
})

// v4 = #resourceURL
xHTTP(t, reg, "PATCH", "dirs/d1/files/f1/versions/v4$structure?inline=file",
`{"contenttype":null, "description":"hi"}`, 200, `{
"fileid": "f1",
"versionid": "v4",
"self": "http://localhost:8181/dirs/d1/files/f1/versions/v4$structure",
"xid": "/dirs/d1/files/f1/versions/v4",
"epoch": 2,
"isdefault": true,
"description": "hi",
"createdat": "2025-01-01T12:00:01Z",
"modifiedat": "2025-01-01T12:00:02Z",
"fileurl": "http://example.com"
}
`)

xHTTP(t, reg, "GET", "dirs/d1/files/f1$structure?export&inline=file",
`{"contenttype":null, "description":"hi"}`, 200, `{
"fileid": "f1",
"self": "http://localhost:8181/dirs/d1/files/f1$structure",
"xid": "/dirs/d1/files/f1",
"metaurl": "http://localhost:8181/dirs/d1/files/f1/meta",
"versionsurl": "http://localhost:8181/dirs/d1/files/f1/versions",
"versionscount": 4
}
`)

// Set default to v2
xHTTP(t, reg, "PATCH", "dirs/d1/files/f1/meta",
`{"defaultversionid":"v2", "defaultversionsticky":true}`, 200, `{
"fileid": "f1",
"self": "http://localhost:8181/dirs/d1/files/f1/meta",
"xid": "/dirs/d1/files/f1/meta",
"epoch": 5,
"createdat": "2025-01-01T12:00:01Z",
"modifiedat": "2025-01-01T12:00:02Z",
"defaultversionid": "v2",
"defaultversionurl": "http://localhost:8181/dirs/d1/files/f1/versions/v2",
"defaultversionsticky": true
}
`)

// v2 = #resource
xHTTP(t, reg, "PATCH", "dirs/d1/files/f1$structure?export&inline=file",
`{"contenttype":null, "description":"hi"}`, 200, `{
"fileid": "f1",
"self": "http://localhost:8181/dirs/d1/files/f1$structure",
"xid": "/dirs/d1/files/f1",
"metaurl": "http://localhost:8181/dirs/d1/files/f1/meta",
"versionsurl": "http://localhost:8181/dirs/d1/files/f1/versions",
"versionscount": 4
}
`)

}

type Test struct {
Expand Down
Loading

0 comments on commit dbca54b

Please sign in to comment.