Skip to content

Commit

Permalink
removed JsonApiObjectNullabilityProcessor and enabled NRT support in …
Browse files Browse the repository at this point in the history
…Swashbuckle.
  • Loading branch information
maurei committed Nov 8, 2021
1 parent bf841f9 commit f1d0bcd
Show file tree
Hide file tree
Showing 6 changed files with 6 additions and 166 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ private static void AddSwaggerGenerator(IServiceScope scope, IServiceCollection

services.AddSwaggerGen(swaggerGenOptions =>
{
swaggerGenOptions.SupportNonNullableReferenceTypes();
SetOperationInfo(swaggerGenOptions, controllerResourceMapping, namingPolicy);
SetSchemaIdSelector(swaggerGenOptions, resourceGraph, resourceNameFormatter);
swaggerGenOptions.DocumentFilter<EndpointOrderingFilter>();
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,9 @@ internal sealed class JsonApiSchemaGenerator : ISchemaGenerator
typeof(SecondaryResourceResponseDocument<>)
};

private static readonly Type[] JsonApiResourceIdentifierDocumentOpenTypes =
{
typeof(ResourceIdentifierCollectionResponseDocument<>),
typeof(ResourceIdentifierResponseDocument<>)
};

private readonly ISchemaGenerator _defaultSchemaGenerator;
private readonly ResourceObjectSchemaGenerator _resourceObjectSchemaGenerator;
private readonly NullableReferenceSchemaGenerator _nullableReferenceSchemaGenerator;
private readonly JsonApiObjectNullabilityProcessor _jsonApiObjectNullabilityProcessor;
private readonly SchemaRepositoryAccessor _schemaRepositoryAccessor = new();

public JsonApiSchemaGenerator(SchemaGenerator defaultSchemaGenerator, IResourceGraph resourceGraph, IJsonApiOptions options)
Expand All @@ -48,7 +41,6 @@ public JsonApiSchemaGenerator(SchemaGenerator defaultSchemaGenerator, IResourceG

_defaultSchemaGenerator = defaultSchemaGenerator;
_nullableReferenceSchemaGenerator = new NullableReferenceSchemaGenerator(_schemaRepositoryAccessor);
_jsonApiObjectNullabilityProcessor = new JsonApiObjectNullabilityProcessor(_schemaRepositoryAccessor);
_resourceObjectSchemaGenerator = new ResourceObjectSchemaGenerator(defaultSchemaGenerator, resourceGraph, options, _schemaRepositoryAccessor);
}

Expand All @@ -73,11 +65,6 @@ public OpenApiSchema GenerateSchema(Type type, SchemaRepository schemaRepository
SetDataObjectSchemaToNullable(schema);
}

if (IsJsonApiDocument(type))
{
RemoveNotApplicableNullability(schema);
}

return schema;
}

Expand All @@ -86,16 +73,6 @@ private static bool IsJsonApiResourceDocument(Type type)
return type.IsConstructedGenericType && JsonApiResourceDocumentOpenTypes.Contains(type.GetGenericTypeDefinition());
}

private static bool IsJsonApiDocument(Type type)
{
return IsJsonApiResourceDocument(type) || IsJsonApiResourceIdentifierDocument(type);
}

private static bool IsJsonApiResourceIdentifierDocument(Type type)
{
return type.IsConstructedGenericType && JsonApiResourceIdentifierDocumentOpenTypes.Contains(type.GetGenericTypeDefinition());
}

private OpenApiSchema GenerateResourceJsonApiDocumentSchema(Type type)
{
Type resourceObjectType = type.BaseType!.GenericTypeArguments[0];
Expand Down Expand Up @@ -141,10 +118,5 @@ private static OpenApiSchema CreateArrayTypeDataSchema(OpenApiSchema referenceSc
Type = "array"
};
}

private void RemoveNotApplicableNullability(OpenApiSchema schema)
{
_jsonApiObjectNullabilityProcessor.ClearDocumentProperties(schema);
}
}
}
4 changes: 2 additions & 2 deletions test/OpenApiTests/LegacyOpenApiIntegration/Airplane.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public sealed class Airplane : Identifiable<string>

[Attr(Capabilities = AttrCapabilities.AllowView | AttrCapabilities.AllowCreate | AttrCapabilities.AllowChange)]
[MaxLength(16)]
public string SerialNumber { get; set; } = null!;
public string? SerialNumber { get; set; }

[Attr(Capabilities = AttrCapabilities.AllowView | AttrCapabilities.AllowCreate | AttrCapabilities.AllowChange)]
public int? AirtimeInHours { get; set; }
Expand All @@ -33,7 +33,7 @@ public sealed class Airplane : Identifiable<string>

[Attr(Capabilities = AttrCapabilities.AllowView | AttrCapabilities.AllowChange)]
[MaxLength(85)]
public string ManufacturedInCity { get; set; } = null!;
public string? ManufacturedInCity { get; set; }

[Attr(Capabilities = AttrCapabilities.AllowView)]
public AircraftKind Kind { get; set; }
Expand Down
4 changes: 2 additions & 2 deletions test/OpenApiTests/LegacyOpenApiIntegration/Flight.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public sealed class Flight : Identifiable<string>

[Attr(Capabilities = AttrCapabilities.AllowView | AttrCapabilities.AllowChange)]
[MaxLength(2000)]
public string StopOverDestination { get; set; } = null!;
public string? StopOverDestination { get; set; }

[Attr(PublicName = "operated-by", Capabilities = AttrCapabilities.AllowView | AttrCapabilities.AllowChange)]
public Airline Airline { get; set; }
Expand All @@ -37,7 +37,7 @@ public sealed class Flight : Identifiable<string>

[Attr]
[NotMapped]
public ICollection<string> ServicesOnBoard { get; set; } = null!;
public ICollection<string>? ServicesOnBoard { get; set; }

[HasMany]
public ICollection<Passenger> Passengers { get; set; } = null!;
Expand Down
2 changes: 1 addition & 1 deletion test/OpenApiTests/LegacyOpenApiIntegration/Passenger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public sealed class Passenger : Identifiable<string>
public string PassportNumber { get; set; } = null!;

[Attr]
public string FullName { get; set; } = null!;
public string? FullName { get; set; }

[Attr]
public CabinArea CabinArea { get; set; }
Expand Down

0 comments on commit f1d0bcd

Please sign in to comment.