Skip to content

Make UriContent mediaType parameter optional with inference from URI file extension#7398

Merged
stephentoub merged 4 commits intomainfrom
copilot/update-uri-content-ctors-media-type
Mar 16, 2026
Merged

Make UriContent mediaType parameter optional with inference from URI file extension#7398
stephentoub merged 4 commits intomainfrom
copilot/update-uri-content-ctors-media-type

Conversation

Copy link
Contributor

Copilot AI commented Mar 16, 2026

Summary

Updates UriContent's constructors to make the mediaType parameter optional. When not supplied or null is passed, the media type is inferred from the file extension on the URI using the existing MediaTypeMap.GetMediaType utility. If there is no file extension or it's unknown, "application/octet-stream" is used as the default. Both absolute and relative URIs are supported for inference.

Changes

  • Modified both UriContent(string, string?) and UriContent(Uri, string?) constructors to accept nullable mediaType with default null
  • Added private InferMediaType helper that safely handles both absolute URIs (via Uri.AbsolutePath) and relative URIs (via Uri.OriginalString with query string/fragment stripping)
  • Added DefaultMediaType constant for "application/octet-stream" fallback (same pattern used in DataContent)
  • Updated XML doc comments to document the new inference behavior
  • Updated API baseline JSON to reflect new constructor signatures
  • Added comprehensive tests for inference, fallback, and explicit override scenarios covering absolute URIs, relative URIs, query string parameters, and URI fragments

Usage

// Before: mediaType was required
var content = new UriContent("http://example.com/photo.jpg", "image/jpeg");

// After: mediaType is optional and inferred from the file extension
var content = new UriContent("http://example.com/photo.jpg"); // MediaType = "image/jpeg"
var content2 = new UriContent("http://example.com/data"); // MediaType = "application/octet-stream"
var content3 = new UriContent("http://example.com/photo.jpg", "custom/type"); // MediaType = "custom/type"

// Also works with relative URIs
var content4 = new UriContent(new Uri("photo.jpg", UriKind.Relative)); // MediaType = "image/jpeg"
var content5 = new UriContent(new Uri("photo.jpg?size=large#top", UriKind.Relative)); // MediaType = "image/jpeg"
Microsoft Reviewers: Open in CodeFlow

💬 Send tasks to Copilot coding agent from Slack and Teams to turn conversations into code. Copilot posts an update in your thread when it's finished.

@stephentoub stephentoub marked this pull request as ready for review March 16, 2026 17:41
@stephentoub stephentoub requested a review from a team as a code owner March 16, 2026 17:41
Copilot AI review requested due to automatic review settings March 16, 2026 17:41
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR updates UriContent to make mediaType optional, inferring it from the URI’s file extension (defaulting to application/octet-stream when unknown), and updates tests and API baselines accordingly.

Changes:

  • Make UriContent constructors accept string? mediaType = null and infer media type when omitted.
  • Add test coverage validating inference behavior and defaulting rules.
  • Update the stable API baseline to reflect the optional/nullable mediaType parameter.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
test/Libraries/Microsoft.Extensions.AI.Abstractions.Tests/Contents/UriContentTests.cs Updates ctor validation expectations and adds inference/defaulting tests.
src/Libraries/Microsoft.Extensions.AI.Abstractions/Microsoft.Extensions.AI.Abstractions.json Updates API baseline signatures for the modified constructors.
src/Libraries/Microsoft.Extensions.AI.Abstractions/Contents/UriContent.cs Implements media type inference and introduces a default media type constant.

You can also share your feedback on Copilot code review. Take the survey.

…URIs with query strings and fragments

Co-authored-by: stephentoub <[email protected]>
@stephentoub stephentoub requested a review from jozkee March 16, 2026 17:57
@stephentoub stephentoub enabled auto-merge (squash) March 16, 2026 17:57
@stephentoub stephentoub merged commit 5b8efcf into main Mar 16, 2026
6 checks passed
@stephentoub stephentoub deleted the copilot/update-uri-content-ctors-media-type branch March 16, 2026 22:58
jeffhandley pushed a commit to jeffhandley/extensions that referenced this pull request Mar 17, 2026
…file extension (dotnet#7398)

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Co-authored-by: stephentoub <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-ai Microsoft.Extensions.AI libraries

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants