Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions .docfx/BuildDocfxImage.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
$version = minver -i -t v -v w
docfx metadata docfx.json
docker buildx build -t carter-docfx:$version --platform linux/arm64,linux/amd64 --load -f Dockerfile.docfx .
get-childItem -recurse -path api -include *.yml, .manifest | remove-item
& (Join-Path $PSScriptRoot 'BuildRunActions.ps1')
try {
docker buildx build -t carter-docfx:$version --platform linux/arm64,linux/amd64 --load -f Dockerfile.docfx .
}
finally {
get-childItem -recurse -path api -include *.yml, .manifest | remove-item
if (Test-Path -LiteralPath (Join-Path $PSScriptRoot 'run-actions')) {
Remove-Item -LiteralPath (Join-Path $PSScriptRoot 'run-actions') -Recurse -Force
}
}
503 changes: 503 additions & 0 deletions .docfx/BuildRunActions.ps1

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
---
uid: Codebelt.Extensions.Carter.AspNetCore.Newtonsoft.Json.NewtonsoftJsonNegotiator
summary: *content
example: [*content]
---

The `NewtonsoftJsonNegotiator` integrates Newtonsoft.Json serialization into the Carter response pipeline. Register it as the response negotiator to have all `HttpResponse.Negotiate()` calls produce JSON output using Newtonsoft.Json's configurable serializer.

[!INCLUDE [availability-modern](../../includes/availability-modern.md)]

### Example

The following example demonstrates how to register `NewtonsoftJsonNegotiator` with a custom converter and negotiate a response model.
To inspect the JSON a Carter client would receive, pass the requested media type to the negotiator and serialize a response into a memory stream. The stream makes the response body readable without starting a server.

```csharp
using System;
Expand All @@ -27,14 +23,16 @@ namespace DocfxExample;

public static class NewtonsoftJsonNegotiatorExample
{
public static async Task DemonstrateAsync()
public static async Task Main()
{
var options = Options.Create(new NewtonsoftJsonFormatterOptions());
var negotiator = new NewtonsoftJsonNegotiator(options);
var acceptHeader = MediaTypeHeaderValue.Parse("application/json");
if (negotiator.CanHandle(acceptHeader))
{
using var body = new MemoryStream();
var context = new DefaultHttpContext();
context.Response.Body = body;
context.Request.Headers.Accept = "application/json";
var model = new { Name = "Newtonsoft", Value = 42 };
await negotiator.Handle(context.Request, context.Response, model, CancellationToken.None);
Expand All @@ -45,3 +43,7 @@ public static class NewtonsoftJsonNegotiatorExample
}
}
```

<!-- docfx-run-actions:start -->
<div class="docfx-run-actions" data-example-id="93c4f046477745c9b8272f18"><a href="~/run-actions/93c4f046477745c9b8272f18/native.ps1" data-transport="native">View Native Script</a> · <a href="~/run-actions/93c4f046477745c9b8272f18/docker.ps1" data-transport="docker">View Docker Script</a></div>
<!-- docfx-run-actions:end -->
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
---
uid: Codebelt.Extensions.Carter.AspNetCore.Text.Json.JsonResponseNegotiator
summary: *content
example: [*content]
---

The `JsonResponseNegotiator` integrates System.Text.Json serialization into the Carter response pipeline. Register it as the response negotiator to have all `HttpResponse.Negotiate()` calls produce JSON output using the modern, high-performance System.Text.Json serializer.

[!INCLUDE [availability-modern](../../includes/availability-modern.md)]

### Example

The following example demonstrates how to register `JsonResponseNegotiator` and negotiate a response model with custom options.
To inspect the System.Text.Json output for a response model, pass the requested media type to the negotiator and serialize into a memory stream. The explicit type information resolver allows the formatter to serialize the anonymous model even when the host disables reflection-based serialization by default.

```csharp
using System;
using System.IO;
using System.Text;
using System.Text.Json.Serialization.Metadata;
using System.Threading;
using System.Threading.Tasks;
using Codebelt.Extensions.Carter.AspNetCore.Text.Json;
Expand All @@ -27,14 +24,18 @@ namespace DocfxExample;

public static class JsonResponseNegotiatorExample
{
public static async Task DemonstrateAsync()
public static async Task Main()
{
var options = Options.Create(new JsonFormatterOptions());
var formatterOptions = new JsonFormatterOptions();
formatterOptions.Settings.TypeInfoResolver = new DefaultJsonTypeInfoResolver();
var options = Options.Create(formatterOptions);
var negotiator = new JsonResponseNegotiator(options);
var acceptHeader = MediaTypeHeaderValue.Parse("application/json");
if (negotiator.CanHandle(acceptHeader))
{
using var body = new MemoryStream();
var context = new DefaultHttpContext();
context.Response.Body = body;
context.Request.Headers.Accept = "application/json";
var model = new { Name = "System.Text.Json", Value = 42 };
await negotiator.Handle(context.Request, context.Response, model, CancellationToken.None);
Expand All @@ -45,3 +46,7 @@ public static class JsonResponseNegotiatorExample
}
}
```

