Skip to content

Commit

Permalink
gh-1106 extend get single thing with api links
Browse files Browse the repository at this point in the history
  • Loading branch information
etiennedi committed Mar 26, 2020
1 parent 1aefdd0 commit 90bad8b
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 1 deletion.
10 changes: 10 additions & 0 deletions adapters/handlers/rest/embedded_spec.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 43 additions & 0 deletions adapters/handlers/rest/handlers_kinds.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ package rest

import (
"context"
"fmt"

middleware "github.com/go-openapi/runtime/middleware"
"github.com/go-openapi/strfmt"
"github.com/semi-technologies/weaviate/adapters/handlers/rest/operations"
"github.com/semi-technologies/weaviate/adapters/handlers/rest/operations/actions"
"github.com/semi-technologies/weaviate/adapters/handlers/rest/operations/things"
"github.com/semi-technologies/weaviate/entities/models"
"github.com/semi-technologies/weaviate/entities/schema/crossref"
"github.com/semi-technologies/weaviate/usecases/auth/authorization/errors"
"github.com/semi-technologies/weaviate/usecases/kinds"
"github.com/semi-technologies/weaviate/usecases/telemetry"
Expand Down Expand Up @@ -161,6 +163,11 @@ func (h *kindHandlers) getThing(params things.ThingsGetParams,
}
}

schemaMap, ok := thing.Schema.(map[string]interface{})
if ok {
thing.Schema = h.extendSchemaWithAPILinks(schemaMap)
}

h.telemetryLogAsync(telemetry.TypeREST, telemetry.LocalQuery)
return things.NewThingsGetOK().WithPayload(thing)
}
Expand Down Expand Up @@ -540,3 +547,39 @@ func derefBool(in *bool) bool {

return *in
}

func (h *kindHandlers) extendSchemaWithAPILinks(schema map[string]interface{}) map[string]interface{} {
if schema == nil {
return schema
}

for key, value := range schema {
asMultiRef, ok := value.(models.MultipleRef)
if !ok {
continue
}

schema[key] = h.extendReferencesWithAPILinks(asMultiRef)
}
return schema
}

func (h *kindHandlers) extendReferencesWithAPILinks(refs models.MultipleRef) models.MultipleRef {
for i, ref := range refs {
refs[i] = h.extendReferenceWithAPILink(ref)
}

return refs
}

func (h *kindHandlers) extendReferenceWithAPILink(ref *models.SingleRef) *models.SingleRef {

parsed, err := crossref.Parse(ref.Beacon.String())
if err != nil {
// ignore return unchanged
return ref
}

ref.Href = strfmt.URI(fmt.Sprintf("%s/v1/%ss/%s", "", parsed.Kind.Name(), parsed.TargetID))
return ref
}
33 changes: 33 additions & 0 deletions adapters/handlers/rest/handlers_kinds_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,39 @@ func TestEnrichObjectsWithLinks(t *testing.T) {
thing: &models.Thing{Class: "Foo", Schema: nil},
expectedResult: &models.Thing{Class: "Foo", Schema: nil},
},
test{
name: "without ref props - nothing changes",
thing: &models.Thing{Class: "Foo", Schema: map[string]interface{}{
"name": "hello world",
"numericalField": 134,
}},
expectedResult: &models.Thing{Class: "Foo", Schema: map[string]interface{}{
"name": "hello world",
"numericalField": 134,
}},
},
test{
name: "with a ref prop - no origin configured",
thing: &models.Thing{Class: "Foo", Schema: map[string]interface{}{
"name": "hello world",
"numericalField": 134,
"someRef": models.MultipleRef{
&models.SingleRef{
Beacon: "weaviate://localhost/things/85f78e29-5937-4390-a121-5379f262b4e5",
},
},
}},
expectedResult: &models.Thing{Class: "Foo", Schema: map[string]interface{}{
"name": "hello world",
"numericalField": 134,
"someRef": models.MultipleRef{
&models.SingleRef{
Beacon: "weaviate://localhost/things/85f78e29-5937-4390-a121-5379f262b4e5",
Href: "/v1/things/85f78e29-5937-4390-a121-5379f262b4e5",
},
},
}},
},
}

for _, test := range tests {
Expand Down
21 changes: 21 additions & 0 deletions entities/models/single_ref.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions openapi-specs/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,11 @@
"format": "uri",
"type": "string"
},
"href": {
"description": "If using a direct reference, this read-only fields provides a link to the refernced resource. If 'origin' is globally configured, an absolute URI is shown - a relative URI otherwise.",
"format": "uri",
"type": "string"
},
"meta": {
"description": "Additional Meta information about this particular reference. Only shown if meta==true.",
"$ref": "#/definitions/ReferenceMeta"
Expand Down
6 changes: 6 additions & 0 deletions test/acceptance/things/crefs_cardinality_many_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ func Test_CREFWithCardinalityMany_UsingPatch(t *testing.T) {
"hasPlaces": []interface{}{
map[string]interface{}{
"beacon": fmt.Sprintf("weaviate://localhost/things/%s", place1ID.String()),
"href": fmt.Sprintf("/v1/things/%s", place1ID.String()),
},
},
}, actualThunk)
Expand Down Expand Up @@ -147,9 +148,11 @@ func Test_CREFWithCardinalityMany_UsingPatch(t *testing.T) {
expectedRefs := []interface{}{
map[string]interface{}{
"beacon": fmt.Sprintf("weaviate://localhost/things/%s", place1ID.String()),
"href": fmt.Sprintf("/v1/things/%s", place1ID.String()),
},
map[string]interface{}{
"beacon": fmt.Sprintf("weaviate://localhost/things/%s", place2ID.String()),
"href": fmt.Sprintf("/v1/things/%s", place2ID.String()),
},
}

Expand Down Expand Up @@ -243,6 +246,7 @@ func Test_CREFWithCardinalityMany_UsingPostReference(t *testing.T) {
"hasPlaces": []interface{}{
map[string]interface{}{
"beacon": fmt.Sprintf("weaviate://localhost/things/%s", place1ID.String()),
"href": fmt.Sprintf("/v1/things/%s", place1ID.String()),
},
},
}, actualThunk)
Expand All @@ -266,9 +270,11 @@ func Test_CREFWithCardinalityMany_UsingPostReference(t *testing.T) {
expectedRefs := []interface{}{
map[string]interface{}{
"beacon": fmt.Sprintf("weaviate://localhost/things/%s", place1ID.String()),
"href": fmt.Sprintf("/v1/things/%s", place1ID.String()),
},
map[string]interface{}{
"beacon": fmt.Sprintf("weaviate://localhost/things/%s", place2ID.String()),
"href": fmt.Sprintf("/v1/things/%s", place2ID.String()),
},
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ func Test_AddingReferenceWithoutWaiting_UsingPostThings(t *testing.T) {
"hasPlace": []interface{}{
map[string]interface{}{
"beacon": fmt.Sprintf("weaviate://localhost/things/%s", placeID.String()),
"href": fmt.Sprintf("/v1/things/%s", placeID.String()),
},
},
}, actualThunk)

}

0 comments on commit 90bad8b

Please sign in to comment.