Skip to content
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

Schema Do not take in account Nullable Record inside record #2698

Closed
aygalinc opened this issue Aug 25, 2023 · 4 comments
Closed

Schema Do not take in account Nullable Record inside record #2698

aygalinc opened this issue Aug 25, 2023 · 4 comments

Comments

@aygalinc
Copy link

Hello ,

I am on .Net 7 with nullable enable and on the following reporducer i do not get my property PnlGenerated mark as nullable inside the generated OpenApi specification.

The swassbuckle configuration :

        options.SchemaFilter<RequireAllPropertiesPropertiesSchemaFilter>();
   options.SupportNonNullableReferenceTypes();
        options.MapType<DateOnly>(() => new OpenApiSchema { Type = "string", Format = "date" });
    }
}

public sealed class RequireAllPropertiesPropertiesSchemaFilter : ISchemaFilter
{
    /// <summary>
    /// Add to model.Required all properties where Nullable is false.
    /// </summary>
    public void Apply(OpenApiSchema schema, SchemaFilterContext context)
    {
        var additionalRequiredProps = schema.Properties
            .Where(x => !schema.Required.Contains(x.Key))
            .Select(x => x.Key);

        foreach (var propKey in additionalRequiredProps)
        {
            schema.Required.Add(propKey);
        }
    }
}

the Controller and its DTO :

  [HttpPost(
        "xxx")]
    [ProducesResponseType(typeof(ComputedPeriodDto), StatusCodes.Status200OK)]
    [SwaggerOperation(OperationId = "ComputePeriodForConvertToForecast")]
    public async Task<ActionResult<ComputedPeriod>> ComputePeriodForConvertToForecast(
        [FromRoute, Required] int companyId, [FromRoute, Required] Guid pnlId, [FromBody] ConvertToForecastDto dto)
    {
        var conversionPeriod = xxx;
        return this.Ok(conversionPeriod.MapToDto());
    }
}

public sealed record ConvertToForecastDto(int? ScenarioId, bool GenerateForecastInPast = false);

public sealed record ComputedPeriodDto(
    PeriodDto? PnlGenerated, [property: JsonRequired] PeriodDto PnlFile);

public sealed record PeriodDto([property: JsonRequired] DateOnly StartDate, [property: JsonRequired] DateOnly EndDate);

Generate for ComputedPEriodDto :

 "ComputedPeriodDto": {
        "required": [
          "pnlFile",
          "pnlGenerated"
        ],
        "type": "object",
        "properties": {
          "pnlFile": {
            "$ref": "#/components/schemas/PeriodDto"
          },
          "pnlGenerated": {
            "$ref": "#/components/schemas/PeriodDto"
          }
        },
        "additionalProperties": false
      },

I would have expect to see :

 "ComputedPeriodDto": {
        "required": [
          "pnlFile",
          "pnlGenerated"
        ],
        "type": "object",
        "properties": {
          "pnlFile": {
            "$ref": "#/components/schemas/PeriodDto"
          },
          "pnlGenerated": {
            "$ref": "#/components/schemas/PeriodDto",
nullable: true
          }
        },
        "additionalProperties": false
      },
@p-m-j
Copy link

p-m-j commented Sep 7, 2023

@aygalinc I had the same issue, but this can be solved by calling UseAllOfToExtendReferenceSchemas on SwaggerGenOptions

services.AddSwaggerGen(opt =>
{
    opt.SupportNonNullableReferenceTypes();
    opt.UseAllOfToExtendReferenceSchemas();
});

@aygalinc
Copy link
Author

aygalinc commented Oct 3, 2023

Hey seems to do the trick but weird that it not works out of the box.

@Havunen
Copy link

Havunen commented Feb 18, 2024

This might be fixed in DotSwashbuckle, can you test using it please.
https://github.com/Havunen/DotSwashbuckle
https://www.nuget.org/packages/DotSwashbuckle.AspNetCore

@martincostello
Copy link
Collaborator

To make issue tracking a bit less overwhelming for the new maintainers (see #2778), I've created a new tracking issue to roll-up various nullability issues here: #2793.

We'll refer back to this issue from there and include it as part of resolving that issue, but I'm going to close this one to help prune the backlog.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants