Skip to content

extension parsing mechanism should not fail if the extension returns null #1695

@baywet

Description

@baywet

Imagine the following scenario:

  1. I defined an extension with a key
  2. A description contains an extension with the same key, but with a different structure
  3. My extension code returns null, in the hope that generic parsing (OpenAPIAny) will kick in instead.
  4. I get a null exception

This is caused by this block (and others similar to it)

private static IOpenApiExtension LoadExtension(string name, ParseNode node)

Which should instead be

            if (node.Context.ExtensionParsers.TryGetValue(name, out var parser) && parser(
                    OpenApiAnyConverter.GetSpecificOpenApiAny(node.CreateAny()),
                    OpenApiSpecVersion.OpenApi2_0) is {} result)
            {
                return result;
            }
            else
            {
                return OpenApiAnyConverter.GetSpecificOpenApiAny(node.CreateAny());
            }

Which ultimately triggers

element.Extensions[name] = Utils.CheckArgumentNull(any);

Once those implementations are updated, all the Microsoft defined extensions should also be updated to return null instead of throwing.

if (source is not OpenApiObject rawObject) throw new ArgumentOutOfRangeException(nameof(source));

Metadata

Metadata

Assignees

Labels

type:bugA broken experience

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions