Skip to content

Swagger

Cratis Arc provides enhanced Swagger/OpenAPI support through the Cratis.Arc.Swagger package. This extension automatically configures Swagger to properly handle the Arc’s specific features and conventions.

The Swagger extension adds several filters and enhancements to provide accurate API documentation for:

  • Concepts - Properly represents concept types as their underlying primitive types
  • Commands - Adds correct response schemas with validation and error handling
  • Queries - Includes pagination/sorting parameters and proper response schemas
  • FromRequest attributes - Correctly handles complex model binding scenarios
  • Model-bound endpoints - Supports minimal API endpoints for commands and queries

To use the Swagger enhancements, add the extension to your Swagger configuration:

builder.Services.AddSwaggerGen(options =>
{
options.AddConcepts();
});

The AddConcepts() method adds all the necessary filters and operation filters automatically.

Automatically maps concept types (types inheriting from ConceptAs<T>) to their underlying primitive types in the Swagger schema. This ensures that concepts appear as their actual data types (string, int, Guid, etc.) rather than complex objects in the API documentation.

Example:

public class UserId : ConceptAs<Guid>;
// In Swagger, UserId parameters will appear as string (UUID format)
// instead of a complex object with a Value property

Enhances command endpoints by:

  • Adding proper CommandResult or CommandResult<T> response schemas
  • Including standard HTTP status codes (200, 400, 403, 500) with appropriate error schemas
  • Handling void/Task return types correctly
  • Supporting concept return types

Enhances query endpoints by:

  • Adding QueryResult response schemas
  • Including standard HTTP status codes with error handling
  • Automatically adding pagination and sorting parameters for enumerable results
  • Supporting concept return types

Properly handles the [FromRequest] attribute by:

  • Removing the parameter from the query string/path parameters
  • Adding it as a request body with the correct JSON schema
  • Supporting complex model binding scenarios

Provides support for minimal API endpoints that use model binding for commands and queries, ensuring they appear correctly in the Swagger documentation.

For query endpoints that return enumerable results, the following query parameters are automatically added to the Swagger documentation:

ParameterTypeDescription
sortBystringField name to sort by
sortDirectionstringSort direction (asc or desc)
pageSizeintegerNumber of items per page
pageintegerPage number (0-based)

Provides proper schema generation for enum types, ensuring they are documented correctly in the API specification.

The Swagger extension automatically adds consistent response schemas for all command and query endpoints:

  • Commands: CommandResult or CommandResult<T>
  • Queries: QueryResult with the actual data type
  • 400 Bad Request: Validation errors or malformed requests
  • 403 Forbidden: Authorization failures
  • 500 Internal Server Error: Unexpected server errors

All error responses use the same result schema as success responses but with error information populated.

The Swagger extension seamlessly integrates with other Arc features:

  • FromRequest: Properly documents complex model binding
  • Commands: Accurate documentation of command endpoints and responses
  • Queries: Complete documentation including pagination for collection results
  • Validation: Error responses include validation failure information
  • Without Wrappers: Works correctly with unwrapped responses

This ensures that your API documentation accurately reflects the actual behavior and capabilities of your Arc-based API.