Skip to content

Commit

Permalink
Favor ModelMetadata.ModelType over parameter/property type for schema…
Browse files Browse the repository at this point in the history
… generation
  • Loading branch information
rmorris committed Apr 8, 2021
1 parent 7196402 commit 95cb4d3
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<LicenseUrl>https://licenses.nuget.org/MIT</LicenseUrl>
<VersionPrefix>6.1.1</VersionPrefix>
<VersionPrefix>6.1.2</VersionPrefix>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,30 +25,27 @@ public SchemaGenerator(SchemaGeneratorOptions generatorOptions, ISerializerDataC
}

public OpenApiSchema GenerateSchema(
Type type,
Type modelType,
SchemaRepository schemaRepository,
MemberInfo memberInfo = null,
ParameterInfo parameterInfo = null)
{
if (memberInfo != null)
return GenerateSchemaForMember(memberInfo, schemaRepository);
return GenerateSchemaForMember(modelType, schemaRepository, memberInfo);

if (parameterInfo != null)
return GenerateSchemaForParameter(parameterInfo, schemaRepository);
return GenerateSchemaForParameter(modelType, schemaRepository, parameterInfo);

return GenerateSchemaForType(type, schemaRepository);
return GenerateSchemaForType(modelType, schemaRepository);
}

private OpenApiSchema GenerateSchemaForMember(
MemberInfo memberInfo,
Type modelType,
SchemaRepository schemaRepository,
MemberInfo memberInfo,
DataProperty dataProperty = null)
{
var memberType = memberInfo.MemberType == MemberTypes.Field
? ((FieldInfo)memberInfo).FieldType
: ((PropertyInfo)memberInfo).PropertyType;

var dataContract = GetDataContractFor(memberType);
var dataContract = GetDataContractFor(modelType);

var schema = _generatorOptions.UseOneOfForPolymorphism && IsBaseTypeWithKnownTypesDefined(dataContract, out var knownTypesDataContracts)
? GeneratePolymorphicSchema(dataContract, schemaRepository, knownTypesDataContracts)
Expand Down Expand Up @@ -90,15 +87,18 @@ private OpenApiSchema GenerateSchemaForMember(

schema.ApplyValidationAttributes(customAttributes);

ApplyFilters(schema, memberType, schemaRepository, memberInfo: memberInfo);
ApplyFilters(schema, modelType, schemaRepository, memberInfo: memberInfo);
}

return schema;
}

private OpenApiSchema GenerateSchemaForParameter(ParameterInfo parameterInfo, SchemaRepository schemaRepository)
private OpenApiSchema GenerateSchemaForParameter(
Type modelType,
SchemaRepository schemaRepository,
ParameterInfo parameterInfo)
{
var dataContract = GetDataContractFor(parameterInfo.ParameterType);
var dataContract = GetDataContractFor(modelType);

var schema = _generatorOptions.UseOneOfForPolymorphism && IsBaseTypeWithKnownTypesDefined(dataContract, out var knownTypesDataContracts)
? GeneratePolymorphicSchema(dataContract, schemaRepository, knownTypesDataContracts)
Expand Down Expand Up @@ -126,31 +126,31 @@ private OpenApiSchema GenerateSchemaForParameter(ParameterInfo parameterInfo, Sc

schema.ApplyValidationAttributes(customAttributes);

ApplyFilters(schema, parameterInfo.ParameterType, schemaRepository, parameterInfo: parameterInfo);
ApplyFilters(schema, modelType, schemaRepository, parameterInfo: parameterInfo);
}

return schema;
}

private OpenApiSchema GenerateSchemaForType(Type type, SchemaRepository schemaRepository)
private OpenApiSchema GenerateSchemaForType(Type modelType, SchemaRepository schemaRepository)
{
var dataContract = GetDataContractFor(type);
var dataContract = GetDataContractFor(modelType);

var schema = _generatorOptions.UseOneOfForPolymorphism && IsBaseTypeWithKnownTypesDefined(dataContract, out var knownTypesDataContracts)
? GeneratePolymorphicSchema(dataContract, schemaRepository, knownTypesDataContracts)
: GenerateConcreteSchema(dataContract, schemaRepository);

if (schema.Reference == null)
{
ApplyFilters(schema, type, schemaRepository);
ApplyFilters(schema, modelType, schemaRepository);
}

return schema;
}

private DataContract GetDataContractFor(Type type)
private DataContract GetDataContractFor(Type modelType)
{
var effectiveType = Nullable.GetUnderlyingType(type) ?? type;
var effectiveType = Nullable.GetUnderlyingType(modelType) ?? modelType;
return _serializerDataContractResolver.GetDataContractForType(effectiveType);
}

Expand Down Expand Up @@ -246,10 +246,10 @@ private OpenApiSchema GenerateConcreteSchema(DataContract dataContract, SchemaRe
: schemaFactory();
}

private bool TryGetCustomTypeMapping(Type type, out Func<OpenApiSchema> schemaFactory)
private bool TryGetCustomTypeMapping(Type modelType, out Func<OpenApiSchema> schemaFactory)
{
return _generatorOptions.CustomTypeMappings.TryGetValue(type, out schemaFactory)
|| (type.IsConstructedGenericType && _generatorOptions.CustomTypeMappings.TryGetValue(type.GetGenericTypeDefinition(), out schemaFactory));
return _generatorOptions.CustomTypeMappings.TryGetValue(modelType, out schemaFactory)
|| (modelType.IsConstructedGenericType && _generatorOptions.CustomTypeMappings.TryGetValue(modelType.GetGenericTypeDefinition(), out schemaFactory));
}

private OpenApiSchema CreatePrimitiveSchema(DataContract dataContract)
Expand Down Expand Up @@ -371,7 +371,7 @@ private OpenApiSchema CreateObjectSchema(DataContract dataContract, SchemaReposi
continue;

schema.Properties[dataProperty.Name] = (dataProperty.MemberInfo != null)
? GenerateSchemaForMember(dataProperty.MemberInfo, schemaRepository, dataProperty)
? GenerateSchemaForMember(dataProperty.MemberType, schemaRepository, dataProperty.MemberInfo, dataProperty)
: GenerateSchemaForType(dataProperty.MemberType, schemaRepository);

if ((dataProperty.IsRequired || customAttributes.OfType<RequiredAttribute>().Any())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Swashbuckle.AspNetCore.SwaggerGen
public interface ISchemaGenerator
{
OpenApiSchema GenerateSchema(
Type type,
Type modelType,
SchemaRepository schemaRepository,
MemberInfo memberInfo = null,
ParameterInfo parameterInfo = null);
Expand Down

0 comments on commit 95cb4d3

Please sign in to comment.