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

Cater for value names defined in EnumMember attributes #563

Merged
merged 1 commit into from
Jan 25, 2016
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
15 changes: 12 additions & 3 deletions Swashbuckle.Core/Swagger/SchemaRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Linq;
using System.Net.Http;
using System.Reflection;
using System.Runtime.Serialization;
using System.Text;
using System.Web.Http;
using Newtonsoft.Json;
Expand Down Expand Up @@ -174,11 +175,11 @@ private Schema CreateEnumSchema(JsonPrimitiveContract primitiveContract, Type ty
{
type = "string",
@enum = camelCase
? type.GetEnumNames().Select(name => name.ToCamelCase()).ToArray()
: type.GetEnumNames()
? GetEnumNames(type).Select(name => name.ToCamelCase()).ToArray()
: GetEnumNames(type)
};
}

return new Schema
{
type = "integer",
Expand All @@ -187,6 +188,14 @@ private Schema CreateEnumSchema(JsonPrimitiveContract primitiveContract, Type ty
};
}

private string[] GetEnumNames(Type type)
{
return (from f in type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static)
let attribute = (EnumMemberAttribute)f.GetCustomAttributes(typeof(EnumMemberAttribute), false).FirstOrDefault()
select attribute == null || string.IsNullOrWhiteSpace(attribute.Value) ? f.Name : attribute.Value)
.ToArray();
}

private Schema CreateDictionarySchema(JsonDictionaryContract dictionaryContract)
{
var valueType = dictionaryContract.DictionaryValueType ?? typeof(object);
Expand Down
1 change: 1 addition & 0 deletions Swashbuckle.Core/Swashbuckle.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
<Reference Include="System.Net.Http.WebRequest, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Net.Http.2.0.20710.0\lib\net40\System.Net.Http.WebRequest.dll</HintPath>
</Reference>
<Reference Include="System.Runtime.Serialization" />
<Reference Include="System.Web.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.AspNet.WebApi.Core.4.0.20710.0\lib\net40\System.Web.Http.dll</HintPath>
</Reference>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System;
using System.Runtime.Serialization;
using System.Web.Http;

namespace Swashbuckle.Dummy.Controllers
{
public class DataContractAnnotatedTypesController : ApiController
{
public int Create(DataContractRequest request)
{
throw new NotImplementedException();
}
}

public class DataContractRequest
{
public DataContractEnumWithOutValues OverriddenValues { get; set; }
public DataContractEnumWithValues RawValues { get; set; }
}

[DataContract]
public enum DataContractEnumWithValues
{
[EnumMember(Value = "aie")]
A = 2,
[EnumMember(Value = "bee")]
B = 4
}

[DataContract]
public enum DataContractEnumWithOutValues
{
[EnumMember]
A = 2,
[EnumMember]
B = 4
}
}
2 changes: 2 additions & 0 deletions Swashbuckle.Dummy.Core/Swashbuckle.Dummy.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\Microsoft.AspNet.WebApi.Client.5.2.2\lib\net45\System.Net.Http.Formatting.dll</HintPath>
</Reference>
<Reference Include="System.Runtime.Serialization" />
<Reference Include="System.Web.Cors">
<HintPath>..\packages\Microsoft.AspNet.Cors.5.0.0\lib\net45\System.Web.Cors.dll</HintPath>
</Reference>
Expand All @@ -65,6 +66,7 @@
<Compile Include="App_Start\CachingSwaggerProvider.cs" />
<Compile Include="App_Start\CustomCorsPolicyProvider.cs" />
<Compile Include="Controllers\ConflictingTypesController.cs" />
<Compile Include="Controllers\DataContractAnnotatedTypesController.cs" />
<Compile Include="Controllers\FileDownloadController.cs" />
<Compile Include="Controllers\FileUploadController.cs" />
<Compile Include="Controllers\FromUriParamsContoller.cs" />
Expand Down