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

Autopatch and Readonly Fields? #73

Closed
Fishwaldo opened this issue Oct 8, 2022 · 2 comments
Closed

Autopatch and Readonly Fields? #73

Fishwaldo opened this issue Oct 8, 2022 · 2 comments

Comments

@Fishwaldo
Copy link
Contributor

I'm having a look at the new Autopatch functionality.

I send something like this:
[{"op":"replace","path":"/description","value":"MouthPieceApp123"}]

In my GET Operation, I return a model with a number of fields Marked ReadOnly, but description is not:

type appGetResponse struct {
	ID          int                  `doc:"ID of the Application" readOnly:"true" json:"id"`
	Name        string               `doc:"Name of the Application" pattern:"^[a-zA-Z0-9_]+$" minLength:"3" maxLength:"32" json:"name"`
	Description string               `doc:"Description of the Application" pattern:"^[a-zA-Z0-9_]+$" minLength:"0" maxLength:"255" json:"description,omitempty"`
	Filters     []appFiltersResponse `doc:"Filters of the Application" json:"filters,omitempty" readOnly:"true"`
	Groups      []appGroupResponse   `doc:"Groups of the Application" json:"groups,omitempty" readOnly:"true"`
}

and my PUT operation input model is:

type appPutRequest struct {
	ID   int `doc:"ID of the Application" readOnly:"true" path:"id"`
	Body struct {
		appGetResponse
	}
}

Before hitting the actual PUT Operation, Huma rejects a json-patch operation with errors such as:

{
  "title": "Unprocessable Entity",
  "status": 422,
  "detail": "Error while processing input parameters",
  "errors": [
    {
      "message": "Additional property id is not allowed",
      "location": "body",
      "value": 1
    },
    {
      "message": "Additional property groups is not allowed",
      "location": "body",
      "value": [
        {
          "Description": "Default MouthPiece App Group",
          "ID": 8589934593,
          "Name": "MouthPiece"
        }
      ]
    }
  ]
}

if I change the Body struct for my appPutRequest struct to just the mutable fields, I still get errors about additional fields - those copied from the internal GET request.

I'm guessing readonly fields should be dropped from the final request sent internally to the PUT handler, but looking over the patch code, I see we basically just copy the result of the GET operation into the json-patch library without any filtering at all. (

huma/patch.go

Line 225 in 1519e49

patched, err = patch.Apply(origWriter.Body.Bytes())
) so I'm guessing this usecase isn't handled?

@Fishwaldo
Copy link
Contributor Author

Also - regarding the comparision between the original and patched inputs here:

huma/patch.go

Line 243 in 1519e49

if bytes.Compare(patched, origWriter.Body.Bytes()) == 0 {

if I read json-patch documentation, we should use:

jsonpatch.Equal(document1, document2)

@danielgtaylor
Copy link
Owner

Take a look at #63 which is related to this problem and would enable you to drop read-only fields on the input side.

Perhaps the better solution is to have the auto-patch code detect and remove read-only fields. I'd be open to a PR that implements that!

danielgtaylor added a commit that referenced this issue Nov 6, 2023
fix: better comparison of encoded payloads for patch, fixes #73
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

No branches or pull requests

2 participants