RicoSuter/NSwag · error · ArgumentException

The schema type '" + schemaType + "' is not supported.

Error message

The schema type '" + schemaType + "' is not supported.

What it means

OpenApiDocument.Serialization.GetJsonSerializerContractResolver only supports Swagger2 and OpenApi3 schema types and throws ArgumentException for any other value. JsonSchema is explicitly not a valid serialization target for OpenAPI documents (see also error 46).

Solutions

  1. Set SchemaType to SchemaType.Swagger2 or SchemaType.OpenApi3 before serializing
  2. Verify the SchemaType enum value passed into serialization helpers
  3. Use FromJsonAsync with explicit expectedSchemaType instead of relying on defaults

Example fix

// before
document.SchemaType = SchemaType.JsonSchema;
await document.ToJsonAsync();
// after
document.SchemaType = SchemaType.OpenApi3;
await document.ToJsonAsync();
Defensive patterns

Strategy: validation

Validate before calling

if (schemaType is not (SchemaType.Swagger2 or SchemaType.OpenApi3))
    throw new ArgumentException($"Unsupported schema type: {schemaType}");

Type guard

bool isSerializable = schemaType == SchemaType.Swagger2 || schemaType == SchemaType.OpenApi3;

Try / catch

try { await doc.ToJsonAsync(); } catch (ArgumentException ex) { /* fix SchemaType and retry */ }

Prevention

When it happens

Trigger: Calling serialization APIs (e.g. ToJsonAsync / contract resolution) with a SchemaType value other than SchemaType.Swagger2 or SchemaType.OpenApi3, such as SchemaType.JsonSchema or a garbage cast value.

Common situations: Setting document.SchemaType manually to an unsupported value, or migrating code from JsonSchema utilities to OpenAPI documents without changing the schema type enum.

Related errors


AI-assisted analysis of RicoSuter/NSwag@63daf8fcc3 (2026-09-14). Data as JSON: /api/errors/e259caffb506f38b. Report an issue: GitHub.

Appendix: source

Thrown at src/NSwag.Core/OpenApiDocument.Serialization.cs:40

        private static readonly Lazy<PropertyRenameAndIgnoreSerializerContractResolver> OpenApi3ContractResolver =
            new Lazy<PropertyRenameAndIgnoreSerializerContractResolver>(() => CreateJsonSerializerContractResolver(SchemaType.OpenApi3));

        /// <summary>Creates the serializer contract resolver based on the <see cref="NJsonSchema.SchemaType"/>.</summary>
        /// <param name="schemaType">The schema type.</param>
        /// <returns>The settings.</returns>
        public static PropertyRenameAndIgnoreSerializerContractResolver GetJsonSerializerContractResolver(SchemaType schemaType)
        {
            if (schemaType == SchemaType.Swagger2)
            {
                return Swagger2ContractResolver.Value;
            }
            else if (schemaType == SchemaType.OpenApi3)
            {
                return OpenApi3ContractResolver.Value;
            }

            throw new ArgumentException("The schema type '" + schemaType + "' is not supported.");
        }

        private static PropertyRenameAndIgnoreSerializerContractResolver CreateJsonSerializerContractResolver(SchemaType schemaType)
        {
            var resolver = JsonSchema.CreateJsonSerializerContractResolver(schemaType);

            if (schemaType == SchemaType.Swagger2)
            {
                resolver.IgnoreProperty(typeof(OpenApiDocument), "openapi");
                resolver.IgnoreProperty(typeof(OpenApiDocument), "servers");
                resolver.IgnoreProperty(typeof(OpenApiParameter), "title");

                // TODO: Use rename for not mapped properties!
                resolver.IgnoreProperty(typeof(OpenApiPathItem), "summary");
                resolver.IgnoreProperty(typeof(OpenApiPathItem), "description");
                resolver.IgnoreProperty(typeof(OpenApiPathItem), "servers");

                resolver.IgnoreProperty(typeof(OpenApiOperation), "callbacks");

View on GitHub (pinned to 63daf8fcc3)