Skip to content

Commit

Permalink
fix: Fix NRE in InheritDocRequestBodyFilter and `InheritDocParamete…
Browse files Browse the repository at this point in the history
…rFilter`
  • Loading branch information
unchase committed Aug 30, 2021
1 parent 5c9c4cd commit f67155e
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 11 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@

These are the changes to each version that has been released on the [nuget](https://www.nuget.org/packages/Unchase.Swashbuckle.AspNetCore.Extensions/).

## v2.6.9 `2021-08-30`

- [x] Fix NRE in `InheritDocRequestBodyFilter` and `InheritDocParameterFilter`

## v2.6.8 `2021-08-26`

- [x] Add `IncludeXmlCommentsWithRemarks` improvements
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public InheritDocParameterFilter(List<XPathDocument> documents, Dictionary<strin
/// <param name="context"><see cref="ParameterFilterContext"/>.</param>
public void Apply(OpenApiParameter parameter, ParameterFilterContext context)
{
if (context.ApiParameterDescription.PropertyInfo() == null)
if (context.ApiParameterDescription?.PropertyInfo() == null)
{
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,25 +61,38 @@ public InheritDocRequestBodyFilter(List<XPathDocument> documents, Dictionary<str

public void Apply(OpenApiRequestBody requestBody, RequestBodyFilterContext context)
{
if (context.BodyParameterDescription.Type == null)
ApplyForType(requestBody, context, context.BodyParameterDescription?.Type);

if (context.FormParameterDescriptions?.Any() == true)
{
foreach (var formParameterDescription in context.FormParameterDescriptions)
{
ApplyForType(requestBody, context, formParameterDescription?.Type);
}
}
}

private void ApplyForType(OpenApiRequestBody requestBody, RequestBodyFilterContext context, Type type)
{
if (type == null)
{
return;
}

if (_excludedTypes.Any() && _excludedTypes.ToList().Contains(context.BodyParameterDescription.Type))
if (_excludedTypes.Any() && _excludedTypes.ToList().Contains(type))
{
return;
}

// Try to apply a description for inherited types.
string parameterMemberName = XmlCommentsNodeNameHelper.GetMemberNameForType(context.BodyParameterDescription.Type);
string parameterMemberName = XmlCommentsNodeNameHelper.GetMemberNameForType(type);
if (string.IsNullOrWhiteSpace(requestBody.Description) && _inheritedDocs.ContainsKey(parameterMemberName))
{
string cref = _inheritedDocs[parameterMemberName];
XPathNavigator targetXmlNode;
if (string.IsNullOrWhiteSpace(cref))
{
var target = context.BodyParameterDescription.Type.GetTargetRecursive(_inheritedDocs, cref);
var target = type.GetTargetRecursive(_inheritedDocs, cref);
if (target == null)
{
return;
Expand Down Expand Up @@ -108,9 +121,9 @@ public void Apply(OpenApiRequestBody requestBody, RequestBodyFilterContext conte
}
}

if (context.SchemaRepository.Schemas.ContainsKey(context.BodyParameterDescription.Type.Name))
if (context.SchemaRepository.Schemas.ContainsKey(type.Name))
{
var schema = context.SchemaRepository.Schemas[context.BodyParameterDescription.Type.Name];
var schema = context.SchemaRepository.Schemas[type.Name];
if (schema?.Properties?.Any() != true)
{
return;
Expand All @@ -119,7 +132,7 @@ public void Apply(OpenApiRequestBody requestBody, RequestBodyFilterContext conte
// Add the summary and examples for the properties.
foreach (var entry in schema.Properties)
{
var members = ((TypeInfo)context.BodyParameterDescription.Type).GetMembers();
var members = ((TypeInfo)type).GetMembers();
var memberInfo = members.FirstOrDefault(p =>
p.Name.Equals(entry.Key, StringComparison.OrdinalIgnoreCase));
if (memberInfo != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
<NeutralLanguage></NeutralLanguage>
<LangVersion>7.3</LangVersion>
<PackageIconUrl>https://github.com/unchase/Unchase.Swashbuckle.AspNetCore.Extensions/blob/master/assets/icon.png?raw=true</PackageIconUrl>
<Version>2.6.8</Version>
<AssemblyVersion>2.6.8.0</AssemblyVersion>
<FileVersion>2.6.8.0</FileVersion>
<Version>2.6.9</Version>
<AssemblyVersion>2.6.9.0</AssemblyVersion>
<FileVersion>2.6.9.0</FileVersion>
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
<DocumentationFile>Unchase.Swashbuckle.AspNetCore.Extensions.xml</DocumentationFile>
</PropertyGroup>
Expand Down

0 comments on commit f67155e

Please sign in to comment.