Skip to content

Commit 13812b1

Browse files
committed
Added CSDL filter for entitysets and singletons
1 parent 33a573b commit 13812b1

File tree

5 files changed

+76
-5
lines changed

5 files changed

+76
-5
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
3+
xmlns:edm="http://docs.oasis-open.org/odata/ns/edm"
4+
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
5+
>
6+
<xsl:output method="xml" indent="yes"/>
7+
8+
<xsl:param name="entitySetOrSingleton" select="'serviceappointments'"></xsl:param>
9+
10+
<xsl:template match="edm:EntityContainer">
11+
<xsl:copy>
12+
<xsl:apply-templates select="@* | edm:EntitySet[contains($entitySetOrSingleton,@Name)] | edm:Singleton[contains($entitySetOrSingleton,@Name)]"/>
13+
</xsl:copy>
14+
</xsl:template>
15+
16+
<xsl:template match="@* | node()">
17+
<xsl:copy>
18+
<xsl:apply-templates select="@* | node()"/>
19+
</xsl:copy>
20+
</xsl:template>
21+
22+
</xsl:stylesheet>

src/Microsoft.OpenApi.Hidi/Microsoft.OpenApi.Hidi.csproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@
3131
<EmbedUntrackedSources>true</EmbedUntrackedSources>
3232
</PropertyGroup>
3333

34+
<ItemGroup>
35+
<None Remove="CsdlFilter.xslt" />
36+
</ItemGroup>
37+
38+
<ItemGroup>
39+
<EmbeddedResource Include="CsdlFilter.xslt" />
40+
</ItemGroup>
41+
3442
<ItemGroup>
3543
<PackageReference Include="Microsoft.Extensions.Logging" Version="6.0.0" />
3644
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="6.0.1" />

src/Microsoft.OpenApi.Hidi/OpenApiService.cs

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@
2424
using Microsoft.OpenApi.Writers;
2525
using static Microsoft.OpenApi.Hidi.OpenApiSpecVersionHelper;
2626
using System.Threading;
27+
using System.Xml.Xsl;
28+
using System.Xml;
29+
using System.Runtime.CompilerServices;
30+
using System.Reflection;
2731

2832
namespace Microsoft.OpenApi.Hidi
2933
{
@@ -35,6 +39,7 @@ public class OpenApiService
3539
public static async Task<int> TransformOpenApiDocument(
3640
string openapi,
3741
string csdl,
42+
string csdlFilter,
3843
FileInfo output,
3944
bool cleanoutput,
4045
string? version,
@@ -85,6 +90,13 @@ CancellationToken cancellationToken
8590
openApiVersion = version != null ? TryParseOpenApiSpecVersion(version) : OpenApiSpecVersion.OpenApi3_0;
8691

8792
stream = await GetStream(csdl, logger, cancellationToken);
93+
94+
if (!string.IsNullOrEmpty(csdlFilter))
95+
{
96+
XslCompiledTransform transform = GetFilterTransform();
97+
stream = ApplyFilter(csdl, csdlFilter, transform);
98+
stream.Position = 0;
99+
}
88100
document = await ConvertCsdlToOpenApi(stream);
89101
stopwatch.Stop();
90102
logger.LogTrace("{timestamp}ms: Generated OpenAPI with {paths} paths.", stopwatch.ElapsedMilliseconds, document.Paths.Count);
@@ -210,6 +222,31 @@ CancellationToken cancellationToken
210222
}
211223
}
212224

