DarkPatterns.OpenApiCodegen.Server.Mvc
0.18.4
See the version list below for details.
dotnet add package DarkPatterns.OpenApiCodegen.Server.Mvc --version 0.18.4
NuGet\Install-Package DarkPatterns.OpenApiCodegen.Server.Mvc -Version 0.18.4
<PackageReference Include="DarkPatterns.OpenApiCodegen.Server.Mvc" Version="0.18.4" />
paket add DarkPatterns.OpenApiCodegen.Server.Mvc --version 0.18.4
#r "nuget: DarkPatterns.OpenApiCodegen.Server.Mvc, 0.18.4"
// Install DarkPatterns.OpenApiCodegen.Server.Mvc as a Cake Addin #addin nuget:?package=DarkPatterns.OpenApiCodegen.Server.Mvc&version=0.18.4 // Install DarkPatterns.OpenApiCodegen.Server.Mvc as a Cake Tool #tool nuget:?package=DarkPatterns.OpenApiCodegen.Server.Mvc&version=0.18.4
Adds source generators to generate C# controller base classes from an OpenAPI specification file.
Add this package, select the OpenAPI specification file from your project, and set the build action to OpenApiSchemaMvcServer
. Within that folder's namespace, you'll automatically get the model and controller classes to implement your interface.
A services.AddOpenApi...
extension method is added for use in your startup file to ensure you have all base controllers implemented. This function call is not required; it only adds services for MVC.
You can also directly add a reference within the .csproj
file as follows:
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<OpenApiSchemaMvcServer Include="schemas/petstore.yaml" />
</ItemGroup>
</Project>
This integrates during the build phase, so you can be sure your classes are up to date with your schema documentation.
Additional yaml files referenced via $ref
in your OpenAPI documents should be
specified as the build action JsonSchemaDocument
to be included in watch mode
and to control the Namespace.
Requirements
- System.Text.Json
- C# 11+
- .NET 8
- Roslyn 3.11 or Roslyn 4.0+ (VS 2022 or later, or other up-to-date Roslyn installation.)
- A reference to
DarkPatterns.OpenApiCodegen.Json.Extensions
Other notes
- Take care to include
PrivateAssets="All"
on the reference to the analyzer package; you do not need to ship the package itself. - The namespace of the generated controllers and models will, by default, match the default namespace and the path for the file within the project (or the Link, if provided.)
Configuration
Additional settings may be added within the .csproj
. For example:
<OpenApiSchemaMvcServer Include="schemas/petstore.yaml" Namespace="My.Extensions" Configuration="path/to/config.yaml" />
Namespace
- Overrides the namespace detected by the default namespace and path of the schema fileConfiguration
- Additional configuration settings specific to this schema. See the configuration yaml documentation below.PathPrefix
- Prefixes the paths of the generated paths with the given path.SchemaId
- Specifies the "retrieval URI" used when resolving relative paths to external files. Otherwise, the absolute file-scheme URL will be used.
In addition, adding the following to an ItemGroup in the csproj (or adding the
yaml file with the build action OpenApiSchemaCSharpServerOptions
via Visual
Studio) will set the configuration yaml file for all client schemas that do not
have one specifically set.
<OpenApiSchemaCSharpServerOptions Include="path/to/config.yaml" />
Configuration Yaml
While the full structure of the yaml can be seen within the source repository, commonly, only one or two parameters are needed. Missing keys are merged with the defaults. For example:
extensions:
controllerName: dotnet-mvc-server-controller
typeNameOverride: dotnet-type-name
namespaceOverride: dotnet-type-namespace
mapType: global::System.Collections.Generic.Dictionary<string, {}>
arrayType: global::System.Collections.Generic.IEnumerable<{}>
types:
number:
formats:
float: float
double: double
default: double
overrideNames:
proj://darkpatterns-openapi/multi-file-ref-types.yaml#/BadRequest: My.Common.BadRequest
extensions.controllerName
specifies the extension (for example,x-dotnet-mvc-server-controller
) used to override the generated controller name. This may be specified on either the operation or the path level.extensions.typeNameOverride
specifies the extension (for example,x-dotnet-type-name
) used to override the generated type name. This may be specified on any JSON schema that will be emitted as its own class, enum, etc.extensions.namespaceOverride
specifies the extension (for example,x-dotnet-type-namespace
) used to override the generated type namespace. This may be specified on any JSON schema that will be emitted as its own class, enum, etc.mapType
specifies the type to use for JSON maps, which occur when whenadditionalProperties
is specified.{}
is used as a placeholder for the type.arrayType
specifies the type to use for JSON arrays.{}
is used as a placeholder for the type.types
must be an object with keys that correspond to the JSON Schematype
properties. Within, the configuration may specify either thedefault
(for if the format is either not specified or not found) or customformats
. Custom types may be used here to customize JSON serialization and deserialization, especially to standardize string formats for currency or APRs.The number example provided will generate a float if the following schema is used:
type: number format: float
overrideNames
is a dictionary of schema URIs to the namespace-qualified C# type name to use for the generated class. (Note: this feature is still experimental and may change or be removed in a later relaese.)
Schema extensions
Extensions in OpenAPI documents are additional properties, starting with x-
that can go nearly anywhere in an OpenAPI 3.0 document. The following extensions
are available:
x-dotnet-mvc-server-controller
overrides the name of the controller class generated for paths and operations. This extension may be specified either at the path or operation level.x-dotnet-type-namespace
overrides the namespace for a single schema. This is a higher-priority than settings within the csproj but lower priority than individual schema name overrides in the options file.x-dotnet-type-name
overrides the type name for a single schema. This is a higher-priority than settings within the csproj but lower priority than individual schema name overrides in the options file.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.