From d9e4cc67b1f81d5e532275f0930e955faa4792b3 Mon Sep 17 00:00:00 2001 From: vancanhuit Date: Sun, 12 Dec 2021 13:56:10 +0700 Subject: [PATCH 1/3] fix: namespace should not be required in List API Fixes #761 --- .gitignore | 1 + internal/e2e/Test | 0 internal/e2e/sdk_client_test.go | 6 ++-- .../client/read/get_check_parameters.go | 34 +++++++++++++++++++ .../client/read/get_expand_parameters.go | 25 +++++++++----- .../read/get_relation_tuples_parameters.go | 25 +++++++++----- .../client/read/post_check_parameters.go | 34 +++++++++++++++++++ internal/httpclient/models/expand_tree.go | 14 +------- .../models/get_relation_tuples_response.go | 4 --- .../models/internal_relation_tuple.go | 4 --- internal/httpclient/models/patch_delta.go | 4 --- internal/httpclient/models/relation_query.go | 4 --- internal/relationtuple/read_server.go | 1 - internal/relationtuple/read_server_test.go | 26 ++++++++++++++ spec/api.json | 25 +++++++++----- 15 files changed, 148 insertions(+), 59 deletions(-) delete mode 100644 internal/e2e/Test diff --git a/.gitignore b/.gitignore index 79f489147..5e3e85fe2 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ node_modules/ coverage.txt dist/ **/*.sqlite +.vscode/ \ No newline at end of file diff --git a/internal/e2e/Test b/internal/e2e/Test deleted file mode 100644 index e69de29bb..000000000 diff --git a/internal/e2e/sdk_client_test.go b/internal/e2e/sdk_client_test.go index a73758091..f6f009688 100644 --- a/internal/e2e/sdk_client_test.go +++ b/internal/e2e/sdk_client_test.go @@ -95,7 +95,7 @@ func (c *sdkClient) deleteTuple(t require.TestingT, r *relationtuple.InternalRel } func compileParams(q *relationtuple.RelationQuery, opts []x.PaginationOptionSetter) *read.GetRelationTuplesParams { - params := read.NewGetRelationTuplesParams().WithNamespace(q.Namespace) + params := read.NewGetRelationTuplesParams().WithNamespace(&q.Namespace) if q.Relation != "" { params = params.WithRelation(&q.Relation) } @@ -213,7 +213,9 @@ func (c *sdkClient) expand(t require.TestingT, r *relationtuple.SubjectSet, dept WithNamespace(r.Namespace). WithObject(r.Object). WithRelation(r.Relation). - WithMaxDepth(int64(depth)), + WithMaxDepth(func(n int64) *int64 { + return &n + }(int64(depth))), ) require.NoError(t, err) return buildTree(t, resp.Payload) diff --git a/internal/httpclient/client/read/get_check_parameters.go b/internal/httpclient/client/read/get_check_parameters.go index 4217ea64b..d6db4270b 100644 --- a/internal/httpclient/client/read/get_check_parameters.go +++ b/internal/httpclient/client/read/get_check_parameters.go @@ -14,6 +14,7 @@ import ( "github.com/go-openapi/runtime" cr "github.com/go-openapi/runtime/client" "github.com/go-openapi/strfmt" + "github.com/go-openapi/swag" ) // NewGetCheckParams creates a new GetCheckParams object, @@ -59,6 +60,11 @@ func NewGetCheckParamsWithHTTPClient(client *http.Client) *GetCheckParams { */ type GetCheckParams struct { + // MaxDepth. + // + // Format: int64 + MaxDepth *int64 + /* Namespace. Namespace of the Relation Tuple @@ -154,6 +160,17 @@ func (o *GetCheckParams) SetHTTPClient(client *http.Client) { o.HTTPClient = client } +// WithMaxDepth adds the maxDepth to the get check params +func (o *GetCheckParams) WithMaxDepth(maxDepth *int64) *GetCheckParams { + o.SetMaxDepth(maxDepth) + return o +} + +// SetMaxDepth adds the maxDepth to the get check params +func (o *GetCheckParams) SetMaxDepth(maxDepth *int64) { + o.MaxDepth = maxDepth +} + // WithNamespace adds the namespace to the get check params func (o *GetCheckParams) WithNamespace(namespace string) *GetCheckParams { o.SetNamespace(namespace) @@ -239,6 +256,23 @@ func (o *GetCheckParams) WriteToRequest(r runtime.ClientRequest, reg strfmt.Regi } var res []error + if o.MaxDepth != nil { + + // query param max-depth + var qrMaxDepth int64 + + if o.MaxDepth != nil { + qrMaxDepth = *o.MaxDepth + } + qMaxDepth := swag.FormatInt64(qrMaxDepth) + if qMaxDepth != "" { + + if err := r.SetQueryParam("max-depth", qMaxDepth); err != nil { + return err + } + } + } + // query param namespace qrNamespace := o.Namespace qNamespace := qrNamespace diff --git a/internal/httpclient/client/read/get_expand_parameters.go b/internal/httpclient/client/read/get_expand_parameters.go index f8f58085a..5e81db9cf 100644 --- a/internal/httpclient/client/read/get_expand_parameters.go +++ b/internal/httpclient/client/read/get_expand_parameters.go @@ -63,7 +63,7 @@ type GetExpandParams struct { // MaxDepth. // // Format: int64 - MaxDepth int64 + MaxDepth *int64 /* Namespace. @@ -137,13 +137,13 @@ func (o *GetExpandParams) SetHTTPClient(client *http.Client) { } // WithMaxDepth adds the maxDepth to the get expand params -func (o *GetExpandParams) WithMaxDepth(maxDepth int64) *GetExpandParams { +func (o *GetExpandParams) WithMaxDepth(maxDepth *int64) *GetExpandParams { o.SetMaxDepth(maxDepth) return o } // SetMaxDepth adds the maxDepth to the get expand params -func (o *GetExpandParams) SetMaxDepth(maxDepth int64) { +func (o *GetExpandParams) SetMaxDepth(maxDepth *int64) { o.MaxDepth = maxDepth } @@ -188,13 +188,20 @@ func (o *GetExpandParams) WriteToRequest(r runtime.ClientRequest, reg strfmt.Reg } var res []error - // query param max-depth - qrMaxDepth := o.MaxDepth - qMaxDepth := swag.FormatInt64(qrMaxDepth) - if qMaxDepth != "" { + if o.MaxDepth != nil { - if err := r.SetQueryParam("max-depth", qMaxDepth); err != nil { - return err + // query param max-depth + var qrMaxDepth int64 + + if o.MaxDepth != nil { + qrMaxDepth = *o.MaxDepth + } + qMaxDepth := swag.FormatInt64(qrMaxDepth) + if qMaxDepth != "" { + + if err := r.SetQueryParam("max-depth", qMaxDepth); err != nil { + return err + } } } diff --git a/internal/httpclient/client/read/get_relation_tuples_parameters.go b/internal/httpclient/client/read/get_relation_tuples_parameters.go index 011f8fc62..71c096666 100644 --- a/internal/httpclient/client/read/get_relation_tuples_parameters.go +++ b/internal/httpclient/client/read/get_relation_tuples_parameters.go @@ -64,7 +64,7 @@ type GetRelationTuplesParams struct { Namespace of the Relation Tuple */ - Namespace string + Namespace *string /* Object. @@ -164,13 +164,13 @@ func (o *GetRelationTuplesParams) SetHTTPClient(client *http.Client) { } // WithNamespace adds the namespace to the get relation tuples params -func (o *GetRelationTuplesParams) WithNamespace(namespace string) *GetRelationTuplesParams { +func (o *GetRelationTuplesParams) WithNamespace(namespace *string) *GetRelationTuplesParams { o.SetNamespace(namespace) return o } // SetNamespace adds the namespace to the get relation tuples params -func (o *GetRelationTuplesParams) SetNamespace(namespace string) { +func (o *GetRelationTuplesParams) SetNamespace(namespace *string) { o.Namespace = namespace } @@ -270,13 +270,20 @@ func (o *GetRelationTuplesParams) WriteToRequest(r runtime.ClientRequest, reg st } var res []error - // query param namespace - qrNamespace := o.Namespace - qNamespace := qrNamespace - if qNamespace != "" { + if o.Namespace != nil { - if err := r.SetQueryParam("namespace", qNamespace); err != nil { - return err + // query param namespace + var qrNamespace string + + if o.Namespace != nil { + qrNamespace = *o.Namespace + } + qNamespace := qrNamespace + if qNamespace != "" { + + if err := r.SetQueryParam("namespace", qNamespace); err != nil { + return err + } } } diff --git a/internal/httpclient/client/read/post_check_parameters.go b/internal/httpclient/client/read/post_check_parameters.go index 1b423a5ab..c24dfe7f0 100644 --- a/internal/httpclient/client/read/post_check_parameters.go +++ b/internal/httpclient/client/read/post_check_parameters.go @@ -14,6 +14,7 @@ import ( "github.com/go-openapi/runtime" cr "github.com/go-openapi/runtime/client" "github.com/go-openapi/strfmt" + "github.com/go-openapi/swag" "github.com/ory/keto/internal/httpclient/models" ) @@ -64,6 +65,11 @@ type PostCheckParams struct { // Payload. Payload *models.RelationQuery + // MaxDepth. + // + // Format: int64 + MaxDepth *int64 + timeout time.Duration Context context.Context HTTPClient *http.Client @@ -128,6 +134,17 @@ func (o *PostCheckParams) SetPayload(payload *models.RelationQuery) { o.Payload = payload } +// WithMaxDepth adds the maxDepth to the post check params +func (o *PostCheckParams) WithMaxDepth(maxDepth *int64) *PostCheckParams { + o.SetMaxDepth(maxDepth) + return o +} + +// SetMaxDepth adds the maxDepth to the post check params +func (o *PostCheckParams) SetMaxDepth(maxDepth *int64) { + o.MaxDepth = maxDepth +} + // WriteToRequest writes these params to a swagger request func (o *PostCheckParams) WriteToRequest(r runtime.ClientRequest, reg strfmt.Registry) error { @@ -141,6 +158,23 @@ func (o *PostCheckParams) WriteToRequest(r runtime.ClientRequest, reg strfmt.Reg } } + if o.MaxDepth != nil { + + // query param max-depth + var qrMaxDepth int64 + + if o.MaxDepth != nil { + qrMaxDepth = *o.MaxDepth + } + qMaxDepth := swag.FormatInt64(qrMaxDepth) + if qMaxDepth != "" { + + if err := r.SetQueryParam("max-depth", qMaxDepth); err != nil { + return err + } + } + } + if len(res) > 0 { return errors.CompositeValidationError(res...) } diff --git a/internal/httpclient/models/expand_tree.go b/internal/httpclient/models/expand_tree.go index f76285ca1..986d71d19 100644 --- a/internal/httpclient/models/expand_tree.go +++ b/internal/httpclient/models/expand_tree.go @@ -30,11 +30,7 @@ type ExpandTree struct { // subject set SubjectSet *SubjectSet `json:"subject_set,omitempty"` - // - // union Union - // exclusion Exclusion - // intersection Intersection - // leaf Leaf + // type // Required: true // Enum: [union exclusion intersection leaf] Type *string `json:"type"` @@ -76,8 +72,6 @@ func (m *ExpandTree) validateChildren(formats strfmt.Registry) error { if err := m.Children[i].Validate(formats); err != nil { if ve, ok := err.(*errors.Validation); ok { return ve.ValidateName("children" + "." + strconv.Itoa(i)) - } else if ce, ok := err.(*errors.CompositeError); ok { - return ce.ValidateName("children" + "." + strconv.Itoa(i)) } return err } @@ -97,8 +91,6 @@ func (m *ExpandTree) validateSubjectSet(formats strfmt.Registry) error { if err := m.SubjectSet.Validate(formats); err != nil { if ve, ok := err.(*errors.Validation); ok { return ve.ValidateName("subject_set") - } else if ce, ok := err.(*errors.CompositeError); ok { - return ce.ValidateName("subject_set") } return err } @@ -182,8 +174,6 @@ func (m *ExpandTree) contextValidateChildren(ctx context.Context, formats strfmt if err := m.Children[i].ContextValidate(ctx, formats); err != nil { if ve, ok := err.(*errors.Validation); ok { return ve.ValidateName("children" + "." + strconv.Itoa(i)) - } else if ce, ok := err.(*errors.CompositeError); ok { - return ce.ValidateName("children" + "." + strconv.Itoa(i)) } return err } @@ -200,8 +190,6 @@ func (m *ExpandTree) contextValidateSubjectSet(ctx context.Context, formats strf if err := m.SubjectSet.ContextValidate(ctx, formats); err != nil { if ve, ok := err.(*errors.Validation); ok { return ve.ValidateName("subject_set") - } else if ce, ok := err.(*errors.CompositeError); ok { - return ce.ValidateName("subject_set") } return err } diff --git a/internal/httpclient/models/get_relation_tuples_response.go b/internal/httpclient/models/get_relation_tuples_response.go index e2e257e0d..e83f0dc18 100644 --- a/internal/httpclient/models/get_relation_tuples_response.go +++ b/internal/httpclient/models/get_relation_tuples_response.go @@ -56,8 +56,6 @@ func (m *GetRelationTuplesResponse) validateRelationTuples(formats strfmt.Regist if err := m.RelationTuples[i].Validate(formats); err != nil { if ve, ok := err.(*errors.Validation); ok { return ve.ValidateName("relation_tuples" + "." + strconv.Itoa(i)) - } else if ce, ok := err.(*errors.CompositeError); ok { - return ce.ValidateName("relation_tuples" + "." + strconv.Itoa(i)) } return err } @@ -90,8 +88,6 @@ func (m *GetRelationTuplesResponse) contextValidateRelationTuples(ctx context.Co if err := m.RelationTuples[i].ContextValidate(ctx, formats); err != nil { if ve, ok := err.(*errors.Validation); ok { return ve.ValidateName("relation_tuples" + "." + strconv.Itoa(i)) - } else if ce, ok := err.(*errors.CompositeError); ok { - return ce.ValidateName("relation_tuples" + "." + strconv.Itoa(i)) } return err } diff --git a/internal/httpclient/models/internal_relation_tuple.go b/internal/httpclient/models/internal_relation_tuple.go index fc6445dbb..e17517c99 100644 --- a/internal/httpclient/models/internal_relation_tuple.go +++ b/internal/httpclient/models/internal_relation_tuple.go @@ -102,8 +102,6 @@ func (m *InternalRelationTuple) validateSubjectSet(formats strfmt.Registry) erro if err := m.SubjectSet.Validate(formats); err != nil { if ve, ok := err.(*errors.Validation); ok { return ve.ValidateName("subject_set") - } else if ce, ok := err.(*errors.CompositeError); ok { - return ce.ValidateName("subject_set") } return err } @@ -132,8 +130,6 @@ func (m *InternalRelationTuple) contextValidateSubjectSet(ctx context.Context, f if err := m.SubjectSet.ContextValidate(ctx, formats); err != nil { if ve, ok := err.(*errors.Validation); ok { return ve.ValidateName("subject_set") - } else if ce, ok := err.(*errors.CompositeError); ok { - return ce.ValidateName("subject_set") } return err } diff --git a/internal/httpclient/models/patch_delta.go b/internal/httpclient/models/patch_delta.go index e29ac7336..2a02a7efb 100644 --- a/internal/httpclient/models/patch_delta.go +++ b/internal/httpclient/models/patch_delta.go @@ -97,8 +97,6 @@ func (m *PatchDelta) validateRelationTuple(formats strfmt.Registry) error { if err := m.RelationTuple.Validate(formats); err != nil { if ve, ok := err.(*errors.Validation); ok { return ve.ValidateName("relation_tuple") - } else if ce, ok := err.(*errors.CompositeError); ok { - return ce.ValidateName("relation_tuple") } return err } @@ -127,8 +125,6 @@ func (m *PatchDelta) contextValidateRelationTuple(ctx context.Context, formats s if err := m.RelationTuple.ContextValidate(ctx, formats); err != nil { if ve, ok := err.(*errors.Validation); ok { return ve.ValidateName("relation_tuple") - } else if ce, ok := err.(*errors.CompositeError); ok { - return ce.ValidateName("relation_tuple") } return err } diff --git a/internal/httpclient/models/relation_query.go b/internal/httpclient/models/relation_query.go index 039caa854..e07d68ce3 100644 --- a/internal/httpclient/models/relation_query.go +++ b/internal/httpclient/models/relation_query.go @@ -74,8 +74,6 @@ func (m *RelationQuery) validateSubjectSet(formats strfmt.Registry) error { if err := m.SubjectSet.Validate(formats); err != nil { if ve, ok := err.(*errors.Validation); ok { return ve.ValidateName("subject_set") - } else if ce, ok := err.(*errors.CompositeError); ok { - return ce.ValidateName("subject_set") } return err } @@ -104,8 +102,6 @@ func (m *RelationQuery) contextValidateSubjectSet(ctx context.Context, formats s if err := m.SubjectSet.ContextValidate(ctx, formats); err != nil { if ve, ok := err.(*errors.Validation); ok { return ve.ValidateName("subject_set") - } else if ce, ok := err.(*errors.CompositeError); ok { - return ce.ValidateName("subject_set") } return err } diff --git a/internal/relationtuple/read_server.go b/internal/relationtuple/read_server.go index 70783fff0..593ef2c5b 100644 --- a/internal/relationtuple/read_server.go +++ b/internal/relationtuple/read_server.go @@ -53,7 +53,6 @@ type getRelationsParams struct { // Namespace of the Relation Tuple // // in: query - // required: true Namespace string `json:"namespace"` // Object of the Relation Tuple diff --git a/internal/relationtuple/read_server_test.go b/internal/relationtuple/read_server_test.go index 4942e13a9..8f323dba7 100644 --- a/internal/relationtuple/read_server_test.go +++ b/internal/relationtuple/read_server_test.go @@ -91,6 +91,32 @@ func TestReadHandlers(t *testing.T) { assert.Equal(t, "", respMsg.NextPageToken) }) + t.Run("case=return tuples without namespace", func(t *testing.T) { + obj := t.Name() + + rts := []*relationtuple.InternalRelationTuple{ + { + Namespace: nspace.Name, + Object: obj, + Relation: "r1", + Subject: &relationtuple.SubjectID{ID: "s1"}, + }, + } + + require.NoError(t, reg.RelationTupleManager().WriteRelationTuples(context.Background(), rts...)) + + resp, err := ts.Client().Get(ts.URL + relationtuple.RouteBase + "?" + url.Values{ + "object": {obj}, + }.Encode()) + require.NoError(t, err) + assert.Equal(t, resp.StatusCode, http.StatusOK) + + var respMsg relationtuple.GetResponse + require.NoError(t, json.NewDecoder(resp.Body).Decode(&respMsg)) + assert.Equal(t, 1, len(respMsg.RelationTuples)) + assert.Equal(t, "", respMsg.NextPageToken) + }) + t.Run("case=returns bad request on malformed subject", func(t *testing.T) { resp, err := ts.Client().Get(ts.URL + relationtuple.RouteBase + "?" + url.Values{ "subject": {"not#a valid subject"}, diff --git a/spec/api.json b/spec/api.json index a5b6305c9..ad501c121 100755 --- a/spec/api.json +++ b/spec/api.json @@ -89,6 +89,12 @@ "description": "Relation of the Subject Set", "name": "subject_set.relation", "in": "query" + }, + { + "type": "integer", + "format": "int64", + "name": "max-depth", + "in": "query" } ], "responses": { @@ -192,6 +198,12 @@ "schema": { "$ref": "#/definitions/RelationQuery" } + }, + { + "type": "integer", + "format": "int64", + "name": "max-depth", + "in": "query" } ], "responses": { @@ -316,8 +328,7 @@ "type": "integer", "format": "int64", "name": "max-depth", - "in": "query", - "required": true + "in": "query" } ], "responses": { @@ -536,8 +547,7 @@ "type": "string", "description": "Namespace of the Relation Tuple", "name": "namespace", - "in": "query", - "required": true + "in": "query" }, { "type": "string", @@ -1065,8 +1075,7 @@ "enum": [ "insert", "delete" - ], - "x-go-enum-desc": "insert ActionInsert\ndelete ActionDelete" + ] }, "relation_tuple": { "$ref": "#/definitions/InternalRelationTuple" @@ -1141,15 +1150,13 @@ "$ref": "#/definitions/SubjectSet" }, "type": { - "description": "\nunion Union\nexclusion Exclusion\nintersection Intersection\nleaf Leaf", "type": "string", "enum": [ "union", "exclusion", "intersection", "leaf" - ], - "x-go-enum-desc": "union Union\nexclusion Exclusion\nintersection Intersection\nleaf Leaf" + ] } } }, From aa9107843b804a8cae41fdfc45e869096297e6fc Mon Sep 17 00:00:00 2001 From: vancanhuit Date: Sun, 12 Dec 2021 14:06:59 +0700 Subject: [PATCH 2/3] test: update --- internal/relationtuple/read_server_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/internal/relationtuple/read_server_test.go b/internal/relationtuple/read_server_test.go index 8f323dba7..96b69075b 100644 --- a/internal/relationtuple/read_server_test.go +++ b/internal/relationtuple/read_server_test.go @@ -114,6 +114,7 @@ func TestReadHandlers(t *testing.T) { var respMsg relationtuple.GetResponse require.NoError(t, json.NewDecoder(resp.Body).Decode(&respMsg)) assert.Equal(t, 1, len(respMsg.RelationTuples)) + assert.Contains(t, rts, respMsg.RelationTuples[0]) assert.Equal(t, "", respMsg.NextPageToken) }) From 635962c6351d33971fb1e1010c51b3e95ea183b3 Mon Sep 17 00:00:00 2001 From: Patrik Date: Mon, 13 Dec 2021 13:38:09 +0100 Subject: [PATCH 3/3] simplify test type conversion --- internal/e2e/sdk_client_test.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/internal/e2e/sdk_client_test.go b/internal/e2e/sdk_client_test.go index f6f009688..74addcb98 100644 --- a/internal/e2e/sdk_client_test.go +++ b/internal/e2e/sdk_client_test.go @@ -213,9 +213,7 @@ func (c *sdkClient) expand(t require.TestingT, r *relationtuple.SubjectSet, dept WithNamespace(r.Namespace). WithObject(r.Object). WithRelation(r.Relation). - WithMaxDepth(func(n int64) *int64 { - return &n - }(int64(depth))), + WithMaxDepth(pointerx.Int64(int64(depth))), ) require.NoError(t, err) return buildTree(t, resp.Payload)