225+
private static XslCompiledTransform GetFilterTransform()
226+
{
227+
XslCompiledTransform transform = new();
228+
Assembly assembly = typeof(OpenApiService).GetTypeInfo().Assembly;
229+
Stream xslt = assembly.GetManifestResourceStream("Microsoft.OpenApi.Hidi.CsdlFilter.xslt");
230+
transform.Load(new XmlTextReader(new StreamReader(xslt)));
231+
return transform;
232+
}
233+
234+
private static Stream ApplyFilter(string csdl, string entitySetOrSingleton, XslCompiledTransform transform)
235+
{
236+
Stream stream;
237+
StreamReader inputReader = new(csdl);
238+
XmlReader inputXmlReader = XmlReader.Create(inputReader);
239+
MemoryStream filteredStream = new();
240+
StreamWriter writer = new(filteredStream);
241+
XsltArgumentList args = new();
242+
args.AddParam("entitySetOrSingleton", "", entitySetOrSingleton);
243+
transform.Transform(inputXmlReader, args, writer);
244+
stream = filteredStream;
245+
return stream;
246+
}
247+
248+
249+
213250
/// <summary>
214251
/// Implementation of the validate command
215252
/// </summary>
@@ -306,8 +343,8 @@ public static async Task<OpenApiDocument> ConvertCsdlToOpenApi(Stream csdl)
306343
EnableDiscriminatorValue = false,
307344
EnableDerivedTypesReferencesForRequestBody = false,
308345
EnableDerivedTypesReferencesForResponses = false,
309-
ShowRootPath = true,
310-
ShowLinks = true
346+
ShowRootPath = false,
347+
ShowLinks = false
311348
};
312349
OpenApiDocument document = edmModel.ConvertToOpenApi(settings);
313350

src/Microsoft.OpenApi.Hidi/Program.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ static async Task Main(string[] args)
2424
var csdlOption = new Option<string>("--csdl", "Input CSDL file path or URL");
2525
csdlOption.AddAlias("-cs");
2626

27+
var csdlFilterOption = new Option<string>("--csdlFilter", "Name of EntitySet or Singleton to filter CSDL on");
28+
csdlOption.AddAlias("-csf");
29+
2730
var outputOption = new Option<FileInfo>("--output", () => new FileInfo("./output"), "The output directory path for the generated file.") { Arity = ArgumentArity.ZeroOrOne };
2831
outputOption.AddAlias("-o");
2932

@@ -66,6 +69,7 @@ static async Task Main(string[] args)
6669
{
6770
descriptionOption,
6871
csdlOption,
72+
csdlFilterOption,
6973
outputOption,
7074
cleanOutputOption,
7175
versionOption,
@@ -78,8 +82,8 @@ static async Task Main(string[] args)
7882
resolveExternalOption,
7983
};
8084

81-
transformCommand.SetHandler<string, string, FileInfo, bool, string?, OpenApiFormat?, LogLevel, bool, bool, string, string, string, CancellationToken> (
82-
OpenApiService.TransformOpenApiDocument, descriptionOption, csdlOption, outputOption, cleanOutputOption, versionOption, formatOption, logLevelOption, inlineOption, resolveExternalOption, filterByOperationIdsOption, filterByTagsOption, filterByCollectionOption);
85+
transformCommand.SetHandler<string, string, string, FileInfo, bool, string?, OpenApiFormat?, LogLevel, bool, bool, string, string, string, CancellationToken> (
86+
OpenApiService.TransformOpenApiDocument, descriptionOption, csdlOption, csdlFilterOption, outputOption, cleanOutputOption, versionOption, formatOption, logLevelOption, inlineOption, resolveExternalOption, filterByOperationIdsOption, filterByTagsOption, filterByCollectionOption);
8387

8488
rootCommand.Add(transformCommand);
8589
rootCommand.Add(validateCommand);

test/Microsoft.OpenApi.Tests/Services/OpenApiServiceTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public async Task ReturnConvertedCSDLFile()
2222

2323
// Act
2424
var openApiDoc = await OpenApiService.ConvertCsdlToOpenApi(csdlStream);
25-
var expectedPathCount = 6;
25+
var expectedPathCount = 5;
2626

2727
// Assert
2828
Assert.NotNull(openApiDoc);

0 commit comments

Comments
 (0)