From bdbfe432f9291d97459e7de45dfffa5897af5956 Mon Sep 17 00:00:00 2001 From: Jakubozga Date: Mon, 12 Dec 2016 11:28:07 +0100 Subject: [PATCH 1/2] Added static method MethodNotAllowed in JsonApiException --- JSONAPI/Documents/Builders/JsonApiException.cs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/JSONAPI/Documents/Builders/JsonApiException.cs b/JSONAPI/Documents/Builders/JsonApiException.cs index 00472ea0..4b72176c 100644 --- a/JSONAPI/Documents/Builders/JsonApiException.cs +++ b/JSONAPI/Documents/Builders/JsonApiException.cs @@ -99,5 +99,20 @@ public static JsonApiException CreateForForbidden(string detail = null) }; return new JsonApiException(error); } + + /// + /// Creates a JsonApiException to send a 405 Method Not Allowed error. + /// + public static JsonApiException CreateForMethodNotAllowed(string detail = null) + { + var error = new Error + { + Id = Guid.NewGuid().ToString(), + Status = HttpStatusCode.MethodNotAllowed, + Title = "Method not allowed", + Detail = detail + }; + return new JsonApiException(error); + } } } From 85ab7a4a23289be56702a684e8fee94b096370c2 Mon Sep 17 00:00:00 2001 From: Jakubozga Date: Fri, 13 Jan 2017 11:11:08 +0100 Subject: [PATCH 2/2] Fixed issue with exceptions being thrown for derivied entities --- .../DbContextExtensions.cs | 34 +++++++++---------- 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/JSONAPI.EntityFramework/DbContextExtensions.cs b/JSONAPI.EntityFramework/DbContextExtensions.cs index 37724b77..b18d0e1c 100644 --- a/JSONAPI.EntityFramework/DbContextExtensions.cs +++ b/JSONAPI.EntityFramework/DbContextExtensions.cs @@ -21,28 +21,27 @@ public static class DbContextExtensions /// public static IEnumerable GetKeyNames(this DbContext dbContext, Type type) { - if (dbContext == null) throw new ArgumentNullException("dbContext"); - if (type == null) throw new ArgumentNullException("type"); + if (dbContext == null) throw new ArgumentNullException(nameof(dbContext)); + if (type == null) throw new ArgumentNullException(nameof(type)); - var originalType = type; - - while (type != null) + var baseEntityType = type; + while (baseEntityType.BaseType != typeof(Object)) { - var openMethod = typeof(DbContextExtensions).GetMethod("GetKeyNamesFromGeneric", BindingFlags.Public | BindingFlags.Static); - var method = openMethod.MakeGenericMethod(type); - - try - { - return (IEnumerable) method.Invoke(null, new object[] {dbContext}); - } - catch (TargetInvocationException) - { - } + baseEntityType = baseEntityType.BaseType; + } + + var openMethod = typeof(DbContextExtensions).GetMethod("GetKeyNamesFromGeneric", BindingFlags.Public | BindingFlags.Static); + var method = openMethod.MakeGenericMethod(baseEntityType); - type = type.BaseType; + try + { + return (IEnumerable) method.Invoke(null, new object[] {dbContext}); + } + catch (TargetInvocationException) + { } - throw new Exception(string.Format("Failed to identify the key names for {0} or any of its parent classes.", originalType.Name)); + throw new Exception(string.Format("Failed to identify the key names for {0} or any of its parent classes.", type.Name)); } /// @@ -59,7 +58,6 @@ public static IEnumerable GetKeyNamesFromGeneric(this DbContext dbCon try { objectSet = objectContext.CreateObjectSet(); - } catch (InvalidOperationException e) {