Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Open Api fails to generate correct schema type for some properties #61407

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
1 task done
mattiasnordqvist opened this issue Apr 9, 2025 · 4 comments
Open
1 task done
Labels
area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc feature-openapi

Comments

@mattiasnordqvist
Copy link

mattiasnordqvist commented Apr 9, 2025

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

Image

Image

generates this schema

Image

Expected Behavior

I would expect this schema

"D": {
  "required": [
    "b",
    "c"
  ],
  "type": "object",
  "properties": {
    "b": {
      "type": "array",
      "items": {
        "$ref": "#/components/schemas/B"
      }
    },
    "c": {
      "type": "array",
      "items": {
        "$ref": "#/components/schemas/B"
      }
    }
  }
}

Steps To Reproduce

No response

Exceptions (if any)

No response

.NET Version

9.0.4

Anything else?

No response

@NWessel
Copy link

NWessel commented Apr 23, 2025

When would a fix for this be a realistic expectation? I know you guys are busy

@simonlevinsson-spinit
Copy link

Just a FYI about the required part of the bug report. It is not connected to the issue as it's a global configuration set for the schema generator in the project.

@wuzzeb
Copy link

wuzzeb commented May 5, 2025

I also hit this problem and was able to make a small reproducible and runnable example. It is a single change 5a705dd on top of the default dotnet new webapi template. See https://github.com/wuzzeb/openapi-bug for the runnable example.

9.0.4 generates

"WeatherForecast": {
  "required": [
    "cities"
  ],
  "type": "object",
  "properties": {
    "date": {
      "type": "string",
      "format": "date"
    },
    "cities": {
      "type": "array",
      "items": {
        "$ref": "#/components/schemas/City"
      }
    },
    "citiesMore": {
      "type": "array",
      "items": { },
      "nullable": true
    }
  }
}

Interestingly, if you switch it to 9.0.3, it instead generates

    "citiesMore": {
      "type": "array",
      "items": {
        "$ref": "#/components/schemas/#/properties/cities/items"
      },
      "nullable": true
    }

So it seems like nullability and duplication isn't an issue here since in both lists City is non-nullable so no need to duplicate. But maybe some of the nullability/duplication fixes between 9.0.3 and 9.0.4 broke this situation?

wuzzeb added a commit to SeedTactics/fms-insight that referenced this issue May 5, 2025
Found the already reported issue in asp.net core

dotnet/aspnetcore#61407

for us, it manifests in the NewJobs record with the SimStations and SimStationsForCompleted properties not being generated correctly.
@p3rh5g
Copy link

p3rh5g commented May 6, 2025

I have made this schema transformer as a workaround until this is fixed:

public sealed class FixBrokenReferencesInArraysSchemaTransformer : IOpenApiSchemaTransformer
{
    public Task TransformAsync(
        OpenApiSchema schema,
        OpenApiSchemaTransformerContext context,
        CancellationToken cancellationToken)
    {
        ArgumentNullException.ThrowIfNull(schema);

        if (schema.Properties is null)
        {
            return Task.CompletedTask;
        }

        var propertiesToFix = schema.Properties
            .Select(x => x.Value)
            .Where(x => x.Type == "array")
            .Where(x => x.Items is not null)
            .Where(x => !string.IsNullOrEmpty(x.Items.Reference?.Id))
            .Where(x => x.Items.Annotations is not null);

        foreach (var property in propertiesToFix)
        {
            if (!property.Items.Annotations.TryGetValue("x-schema-id", out var schemaId))
            {
                continue;
            }

            property.Items.Reference = new OpenApiReference
            {
                Type = ReferenceType.Schema,
                Id = schemaId.ToString(),
            };
        }

        return Task.CompletedTask;
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc feature-openapi
Projects
None yet
Development

No branches or pull requests

6 participants