Fix DataUriParser to default to text/plain;charset=US-ASCII per RFC 2397#7247
Merged
stephentoub merged 5 commits intomainfrom Feb 3, 2026
Merged
Conversation
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix DataUriParser to honour RFC 2397 default behaviour
Fix DataUriParser to default to text/plain;charset=US-ASCII per RFC 2397
Jan 30, 2026
stephentoub
reviewed
Jan 30, 2026
src/Libraries/Microsoft.Extensions.AI.Abstractions/Contents/DataUriParser.cs
Outdated
Show resolved
Hide resolved
test/Libraries/Microsoft.Extensions.AI.Abstractions.Tests/Contents/DataContentTests.cs
Outdated
Show resolved
Hide resolved
test/Libraries/Microsoft.Extensions.AI.Abstractions.Tests/Contents/DataContentTests.cs
Show resolved
Hide resolved
…ests to Theory Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR updates the AI abstractions data URI handling to conform to RFC 2397 by defaulting omitted media types to text/plain;charset=US-ASCII, and adds tests to cover the new behavior.
Changes:
- Introduces
DataUriParser.DefaultMediaType(text/plain;charset=US-ASCII) and uses it inParsewhen the media-type metadata section is empty. - Extends
IsValidMediaType’s fast-path table to recognizetext/plain;charset=US-ASCIIwithout invokingMediaTypeHeaderValue.TryParse. - Adds unit tests verifying that omitted media types in data URIs default correctly and that an explicitly supplied media type still overrides the URI’s default.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
src/Libraries/Microsoft.Extensions.AI.Abstractions/Contents/DataUriParser.cs |
Adds a default media type constant, applies RFC 2397 defaulting logic when the metadata span is empty, and recognizes the default in the known media types fast-path table. |
test/Libraries/Microsoft.Extensions.AI.Abstractions.Tests/Contents/DataContentTests.cs |
Adds tests to validate defaulting behavior for omitted media types (including base64 and non-base64 cases, URI vs string constructors) and that an explicit mediaType parameter overrides the default. |
stephentoub
reviewed
Jan 30, 2026
test/Libraries/Microsoft.Extensions.AI.Abstractions.Tests/Contents/DataContentTests.cs
Outdated
Show resolved
Hide resolved
stephentoub
approved these changes
Jan 30, 2026
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
stephentoub
approved these changes
Jan 30, 2026
eiriktsarpalis
approved these changes
Feb 3, 2026
This was referenced Feb 16, 2026
Open
Open
Open
Open
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
text/plain;charset=US-ASCIIDataUriParser.csandDataContent.csDataContentTests.csDataUriParser.Parsemethod to return default media type when omittedDefaultMediaType => DefaultMediaTypeto top of known media types switch (per review feedback)Ctor_OmittedMediaType_DefaultsToTextPlaintest to use[Theory]with[InlineData]Ctor_OmittedMediaType_CanBeOverriddentest to use[Theory]with[InlineData]Original prompt
DataUriParserdoes not honour RFC 2397 default behaviour when media type omitted #7246💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.
Microsoft Reviewers: Open in CodeFlow