Skip to content

Commit

Permalink
Bugfix/nestedclass (#15)
Browse files Browse the repository at this point in the history
* Add tests for scenario when swagger contains nested references

* Fixed generating swagger when contains nested references

* New version
  • Loading branch information
Burgyn authored Feb 5, 2019
1 parent 7e3b4ac commit 2234489
Show file tree
Hide file tree
Showing 6 changed files with 462 additions and 13 deletions.
4 changes: 1 addition & 3 deletions src/MMLib.SwaggerForOcelot/MMLib.SwaggerForOcelot.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Version>1.0.0</Version>
<Version>1.0.1</Version>
<Authors>Milan Martiniak</Authors>
<Company>MMLib</Company>
<Description>Swagger generator for Ocelot downstream services.</Description>
Expand All @@ -12,8 +12,6 @@
<PackageTags>swagger;documentation;ocelot</PackageTags>
<PackageReleaseNotes />
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<AssemblyVersion>1.0.0.0</AssemblyVersion>
<FileVersion>1.0.0.0</FileVersion>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<Copyright>@MMLib</Copyright>
<LangVersion>7.3</LangVersion>
Expand Down
31 changes: 21 additions & 10 deletions src/MMLib.SwaggerForOcelot/Transformation/SwaggerJsonTransformer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ private bool RemoveMethods(JProperty path, ReRouteOptions reRoute)

while (method != null)
{
if(!reRoute.ContainsHttpMethod(method.Name))
if (!reRoute.ContainsHttpMethod(method.Name))
{
forRemove.Add(method);
}
Expand All @@ -101,15 +101,7 @@ private static void RemoveItems<T>(JToken token, JToken paths, params Func<T, st

if (typeof(T) == typeof(JProperty))
{
var notForRemove = token.Cast<T>().Where(t => !forRemove.Contains(t)).Cast<JProperty>().ToList();
var subReference = forRemove
.Cast<JProperty>()
.Where(i
=> searchPaths
.Select(p => notForRemove.Any(t => t.SelectTokens(p(i as T)).Any())).Any(p => p))
.ToDictionary(p => p.Name, p => p);

forRemove.RemoveAll(p => subReference.ContainsKey((p as JProperty).Name));
CheckSubreferences(token, searchPaths, forRemove);
}

foreach (var item in forRemove)
Expand All @@ -125,6 +117,25 @@ private static void RemoveItems<T>(JToken token, JToken paths, params Func<T, st
}
}

private static void CheckSubreferences<T>(IEnumerable<JToken> token, Func<T, string>[] searchPaths, List<T> forRemove)
where T : class
{
var notForRemove = token.Cast<T>().Where(t => !forRemove.Contains(t)).Cast<JProperty>().ToList();
var subReference = forRemove
.Cast<JProperty>()
.Where(i
=> searchPaths
.Select(p => notForRemove.Any(t => t.SelectTokens(p(i as T)).Any())).Any(p => p))
.ToDictionary(p => p.Name, p => p);

forRemove.RemoveAll(p => subReference.ContainsKey((p as JProperty).Name));

if (subReference.Count > 0)
{
CheckSubreferences(subReference.Values, searchPaths, forRemove);
}
}

private static ReRouteOptions FindReRoute(IEnumerable<ReRouteOptions> reRoutes, string downstreamPath)
=> reRoutes.FirstOrDefault(p
=> p.CanCatchAll
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
<ItemGroup>
<None Remove="Resources\SwaggerBase.json" />
<None Remove="Resources\SwaggerBaseTransformed.json" />
<None Remove="Resources\SwaggerNestedClass.json" />
<None Remove="Resources\SwaggerNestedClassTransformed.json" />
<None Remove="Resources\SwaggerPetsBase.json" />
<None Remove="Resources\SwaggerPetsOnlyAnyActions.json" />
<None Remove="Resources\SwaggerPetsOnlyPost.json" />
Expand All @@ -17,6 +19,8 @@
</ItemGroup>

<ItemGroup>
<EmbeddedResource Include="Resources\SwaggerNestedClassTransformed.json" />
<EmbeddedResource Include="Resources\SwaggerNestedClass.json" />
<EmbeddedResource Include="Resources\SwaggerPetsOnlyAnyActions.json">
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</EmbeddedResource>
Expand Down
210 changes: 210 additions & 0 deletions tests/MMLib.SwaggerForOcelot.Tests/Resources/SwaggerNestedClass.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
{
"swagger": "2.0",
"info": {
"version": "v1",
"title": "Data",
"description": ""
},
"paths": {
"/api/reportdata/savings": {
"post": {
"tags": [
"ReportData"
],
"operationId": "GetData",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"parameters": [
{
"name": "contextRequest",
"in": "body",
"required": false,
"schema": {
"$ref": "#/definitions/RequestContext"
}
}
],
"responses": {
"200": {
"description": "Success",
"schema": {
"$ref": "#/definitions/SavingData"
}
}
}
}
}
},
"definitions": {
"RequestContext": {
"type": "object",
"properties": {
"organizationId": {
"type": "string"
},
"context": {
"$ref": "#/definitions/Context"
}
}
},
"Context": {
"type": "object",
"properties": {
"comment": {
"type": "string"
},
"facilityIds": {
"uniqueItems": false,
"type": "array",
"items": {
"type": "string"
}
},
"startTime": {
"type": "string"
},
"endTime": {
"type": "string"
},
"interval": {
"type": "string"
},
"groupBy": {
"type": "string"
}
}
},
"SavingData": {
"type": "object",
"properties": {
"mainChartData": {
"$ref": "#/definitions/MainChartSavingData"
},
"detailedChartData": {
"uniqueItems": false,
"type": "array",
"items": {
"$ref": "#/definitions/DetailedChartSavingData"
}
},
"groupPerformances": {
"uniqueItems": false,
"type": "array",
"items": {
"$ref": "#/definitions/GroupPerformanceFinancialData"
}
}
}
},
"MainChartSavingData": {
"type": "object",
"properties": {
"comfort": {
"format": "double",
"type": "number"
},
"energy": {
"format": "double",
"type": "number"
},
"realEstate": {
"format": "double",
"type": "number"
},
"total": {
"format": "double",
"type": "number"
},
"projection": {
"format": "double",
"type": "number"
},
"target": {
"format": "double",
"type": "number"
}
}
},
"DetailedChartSavingData": {
"type": "object",
"properties": {
"group": {
"type": "string"
},
"projection": {
"format": "double",
"type": "number"
},
"target": {
"format": "double",
"type": "number"
},
"actual": {
"$ref": "#/definitions/SavingActuals"
}
}
},
"GroupPerformanceFinancialData": {
"type": "object",
"properties": {
"criterion": {
"type": "string"
},
"groups": {
"uniqueItems": false,
"type": "array",
"items": {
"$ref": "#/definitions/FinancialPerformanceData"
}
}
}
},
"SavingActuals": {
"type": "object",
"properties": {
"comfort": {
"format": "double",
"type": "number"
},
"energy": {
"format": "double",
"type": "number"
},
"realEstate": {
"format": "double",
"type": "number"
}
}
},
"FinancialPerformanceData": {
"type": "object",
"properties": {
"name": {
"type": "string"
},
"value": {
"format": "int32",
"type": "integer"
}
}
}
},
"securityDefinitions": {
"Bearer": {
"name": "Authorization",
"in": "header",
"type": "apiKey",
"description": "JWT Authorization header using the Bearer scheme. Example: \"Authorization: Bearer {token}\""
}
},
"security": [
{
"Bearer": [],
"Basic": []
}
]
}
Loading

0 comments on commit 2234489

Please sign in to comment.