RicoSuter/NSwag · error · NotSupportedException

The schema type JsonSchema is not supported.

Error message

The schema type JsonSchema is not supported.

What it means

OpenApiDocument.FromJsonAsync detects the schema type of the input JSON and throws NotSupportedException if the result is SchemaType.JsonSchema, because OpenApiDocument can only be deserialized as Swagger 2 or OpenAPI 3. A plain JSON Schema document is not an OpenAPI document.

Solutions

  1. Load the document as JsonSchema instead (use the JsonSchema class / FromJsonAsync on JsonSchema)
  2. Ensure the input has a valid 'swagger': '2.0' or 'openapi': '3.x' field
  3. Check detection logic: verify the file is actually an OpenAPI document

Example fix

// before
var doc = await OpenApiDocument.FromJsonAsync(json);
// after
if (json.Contains("\"swagger\"") || json.Contains("\"openapi\""))
    var doc = await OpenApiDocument.FromJsonAsync(json);
else
    var schema = await JsonSchema.FromJsonAsync(json);
Defensive patterns

Strategy: validation

Validate before calling

var isOpenApi = json.Contains("\"swagger\"") || json.Contains("\"openapi\"");
if (!isOpenApi) throw new InvalidOperationException("Input is not an OpenAPI document");

Try / catch

try { doc = await OpenApiDocument.FromJsonAsync(json); }
catch (NotSupportedException) { schema = await JsonSchema.FromJsonAsync(json); }

Prevention

When it happens

Trigger: Passing a pure JSON Schema document (or one whose $schema/version fields resolve to JsonSchema) to OpenApiDocument.FromJsonAsync.

Common situations: Pointing the loader at a JSON Schema file instead of a Swagger/OpenAPI spec, or a spec missing the 'swagger'/'openapi' version field so detection falls back to JsonSchema.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


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

Appendix: source

Thrown at src/NSwag.Core/OpenApiDocument.cs:200

            var match = Regex.Match(data, pattern, RegexOptions.IgnoreCase);
            if (match.Success)
            {
                var schemaType = match.Groups["schemaType"].Value.ToLowerInvariant();
                var schemaVersion = match.Groups["schemaVersion"].Value.ToLowerInvariant();

                if (schemaType == "swagger" && schemaVersion.StartsWith('2'))
                {
                    expectedSchemaType = SchemaType.Swagger2;
                }
                else if (schemaType == "openapi" && schemaVersion.StartsWith('3'))
                {
                    expectedSchemaType = SchemaType.OpenApi3;
                }
            }

            if (expectedSchemaType == SchemaType.JsonSchema)
            {
                throw new NotSupportedException("The schema type JsonSchema is not supported.");
            }

            var contractResolver = GetJsonSerializerContractResolver(expectedSchemaType);
            return await JsonSchemaSerialization.FromJsonAsync<OpenApiDocument>(data, expectedSchemaType, documentPath, document =>
            {
                document.SchemaType = expectedSchemaType;
                if (referenceResolverFactory != null)
                {
                    return referenceResolverFactory(document);
                }
                else
                {
                    var schemaResolver = new OpenApiSchemaResolver(document, new SystemTextJsonSchemaGeneratorSettings());
                    return new JsonReferenceResolver(schemaResolver);
                }
            }, contractResolver, cancellationToken).ConfigureAwait(false);
        }

View on GitHub (pinned to 63daf8fcc3)