Skip to content

JSON merge-patch support #7207

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

Merged
merged 31 commits into from
May 5, 2025
Merged

JSON merge-patch support #7207

merged 31 commits into from
May 5, 2025

Conversation

markcowl
Copy link
Contributor

@markcowl markcowl commented May 2, 2025

fix #7221(@mediaTypeHint not allowing media types with suffix)

Copy link
Contributor

github-actions bot commented May 2, 2025

All changed packages have been documented.

  • @typespec/compiler
  • @typespec/http
Show changes

@typespec/compiler - fix ✏️

Weakened rules around @mediaTypeHint decorator, allowing media type hints with suffixes like "application/merge-patch+json".

@typespec/http - feature ✏️

Implemented JSON Merge-Patch wrappers. This allows converting a type to a JSON Merge-Patch compatible update record using the MergePatchUpdate and MergePatchCreateOrUpdate templates.

@typespec/compiler - feature ✏️

Exposed experimental function isMutableType as unsafe_isMutableType.

@azure-sdk
Copy link
Collaborator

azure-sdk commented May 2, 2025

You can try these changes here

🛝 Playground 🌐 Website 🛝 VSCode Extension

@markcowl markcowl changed the title [WIP]: Merge-Patch JSON merge-patch support May 5, 2025
@markcowl markcowl marked this pull request as ready for review May 5, 2025 03:58
…name templates when visibilityMode is CreateOrUpdate.
@markcowl markcowl added this pull request to the merge queue May 5, 2025

[RFC 7396](https://www.rfc-editor.org/rfc/rfc7396) describes a standard for interpreting
a Patch request body using content-type `application/merge-patch+json`to update an existing
resource. The TypeSpec Http library provides Model templates `MergePatchUpdate` and
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
resource. The TypeSpec Http library provides Model templates `MergePatchUpdate` and
resource. The TypeSpec HTTP library provides Model templates `MergePatchUpdate` and

- `quantity` may or ay not occur in the request, if it does occur, the resource value can be updated to a new integer value or erased by sending null.
- `color` may or may not occur in the request, if it does occur, the resource value can be updated to one of the appropriate values. If set to `null` it is returned to its default value (`blue`)
- `flavor` may or may not occur in the request, if it does occur, the resource value can be updated to one of the appropriate values. If set to `null` it is returned to its default value (`vanilla`)
- `related` may or may not occur in the request, if it does occur, the resource value can be updated or erased (set to `{}`) by sending null.
Copy link
Member

Choose a reason for hiding this comment

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

"Erased" in this context (related) should set the value of the resource so that it has no related record, not an empty record, right? Are records special in this respect? It seems like if you had a resource with related: { ... } and you wanted to set it to related: {}, you would have to first send related: null and then related: {}.

Comment on lines +633 to +638
Because JSON merge-patch only describes the request body of an http request, the merge-patch
transform in TypeSpec does not allow http envelope property metadata like `@query`, `@path`,
`@header`, `@cookie`, and `@statusCode` in http requests.

The merge-patch templates will **emit an error diagnostic** if a model containing Http metadata
properties is passed in. For example:
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
Because JSON merge-patch only describes the request body of an http request, the merge-patch
transform in TypeSpec does not allow http envelope property metadata like `@query`, `@path`,
`@header`, `@cookie`, and `@statusCode` in http requests.
The merge-patch templates will **emit an error diagnostic** if a model containing Http metadata
properties is passed in. For example:
Because JSON merge-patch only describes the request body of an HTTP request, the merge-patch
transform in TypeSpec does not allow HTTP envelope property metadata like `@query`, `@path`,
`@header`, `@cookie`, and `@statusCode` in HTTP requests.
The merge-patch templates will **emit an error diagnostic** if a model containing HTTP metadata
properties is passed in. For example:


// THIS WILL RESULT IN AN ERROR
@patch op update(...MergePatchUpdate<ResourceWithMetadata>): ResourceWithMetadata;
```
Copy link
Member

Choose a reason for hiding this comment

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

Perhaps worth mentioning also that it is invalid to pass a model into MergePatchUpdate or MergePatchCreateOrUpdate that contains null anywhere in its ordinary value space, since null has special meaning in JSON Merge Patch.

Merged via the queue into microsoft:main with commit 6769807 May 5, 2025
20 of 22 checks passed
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.

[Bug]: application/problem+json not supported in mediaTypeHint
4 participants