Reputation: 1022
I am trying to ignore properties from a Swagger API if their type implements an interface.
Here is my schema filter:
Public Class cSwaggerIgnoreNavigationProperties
Implements ISchemaFilter
Public Sub Apply(voSchema As OpenApiSchema, voContext As SchemaFilterContext) Implements ISchemaFilter.Apply
If voSchema.Properties.Count = 0 Then
Return
End If
Const oFlags = BindingFlags.Instance Or BindingFlags.NonPublic Or BindingFlags.Public
Dim oPropertyList = voContext.Type.GetProperties(oFlags)
Dim oExcludedList =
oPropertyList.Where(Function(p) p.PropertyType.IsAssignableTo(GetType(IEntity))) _
.Select(Function(p)
Dim oCamelCaser As cCamelCaser = New cCamelCaser()
Return If(p.GetCustomAttribute(Of JsonPropertyNameAttribute)()?.Name, oCamelCaser.ToLowerCamelCase(p.Name))
End Function)
For Each sExcluded In oExcludedList
If voSchema.Properties.ContainsKey(sExcluded) Then
voSchema.Properties.Remove(sExcluded)
End If
Next
End Sub
End Class
This works in most cases, but misses certain types altogether. Whilst debugging, I found that p.PropertyType.IsAssignableTo(GetType(IEntity))
returns false unexpectedly for (at least) one of my types.
My Patient
type in my S4D_Data
assembly inherits from a class which implements IEntity
.
Here are some outputs from the immediate window when breaking on that call:
?p.PropertyType.FullName
"S4D_Data.Patient"
?p.PropertyType.IsAssignableTo(GetType(S4D_Data.IEntity))
False
This shows that S4D_Data.Patient
does definitely implement IEntity
:
?GetType(S4D_Data.Patient).IsAssignableTo(GetType(S4D_Data.IEntity))
True
But this instance of propertyInfo.PropertyType even returns false when checking if it is its own type:
?p.PropertyType.IsAssignableTo(GetType(S4D_Data.Patient))
False
?p.PropertyType = GetType(S4D_Data.Patient)
False
It looks like the issue may be related to having two instances of the S4D_Data
assembly loaded, for some reason.
If I check the assembly for the named type and then for the reflected property type I get this output:
?GetType(S4D_Data.Patient).AssemblyQualifiedName
"S4D_Data.Patient, S4D Data, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
?p.PropertyType.AssemblyQualifiedName
"S4D_Data.Patient, V2e56f2547b0f45d2a33a0d70b908a55e.DynamicModels, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"
The reflected instance appears to reference a dynamic assembly.
Upvotes: 1
Views: 36