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

feat: implement partial client updates #2411

Merged
merged 18 commits into from
Apr 5, 2021

Conversation

mattbonnell
Copy link
Contributor

@mattbonnell mattbonnell commented Mar 19, 2021

Related issue

#2385

Proposed changes

Checklist

  • I have read the contributing guidelines.
  • I have read the security policy.
  • I confirm that this pull request does not address a security
    vulnerability. If this pull request addresses a security. vulnerability, I
    confirm that I got green light (please contact
    security@ory.sh) from the maintainers to push
    the changes.
  • I have added tests that prove my fix is effective or that my feature
    works.
  • I have added or changed the documentation.

Further comments

@mattbonnell
Copy link
Contributor Author

wow, git made such a strange diff :) probably easiest to just look at the client/handler.go file directly

@mattbonnell
Copy link
Contributor Author

getting the following error when I try to regenerate the sdk:

❯ make sdk
GOBIN=/Users/mattbonnell/dev/go/github.com/ory/hydra/.bin/ go install  github.com/ory/cli
# github.com/ory/jsonschema/v3/httploader
../../../../../go/pkg/mod/github.com/ory/jsonschema/v3@v3.0.1/httploader/httploader.go:27:15: undefined: httpx.NewResilientClientLatencyToleranceMedium
# github.com/ory/cli/cmd/dev/newsletter
../../../../../go/pkg/mod/github.com/ory/cli@v0.0.35/cmd/dev/newsletter/pkg.go:86:12: undefined: httpx.NewResilientClientLatencyToleranceMedium
make: *** [.bin/cli] Error 2

Copy link
Member

@aeneasr aeneasr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks pretty much like what I imagined! We obviously need a few tests and so on :) But I think this is almost good as is

"net/http"
"time"

jsonpatch "github.com/evanphx/json-patch"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this the best library for the job? What alternatives do we have?

Copy link
Contributor Author

@mattbonnell mattbonnell Mar 23, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The JSON Patch site gives two options for Go implementations, this and another library, https://github.com/mattbaird/jsonpatch. Between the two, this one has more stars (551 vs 89) and more contributors (41 vs 8). This one was also updated more recently (January vs last August). Additionally, this library also supports Merge Patch, which was indicated as a potential alternative in ory/meta#2.

For these reasons, I felt this was the better choice between the two.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfect, thank you!

Comment on lines 202 to 197
original, err := json.Marshal(c)
if err != nil {
h.r.Writer().WriteError(w, r, err)
return
}
modified, err := patch.Apply(original)
if err != nil {
h.r.Writer().WriteError(w, r, err)
return
}
if err := json.Unmarshal(modified, c); err != nil {
h.r.Writer().WriteError(w, r, err)
return
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we could wrap this, including DecodePatch, in a helper function in x and add a few test there? Alternatively we could also add this to ory/x/decoderx

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that would be a good idea! Will investigate this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, thank you :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done! defined a helper in x/json.go

@mattbonnell mattbonnell force-pushed the feat/partial-client-updates branch 2 times, most recently from 91669b5 to 90081b5 Compare March 24, 2021 13:56
@mattbonnell mattbonnell marked this pull request as ready for review March 24, 2021 14:01
@aeneasr
Copy link
Member

aeneasr commented Mar 24, 2021

getting the following error when I try to regenerate the sdk:

You can bum ory/cli to a newer version which should fix that!

@mattbonnell
Copy link
Contributor Author

getting the following error when I try to regenerate the sdk:

You can bum ory/cli to a newer version which should fix that!

Hey, can you help me understand the make sdk rule? The first command is:
swagger generate spec -m -o ./spec/api.json -x internal/httpclient -x gopkg.in/square/go-jose.v2

Isn't internal/httpclient the swagger-generated client code? I think I'm missing something here, this seems circular. How can we go about adding definitions to the swagger spec in this case? Additionally, it seems the sdk rule doesn't consider changes made in the client package.

I had added the PatchRequest and PatchDocument definitions to api.json, but this would overwrite them and then I get some other errors. If I remove this first line, then the rule runs as expected and I end up getting the new generated code in internal/httpclient (check the files which this PR adds to internal/httpclient).

The PR is failing in CI currently at this sdk generation step.

@aeneasr
Copy link
Member

aeneasr commented Mar 24, 2021

I'll look into this now

@aeneasr
Copy link
Member

aeneasr commented Mar 24, 2021

make sdk works fine for me but fails with:

The swagger spec at "./spec/api.json" is invalid against swagger specification 2.0.
See errors below:
- "updateOAuth2Client" is defined 2 times

because

// swagger:route PATCH /clients/{id} admin updateOAuth2Client

and

// swagger:route PUT /clients/{id} admin updateOAuth2Client

share the operation id. So one should be update and the other maybe patchOAuth2Client?

spec/api.json Outdated
"schemes": ["http", "https"],
"tags": ["admin"],
"summary": "Patch an OAuth 2.0 Client",
"operationId": "patchOAuth2Client",
Copy link
Contributor Author

@mattbonnell mattbonnell Mar 24, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aeneasr following up from your last comment:

that's the thing though, it is patchOAuth2Client as I've defined it, but it seems when that swagger generate command is run, it replaces spec/api.json and in the replaced version it has two updateOAuth2Clients defined for some reason. If you eliminate that first generate command, everything works as expected.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ohhh, i see. i think i was doing things wrong. hang on.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok. think i've fixed it now.

@mattbonnell
Copy link
Contributor Author

Hm, generate step failing in pipeline with

#!/bin/bash -eo pipefail
swagger generate spec -m -o spec/api.json $(printf -- " -x %s" internal/httpclient -x gopkg.in/square/go-jose.v2)
expected argument for flag `-x, --exclude', but got option `-x'

@aeneasr
Copy link
Member

aeneasr commented Mar 29, 2021

LMK when good for another 👀

@mattbonnell
Copy link
Contributor Author

mattbonnell commented Mar 29, 2021

LMK when good for another 👀

hey, yes ready, looks like all checks are passing

edit: pulled in latest changes from master and there seems to be a vulnerability in the version of protobuf being used, causing ci/circleci:test to fail

@zepatrik
Copy link
Member

I opened #2434 to fix the nancy warning

@aeneasr aeneasr self-requested a review April 1, 2021 18:31
return
}

if err := x.ApplyJSONPatch(patchJSON, c); err != nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It shouldn't be allowed to update the ID of the client as that is immutable iirc!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, will fix

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed.

Copy link
Member

@aeneasr aeneasr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome! This looks pretty much good to go, but I think we need another test for the handler which also includes the ID behavior test! Another option would be to add a „deny list“ to the JSON PATCH helper which returns an error if a specific path is updated (e.g. ID). This would make it more general purpose and easier to test :)

@mattbonnell
Copy link
Contributor Author

Awesome! This looks pretty much good to go, but I think we need another test for the handler which also includes the ID behavior test! Another option would be to add a „deny list“ to the JSON PATCH helper which returns an error if a specific path is updated (e.g. ID). This would make it more general purpose and easier to test :)

done!

@aeneasr
Copy link
Member

aeneasr commented Apr 5, 2021

Thank you! Appreciate your contributions as always :)

@aeneasr aeneasr merged commit 540c89d into ory:master Apr 5, 2021
@mattbonnell mattbonnell deleted the feat/partial-client-updates branch April 5, 2021 12:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants