diff --git a/src/DotSwashbuckle.AspNetCore.Newtonsoft/SchemaGenerator/NewtonsoftDataContractResolver.cs b/src/DotSwashbuckle.AspNetCore.Newtonsoft/SchemaGenerator/NewtonsoftDataContractResolver.cs index 1d849d5d9d..9c6f195c6b 100644 --- a/src/DotSwashbuckle.AspNetCore.Newtonsoft/SchemaGenerator/NewtonsoftDataContractResolver.cs +++ b/src/DotSwashbuckle.AspNetCore.Newtonsoft/SchemaGenerator/NewtonsoftDataContractResolver.cs @@ -72,12 +72,17 @@ public DataContract GetDataContractForType(Type type) jsonConverter: JsonConverterFunc); } - if (JsonSerializerDataContractResolver.IsSupportedDictionary(type, out Type _, out Type valueType1)) + if (jsonContract is JsonDictionaryContract jsonDictionaryContract) { + var keyType = jsonDictionaryContract.DictionaryKeyType ?? typeof(object); + var valueType = jsonDictionaryContract.DictionaryValueType ?? typeof(object); + + var keys = readDictionaryKeyTypes(keyType); + return DataContract.ForDictionary( - underlyingType: type, - valueType: valueType1, - keys: null, // STJ doesn't currently support dictionaries with enum key types + underlyingType: jsonDictionaryContract.UnderlyingType, + valueType: valueType, + keys: keys, jsonConverter: JsonConverterFunc); } @@ -89,28 +94,13 @@ public DataContract GetDataContractForType(Type type) jsonConverter: JsonConverterFunc); } - if (jsonContract is JsonDictionaryContract jsonDictionaryContract) + if (JsonSerializerDataContractResolver.IsSupportedDictionary(type, out Type keyType1, out Type valueType1)) { - var keyType = jsonDictionaryContract.DictionaryKeyType ?? typeof(object); - var valueType = jsonDictionaryContract.DictionaryValueType ?? typeof(object); - - IEnumerable keys = null; - - if (keyType.IsEnum) - { - // This is a special case where we know the possible key values - var enumValuesAsJson = keyType.GetEnumValues() - .Cast() - .Select(value => JsonConverterFunc(value)); - - keys = enumValuesAsJson.Any(json => json.StartsWith("\"")) - ? enumValuesAsJson.Select(json => json.Replace("\"", string.Empty)) - : keyType.GetEnumNames(); - } + var keys = readDictionaryKeyTypes(keyType1); return DataContract.ForDictionary( - underlyingType: jsonDictionaryContract.UnderlyingType, - valueType: valueType, + underlyingType: type, + valueType: valueType1, keys: keys, jsonConverter: JsonConverterFunc); } @@ -145,6 +135,25 @@ public DataContract GetDataContractForType(Type type) jsonConverter: JsonConverterFunc); } + private IEnumerable readDictionaryKeyTypes(Type keyType) + { + IEnumerable keys = null; + + if (keyType.IsEnum) + { + // This is a special case where we know the possible key values + var enumValuesAsJson = keyType.GetEnumValues() + .Cast() + .Select(value => JsonConverterFunc(value)); + + keys = enumValuesAsJson.Any(json => json.StartsWith("\"")) + ? enumValuesAsJson.Select(json => json.Replace("\"", string.Empty)) + : keyType.GetEnumNames(); + } + + return keys; + } + private string JsonConverterFunc(object value) { return JsonConvert.SerializeObject(value, _serializerSettings);