<!-- docfx-run-actions:start -->
<div class="docfx-run-actions" data-example-id="6352b06e7d9ea46088bd7abf"><a href="~/run-actions/6352b06e7d9ea46088bd7abf/native.ps1" data-transport="native">View Native Script</a> · <a href="~/run-actions/6352b06e7d9ea46088bd7abf/docker.ps1" data-transport="docker">View Docker Script</a></div>
<!-- docfx-run-actions:end -->
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
---
uid: Codebelt.Extensions.Carter.AspNetCore.Text.Yaml.YamlResponseNegotiator
summary: *content
example: [*content]
---

The `YamlResponseNegotiator` integrates YamlDotNet serialization into the Carter response pipeline. Register it as the response negotiator to have all `HttpResponse.Negotiate()` calls produce YAML output—ideal for tooling, configuration, and DevOps-focused APIs.

[!INCLUDE [availability-modern](../../includes/availability-modern.md)]

### Example

`YamlResponseNegotiator` is constructed with an `IOptions<YamlFormatterOptions>` that controls supported media types and YamlDotNet serialization settings. Before serializing a model, the negotiator tests the client's `Accept` header via `CanHandle`; only when it returns `true` is `Handle` called to produce YAML output. `Handle` resolves the character encoding from the `Accept-Charset` header (falling back to `YamlFormatterOptions.Encoding`), sets the `Content-Type` response header, and streams the serialized YAML directly to the response body.

The following example constructs the negotiator with default options, confirms that the `application/yaml` media type is accepted, then writes an anonymous object to an in-memory response stream and reads back the serialized output:
Expand All @@ -29,14 +25,16 @@ namespace DocfxExample;

