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

Change DocumentAttribute.Required default value to true (#1007) #1023

Merged
merged 1 commit into from
Sep 3, 2024
Merged
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
23 changes: 5 additions & 18 deletions docs/guide/http/marten.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,26 +69,13 @@ public static IMartenOp Approve([Document("number")] Invoice invoice)
<sup><a href='https://github.com/JasperFx/wolverine/blob/main/src/Http/WolverineWebApi/Marten/Documents.cs#L54-L63' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_overriding_route_argument_with_document_attribute' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

You can also combine the behavior of `[Document]` and `[Required]` through a single attribute like this:
In the code above, if the `Invoice` document does not exist, the route will stop and return a status code 404 for Not Found.

<!-- snippet: sample_using_Document_required -->
<a id='snippet-sample_using_document_required'></a>
```cs
[WolverinePost("/api/tenants/{tenant}/counters/{id}/inc2")]
public static IMartenOp Increment2([Document(Required = true)] Counter counter)
{
counter = counter with { Count = counter.Count + 1 };
return MartenOps.Store(counter);
}
```
<sup><a href='https://github.com/JasperFx/wolverine/blob/main/src/Http/Wolverine.Http.Tests/Bugs/Bug_865_returning_IResult_using_Auto_codegen.cs#L120-L129' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_using_document_required' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

In the code above, if the `Counter` document does not exist, the route will stop and return a status code 404 for Not Found.
If you, for whatever reason, want your handler executed even if the document does not exist, then you can set the `DocumentAttribute.Required` property to `false`.

:::warning
It is recommended to use `[Document(Required = true)]` instead of the combination of `[Document][Required]`!
If the document is `NULL`, the former skip your `Load` / `Before` / etc. method, while the latter will still call it.
:::info
Starting with Wolverine 3 `DocumentAttribute.Required = true` is the default behavior.
In previous versions the default value was `false`.
:::

However, if the document is soft-deleted your endpoint will still be executed.
Expand Down
4 changes: 2 additions & 2 deletions src/Http/Wolverine.Http.Marten/DocumentAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ public DocumentAttribute(string routeArgumentName)

/// <summary>
/// Should the absence of this document cause the endpoint to return a 404 Not Found response?
/// Default -- for backward compatibility -- is false.
/// Default is <c>true</c>.
/// </summary>
public bool Required { get; set; } = false;
public bool Required { get; set; } = true;

/// <summary>
/// If the document is soft-deleted, whether the endpoint should receive the document (<c>true</c>) or NULL (<c>false</c>).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public static (IResult, IMartenOp) Increment([Document(Required = true)] Counter
#region sample_using_Document_required

[WolverinePost("/api/tenants/{tenant}/counters/{id}/inc2")]
public static IMartenOp Increment2([Document(Required = true)] Counter counter)
public static IMartenOp Increment2([Document] Counter counter)
{
counter = counter with { Count = counter.Count + 1 };
return MartenOps.Store(counter);
Expand Down
4 changes: 2 additions & 2 deletions src/Http/WolverineWebApi/Marten/DocumentRequiredEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ public static ProblemDetails Load(Invoice? invoice)
}

[WolverineGet("document-required/separate-attributes/{id}")]
public static Invoice SeparateAttributes([Document][Required] Invoice invoice)
public static Invoice SeparateAttributes([Document(Required = false)][Required] Invoice invoice)
{
return invoice;
}

[WolverineGet("document-required/document-attribute-only/{id}")]
public static Invoice DocumentAttributeOnly([Document(Required = true)] Invoice invoice)
public static Invoice DocumentAttributeOnly([Document] Invoice invoice)
{
return invoice;
}
Expand Down
Loading