Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,11 @@ public DataContract GetDataContractForType(Type type)
jsonConverter: JsonConverterFunc);
}

var jsonContract = _contractResolver.ResolveContract(type);
var jsonContract = _contractResolver.ResolveContract(effectiveType);

var effectiveUnderlyingType = Nullable.GetUnderlyingType(jsonContract.UnderlyingType) ?? jsonContract.UnderlyingType;

if (jsonContract is JsonPrimitiveContract && !effectiveUnderlyingType.IsEnum)
if (jsonContract is JsonPrimitiveContract && !jsonContract.UnderlyingType.IsEnum)
{
if (!PrimitiveTypesAndFormats.TryGetValue(effectiveUnderlyingType, out var primitiveTypeAndFormat))
if (!PrimitiveTypesAndFormats.TryGetValue(jsonContract.UnderlyingType, out var primitiveTypeAndFormat))
{
primitiveTypeAndFormat = Tuple.Create(DataType.String, (string)null);
}
Expand All @@ -40,9 +38,9 @@ public DataContract GetDataContractForType(Type type)
jsonConverter: JsonConverterFunc);
}

if (jsonContract is JsonPrimitiveContract && effectiveUnderlyingType.IsEnum)
if (jsonContract is JsonPrimitiveContract && jsonContract.UnderlyingType.IsEnum)
{
var enumValues = effectiveUnderlyingType.GetEnumValues();
var enumValues = jsonContract.UnderlyingType.GetEnumValues();

// Test to determine if the serializer will treat as string
var serializeAsString = (enumValues.Length > 0) &&
Expand All @@ -54,7 +52,7 @@ public DataContract GetDataContractForType(Type type)

var primitiveTypeAndFormat = serializeAsString
? PrimitiveTypesAndFormats[typeof(string)]
: PrimitiveTypesAndFormats[effectiveUnderlyingType.GetEnumUnderlyingType()];
: PrimitiveTypesAndFormats[jsonContract.UnderlyingType.GetEnumUnderlyingType()];

return DataContract.ForPrimitive(
underlyingType: jsonContract.UnderlyingType,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public DataContract GetDataContractForType(Type type)
primitiveTypeAndFormat = PrimitiveTypesAndFormats[exampleType];

return DataContract.ForPrimitive(
underlyingType: type,
underlyingType: effectiveType,
dataType: primitiveTypeAndFormat.Item1,
dataFormat: primitiveTypeAndFormat.Item2,
jsonConverter: (value) => JsonConverterFunc(value, type));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,6 @@ private OpenApiSchema GenerateSchemaForMember(
MemberInfo memberInfo,
DataProperty dataProperty = null)
{
if (dataProperty != null)
{
var customAttributes = memberInfo.GetInlineAndMetadataAttributes();

var requiredAttribute = customAttributes.OfType<RequiredAttribute>().FirstOrDefault();

if (!IsNullable(requiredAttribute, dataProperty, memberInfo))
{
modelType = Nullable.GetUnderlyingType(modelType) ?? modelType;
}
}

var dataContract = GetDataContractFor(modelType);

var schema = _generatorOptions.UseOneOfForPolymorphism && IsBaseTypeWithKnownTypesDefined(dataContract, out var knownTypesDataContracts)
Expand Down Expand Up @@ -284,7 +272,7 @@ private OpenApiSchema GenerateConcreteSchema(DataContract dataContract, SchemaRe
case DataType.String:
{
schemaFactory = () => CreatePrimitiveSchema(dataContract);
returnAsReference = (Nullable.GetUnderlyingType(dataContract.UnderlyingType) ?? dataContract.UnderlyingType).IsEnum && !_generatorOptions.UseInlineDefinitionsForEnums;
returnAsReference = dataContract.UnderlyingType.IsEnum && !_generatorOptions.UseInlineDefinitionsForEnums;
break;
}

Expand Down Expand Up @@ -350,18 +338,12 @@ private static OpenApiSchema CreatePrimitiveSchema(DataContract dataContract)
}
#pragma warning restore CS0618 // Type or member is obsolete

var underlyingType = Nullable.GetUnderlyingType(dataContract.UnderlyingType) ?? dataContract.UnderlyingType;
var underlyingType = dataContract.UnderlyingType;

if (underlyingType.IsEnum)
{
var enumValues = underlyingType.GetEnumValues().Cast<object>();

if (dataContract.UnderlyingType != underlyingType)
{
schema.Nullable = true;
enumValues = enumValues.Append(null);
}

schema.Enum = [.. enumValues
.Select(value => dataContract.JsonConverter(value))
.Distinct()
Expand Down Expand Up @@ -469,7 +451,7 @@ private OpenApiSchema CreateObjectSchema(DataContract dataContract, SchemaReposi
continue;
}

var memberType = dataProperty.IsNullable ? dataProperty.MemberType : (Nullable.GetUnderlyingType(dataProperty.MemberType) ?? dataProperty.MemberType);
var memberType = dataProperty.MemberType;

schema.Properties[dataProperty.Name] = (dataProperty.MemberInfo != null)
? GenerateSchemaForMember(memberType, schemaRepository, dataProperty.MemberInfo, dataProperty)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,7 @@ apiParameter.Type is not null &&

var schema = (type != null)
? GenerateSchema(
isRequired ? (Nullable.GetUnderlyingType(type) ?? type) : type,
type,
schemaRepository,
apiParameter.PropertyInfo(),
apiParameter.ParameterInfo(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1491,7 +1491,7 @@
"$ref": "#/components/schemas/ProductStatus"
},
"status2": {
"$ref": "#/components/schemas/ProductStatusNullable"
"$ref": "#/components/schemas/ProductStatus"
},
"price": {
"maximum": 122.9,
Expand Down Expand Up @@ -1520,17 +1520,6 @@
"type": "integer",
"format": "int32"
},
"ProductStatusNullable": {
"enum": [
0,
1,
2,
null
],
"type": "integer",
"format": "int32",
"nullable": true
},
"Promotion": {
"type": "object",
"properties": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1491,7 +1491,7 @@
"$ref": "#/components/schemas/ProductStatus"
},
"status2": {
"$ref": "#/components/schemas/ProductStatusNullable"
"$ref": "#/components/schemas/ProductStatus"
},
"price": {
"maximum": 122.9,
Expand Down Expand Up @@ -1520,17 +1520,6 @@
"type": "integer",
"format": "int32"
},
"ProductStatusNullable": {
"enum": [
0,
1,
2,
null
],
"type": "integer",
"format": "int32",
"nullable": true
},
"Promotion": {
"type": "object",
"properties": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"schema": {
"allOf": [
{
"$ref": "#/components/schemas/LogLevelNullable"
"$ref": "#/components/schemas/LogLevel"
}
],
"default": 4
Expand Down Expand Up @@ -73,21 +73,6 @@
],
"type": "integer",
"format": "int32"
},
"LogLevelNullable": {
"enum": [
0,
1,
2,
3,
4,
5,
6,
null
],
"type": "integer",
"format": "int32",
"nullable": true
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"schema": {
"allOf": [
{
"$ref": "#/components/schemas/LogLevelNullable"
"$ref": "#/components/schemas/LogLevel"
}
],
"default": 4
Expand Down Expand Up @@ -73,21 +73,6 @@
],
"type": "integer",
"format": "int32"
},
"LogLevelNullable": {
"enum": [
0,
1,
2,
3,
4,
5,
6,
null
],
"type": "integer",
"format": "int32",
"nullable": true
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@
"name": "paramNine",
"in": "query",
"schema": {
"$ref": "#/components/schemas/DateTimeKindNullable"
"$ref": "#/components/schemas/DateTimeKind"
}
},
{
Expand Down Expand Up @@ -946,7 +946,7 @@
"format": "time"
},
"paramNine": {
"$ref": "#/components/schemas/DateTimeKindNullable"
"$ref": "#/components/schemas/DateTimeKind"
},
"paramTen": {
"$ref": "#/components/schemas/DateTimeKind"
Expand Down Expand Up @@ -995,17 +995,6 @@
"type": "integer",
"format": "int32"
},
"DateTimeKindNullable": {
"enum": [
0,
1,
2,
null
],
"type": "integer",
"format": "int32",
"nullable": true
},
"Fruit": {
"type": "object",
"properties": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@
"name": "paramNine",
"in": "query",
"schema": {
"$ref": "#/components/schemas/DateTimeKindNullable"
"$ref": "#/components/schemas/DateTimeKind"
}
},
{
Expand Down Expand Up @@ -946,7 +946,7 @@
"format": "time"
},
"paramNine": {
"$ref": "#/components/schemas/DateTimeKindNullable"
"$ref": "#/components/schemas/DateTimeKind"
},
"paramTen": {
"$ref": "#/components/schemas/DateTimeKind"
Expand Down Expand Up @@ -995,17 +995,6 @@
"type": "integer",
"format": "int32"
},
"DateTimeKindNullable": {
"enum": [
0,
1,
2,
null
],
"type": "integer",
"format": "int32",
"nullable": true
},
"Fruit": {
"type": "object",
"properties": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@
"name": "paramNine",
"in": "query",
"schema": {
"$ref": "#/components/schemas/DateTimeKindNullable"
"$ref": "#/components/schemas/DateTimeKind"
}
},
{
Expand Down Expand Up @@ -946,7 +946,7 @@
"format": "time"
},
"paramNine": {
"$ref": "#/components/schemas/DateTimeKindNullable"
"$ref": "#/components/schemas/DateTimeKind"
},
"paramTen": {
"$ref": "#/components/schemas/DateTimeKind"
Expand Down Expand Up @@ -995,17 +995,6 @@
"type": "integer",
"format": "int32"
},
"DateTimeKindNullable": {
"enum": [
0,
1,
2,
null
],
"type": "integer",
"format": "int32",
"nullable": true
},
"Fruit": {
"type": "object",
"properties": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@
"name": "paramNine",
"in": "query",
"schema": {
"$ref": "#/components/schemas/DateTimeKindNullable"
"$ref": "#/components/schemas/DateTimeKind"
}
},
{
Expand Down Expand Up @@ -946,7 +946,7 @@
"format": "time"
},
"paramNine": {
"$ref": "#/components/schemas/DateTimeKindNullable"
"$ref": "#/components/schemas/DateTimeKind"
},
"paramTen": {
"$ref": "#/components/schemas/DateTimeKind"
Expand Down Expand Up @@ -995,17 +995,6 @@
"type": "integer",
"format": "int32"
},
"DateTimeKindNullable": {
"enum": [
0,
1,
2,
null
],
"type": "integer",
"format": "int32",
"nullable": true
},
"Fruit": {
"type": "object",
"properties": {
Expand Down
Loading