public static class YamlResponseNegotiatorExample
{
public static async Task DemonstrateAsync()
public static async Task Main()
{
var options = Options.Create(new YamlFormatterOptions());
var negotiator = new YamlResponseNegotiator(options);
var acceptHeader = MediaTypeHeaderValue.Parse("application/yaml");
if (negotiator.CanHandle(acceptHeader))
{
using var body = new MemoryStream();
var context = new DefaultHttpContext();
context.Response.Body = body;
context.Request.Headers.Accept = "application/yaml";
var model = new { Name = "YamlDotNet", Value = 42 };
await negotiator.Handle(context.Request, context.Response, model, CancellationToken.None);
Expand All @@ -47,3 +45,7 @@ public static class YamlResponseNegotiatorExample
}
}
```

<!-- docfx-run-actions:start -->
<div class="docfx-run-actions" data-example-id="bf3b685f7384b7944901e7d0"><a href="~/run-actions/bf3b685f7384b7944901e7d0/native.ps1" data-transport="native">View Native Script</a> · <a href="~/run-actions/bf3b685f7384b7944901e7d0/docker.ps1" data-transport="docker">View Docker Script</a></div>
<!-- docfx-run-actions:end -->
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
---
uid: Codebelt.Extensions.Carter.AspNetCore.Xml.XmlResponseNegotiator
summary: *content
example: [*content]
---

The `XmlResponseNegotiator` integrates System.Xml XML serialization into the Carter response pipeline. Register it as the response negotiator to have all `HttpResponse.Negotiate()` calls produce standards-compliant XML output—suitable for legacy interoperability and enterprise integration scenarios.

[!INCLUDE [availability-modern](../../includes/availability-modern.md)]

### Example

`XmlResponseNegotiator` is constructed with an `IOptions<XmlFormatterOptions>` that controls supported media types and System.Xml serialization settings, including writer encoding. Before serializing a model, the negotiator tests the client's `Accept` header via `CanHandle`; only when it returns `true` is `Handle` called to produce XML output. `Handle` resolves the character encoding from the `Accept-Charset` header (falling back to `XmlFormatterOptions.Settings.Writer.Encoding`), sets the `Content-Type` response header, and streams the serialized XML directly to the response body.

The following example constructs the negotiator with default options, confirms that the `application/xml` media type is accepted, then writes an anonymous object to an in-memory response stream and reads back the serialized output:
The following example constructs the negotiator with default options, confirms that the `application/xml` media type is accepted, then writes a public model to an in-memory response stream and reads back the serialized output:

```csharp
using System;
Expand All @@ -29,21 +25,33 @@ namespace DocfxExample;

public static class XmlResponseNegotiatorExample
{
public static async Task DemonstrateAsync()
public static async Task Main()
{
var options = Options.Create(new XmlFormatterOptions());
var negotiator = new XmlResponseNegotiator(options);
var acceptHeader = MediaTypeHeaderValue.Parse("application/xml");
if (negotiator.CanHandle(acceptHeader))
{
using var body = new MemoryStream();
var context = new DefaultHttpContext();
context.Response.Body = body;
context.Request.Headers.Accept = "application/xml";
var model = new { Name = "XML", Value = 42 };
var model = new Report { Name = "XML", Value = 42 };
await negotiator.Handle(context.Request, context.Response, model, CancellationToken.None);
context.Response.Body.Position = 0;
using var reader = new StreamReader(context.Response.Body, Encoding.UTF8);
Console.WriteLine(await reader.ReadToEndAsync());
}
}
}

public sealed class Report
{
public string Name { get; set; } = string.Empty;
public int Value { get; set; }
}
```

<!-- docfx-run-actions:start -->
<div class="docfx-run-actions" data-example-id="f53010d9cc9ec55bc07adac2"><a href="~/run-actions/f53010d9cc9ec55bc07adac2/native.ps1" data-transport="native">View Native Script</a> · <a href="~/run-actions/f53010d9cc9ec55bc07adac2/docker.ps1" data-transport="docker">View Docker Script</a></div>
<!-- docfx-run-actions:end -->
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
---
uid: Codebelt.Extensions.Carter.EndpointConventionBuilderExtensions
summary: *content
example: [*content]
---

Attach typed response metadata to any Carter or minimal API endpoint by calling `Produces<TResponse>` or `Produces` on an `IEndpointConventionBuilder`. Each call records an `IProducesResponseTypeMetadata` entry that OpenAPI tooling reads to document the endpoint's response shape and status codes.

[!INCLUDE [availability-modern](../../includes/availability-modern.md)]

> **Note:** DocFX does not yet generate complete API metadata for C# 14 extension block members ([docfx#11010](https://github.com/dotnet/docfx/issues/11010)). The extension methods themselves are fully functional; the limitation is cosmetic in generated API docs only.

### Example

`Produces<TResponse>` and `Produces` attach `IProducesResponseTypeMetadata` directly to an endpoint builder. `Produces<TResponse>` records the response body type, HTTP status code, and accepted content types; `Produces` (no type parameter) records a status-only response with no body—useful for `404`, `401`, and similar cases. Both are chainable and are typically called on a `RouteHandlerBuilder` inside a Carter module or minimal API route definition.

The following example constructs an endpoint builder directly, calls the extension members through their declaring type, and reads back the attached metadata to show what each call contributes:
The following example constructs an endpoint builder directly, applies both extension members to the convention builder, and reads back the attached metadata to show what each call contributes:

```csharp
using System;
Expand All @@ -31,7 +27,7 @@ namespace DocfxExample;

public static class ProducesExtensionExample
{
public static void Demonstrate()
public static void Main()
{
var endpointBuilder = new RouteEndpointBuilder(
context => Task.CompletedTask,
Expand All @@ -40,7 +36,7 @@ public static class ProducesExtensionExample

var fakeBuilder = new FakeEndpointConventionBuilder();
EndpointConventionBuilderExtensions.Produces<OrderSummary>(fakeBuilder, StatusCodes.Status200OK, "application/json");
EndpointConventionBuilderExtensions.Produces(fakeBuilder, StatusCodes.Status404NotFound);
fakeBuilder.Produces(StatusCodes.Status404NotFound);

foreach (var convention in fakeBuilder.Conventions)
{
Expand All @@ -66,5 +62,9 @@ public static class ProducesExtensionExample
public sealed record OrderSummary(int Id, string Name);
```

<!-- docfx-run-actions:start -->
<div class="docfx-run-actions" data-example-id="83888118c4da05f5423d7ce9"><a href="~/run-actions/83888118c4da05f5423d7ce9/native.ps1" data-transport="native">View Native Script</a> · <a href="~/run-actions/83888118c4da05f5423d7ce9/docker.ps1" data-transport="docker">View Docker Script</a></div>
<!-- docfx-run-actions:end -->

The `Produces<TResponse>` and `Produces` extension members are the same methods used when chaining on a `RouteHandlerBuilder` inside a Carter module: `app.MapGet("/orders/{id}", ...).Produces<OrderSummary>().Produces(404)`.

9 changes: 8 additions & 1 deletion .docfx/docfx.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
"build": {
"sitemap": {
"changefreq": "monthly",
"baseUrl": "https://carter.codebelt.net/"},
"baseUrl": "https://carter.codebelt.net/"
},
"xref": [
"https://docs.cuemon.net/xrefmap.yml?v=20260120",
"https://newtonsoft.codebelt.net/xrefmap.yml",
Expand Down Expand Up @@ -52,6 +53,11 @@
"files": [
"images/**"
]
},
{
"files": [
"run-actions/**"
]
}
],
"globalMetadata": {
Expand All @@ -74,6 +80,7 @@
"template": [
"default",
"modern",
"templates/modern",
"templates/ms-style"
],
"overwrite": [
Expand Down
Loading
Loading