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

Data List: Extends ContentmentContentContext to access parentId Form and JSON data #389

Merged
merged 3 commits into from
Mar 31, 2024

Conversation

leekelleher
Copy link
Owner

@leekelleher leekelleher commented Mar 30, 2024

Description

Transpires that the client-side patch I submitted for the BlockList editor (in umbraco/Umbraco-CMS#15063), didn't work. This is due to how ContentmentContentContext was trying to access the id and parentId parameters (on the querystring URL). However, the BlockList editor makes a POST request to the GetEmptyByKeys() endpoint, but not only that, the parentId is within a JSON blob sent in the body of the POST request. Which sounds fine, read the JSON from the request body and all good.

Not so fast! Turns out that in ASP.NET Core, the Request.Body can only be read once, which in the case of the GetEmptyByKeys() endpoint, that's done with the model-binding (of the ContentTypesByKeys type).

So to access the request body contents multiple times, you need to enable request.EnableBuffering(). From searching through Umbraco source, I found that there is an extension method called GetRawBodyStringAsync(), which even has the .EnableBuffering() call in it, super, job done.

Not so fast! Turns out that request.EnableBuffering() must be done at app-startup. Now, I needed a way to add a middleware to configure the request buffering, but I didn't want to enable this for all request (as it's considered a small performance hit), so I wanted to have a conditional middleware for a specific route/path.

I found this blog post super helpful in figuring out how to do this:
https://markb.uk/asp-net-core-read-raw-request-body-as-string.html

TL,DR; Contentment adds a conditional middleware to the GetEmptyByKeys() API route to enable buffering of the request body contents, so that it can be accessed again during the request lifecycle.

Please note, that I have targeted this PR on (upcoming) Contentment v5, as it (currently) only relevant for the BlockList editor patch available in Umbraco v13.1.0. I do not intend to backport it to Contentment v4.6.x, that is, unless there is a good enough reason to do so.

Related Issues?

Types of changes

  • Documentation change
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist

  • My code follows the coding style of this project.
  • My changes generate no new warnings.
  • My change requires a change to the documentation.
  • I have updated the corresponding documentation.
  • I have read the CONTRIBUTING and CODE_OF_CONDUCT documents.

the `Request.Body` to see if has a JSON blob with a `parentId` value.

Fixes #388
to add conditional middleware for BlockList's `GetEmptyByKeys()` call,
to ensure that the `Request.Body` is still accessible after the initial binding.
@leekelleher leekelleher added this to the 5.0.0 milestone Mar 30, 2024
@leekelleher leekelleher self-assigned this Mar 30, 2024
@leekelleher leekelleher merged commit 0a7a26e into dev/v5.x Mar 31, 2024
@leekelleher leekelleher deleted the dev/wip/content-context-blocklist-parentid-fix branch April 19, 2024 08:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant