Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

- refactoring: all regexes use a generate attribute #3813

Merged
merged 16 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions .github/workflows/load-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
name: Generation load tests

on:
workflow_dispatch:
schedule:
- cron: "0 4 * * 1" # every monday at 3am UTC

jobs:
test:
name: Test
runs-on: ubuntu-latest
strategy:
matrix:
exec:
[
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
14,
15,
16,
17,
18,
19,
20,
]
fail-fast: false
steps:
- uses: actions/checkout@v4
- name: Setup .NET
uses: actions/setup-dotnet@v3
with:
dotnet-version: 8.x
- name: Compile the CLI in debug mode
run: |
dotnet restore ./src/kiota
dotnet build ./src/kiota
- name: Run the CLI
run: |
curl -o petstore.yaml https://raw.githubusercontent.com/OAI/OpenAPI-Specification/main/examples/v3.0/petstore.yaml
curl -o twitter.json https://api.apis.guru/v2/specs/twitter.com/current/2.61/openapi.json
curl -o notion.json https://api.apis.guru/v2/specs/notion.com/1.0.0/openapi.json
curl -o stripe.json https://api.apis.guru/v2/specs/stripe.com/2022-11-15/openapi.json
curl -o maps.yaml https://raw.githubusercontent.com/googlemaps/openapi-specification/main/dist/google-maps-platform-openapi3.yml
curl -o meraki.json https://api.apis.guru/v2/specs/meraki.com/0.0.0-streaming/openapi.json
curl -o pipedrive.yaml https://developers.pipedrive.com/docs/api/v1/openapi.yaml
curl -o twilio.json https://api.apis.guru/v2/specs/twilio.com/api/1.42.0/openapi.json
curl -o docusign.json https://api.apis.guru/v2/specs/docusign.net/v2.1/openapi.json
nohup sh -c "for i in 1 2 3 4 5 6 7 8 9 10; do while : ; do : ; done & done" > /dev/null 2> /dev/null &
for ((n=0;n<100;n++))
do
src/kiota/bin/Debug/net8.0/kiota generate --openapi="$(pwd)/petstore.yaml" --output=tmp --clean-output --clear-cache --language java --dvr all
src/kiota/bin/Debug/net8.0/kiota generate --openapi="$(pwd)/twitter.json" --output=tmp --clean-output --clear-cache --language java --dvr all
src/kiota/bin/Debug/net8.0/kiota generate --openapi="$(pwd)/notion.json" --output=tmp --clean-output --clear-cache --language java --dvr all
src/kiota/bin/Debug/net8.0/kiota generate --openapi="$(pwd)/stripe.json" --output=tmp --clean-output --clear-cache --language java --dvr all
src/kiota/bin/Debug/net8.0/kiota generate --openapi="$(pwd)/maps.yaml" --output=tmp --clean-output --clear-cache --language java --dvr all
src/kiota/bin/Debug/net8.0/kiota generate --openapi="$(pwd)/meraki.json" --output=tmp --clean-output --clear-cache --language java --dvr all
src/kiota/bin/Debug/net8.0/kiota generate --openapi="$(pwd)/pipedrive.yaml" --output=tmp --clean-output --clear-cache --language java --dvr all
src/kiota/bin/Debug/net8.0/kiota generate --openapi="$(pwd)/twilio.json" --output=tmp --clean-output --clear-cache --language java --dvr all
src/kiota/bin/Debug/net8.0/kiota generate --openapi="$(pwd)/docusign.json" --output=tmp --clean-output --clear-cache --language java --dvr all
done
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fixed a bug where path parameters deduplication would create collisions on sub path segments. [#3757](https://github.com/microsoft/kiota/issues/3757)
- Moved from net7 to net8.
- Removed windows OS from docker multi-partform image to align with [dotnet changes](https://github.com/dotnet/dotnet-docker/issues/4492).
- Fixed a bug where import statements for additionalDataHolder and enumSet are missing when BackingStore is enabled in java. [#3643](https://github.com/microsoft/kiota/pull/3643)
- Fixed a bug where import statements for additionalDataHolder and enumSet are missing when BackingStore is enabled in java. [#3643](https://github.com/microsoft/kiota/pull/3643)
- Fixed an issue where Kiota's regex's might time out. [#3797](https://github.com/microsoft/kiota/issues/3797)
- Fixed a bug where getBackingStore method body was malformed for java. [#3643](https://github.com/microsoft/kiota/pull/3643)
- Fixed a bug where serialize method will not write additional data when backingStore is enabled for java. [#3643](https://github.com/microsoft/kiota/pull/3643)
- Fixed a bug where paths with parameters in the same position but different names would make generation fail. [#3756](https://github.com/microsoft/kiota/issues/3756)
Expand Down
2 changes: 1 addition & 1 deletion src/Kiota.Builder/CodeDOM/CodeBlock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public virtual void RenameChildElement(string oldName, string newName)
}
else throw new InvalidOperationException($"The element to rename was not found {oldName}");
}
public void RemoveChildElement<T>(params T[] elements) where T : CodeElement
public void RemoveChildElement<T>(params T[] elements) where T : ICodeElement
{
if (elements == null) return;
RemoveChildElementByName(elements.Select(static x => x.Name).ToArray());
Expand Down
1 change: 1 addition & 0 deletions src/Kiota.Builder/CodeDOM/ICodeElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ string Name
{
get; set;
}
T GetImmediateParentOfType<T>(CodeElement? item = null);
}
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,8 @@ public IEnumerable<string> GetContentTypes(IEnumerable<string> searchTypes)
.ThenByDescending(static x => x.Key, StringComparer.OrdinalIgnoreCase)
.Select(static x => x.Key);
}
[GeneratedRegex(@"[^/+]+\+", RegexOptions.IgnoreCase | RegexOptions.Compiled | RegexOptions.Singleline, 2000)]
[GeneratedRegex(@"[^/+]+\+", RegexOptions.IgnoreCase | RegexOptions.Singleline, 2000)]
private static partial Regex vendorStripRegex();
private readonly static Regex vendorStripRegexInstance = vendorStripRegex();
private bool TryGetMimeType(string mimeType, out float result)
{
if (string.IsNullOrEmpty(mimeType))
Expand All @@ -140,10 +139,10 @@ private bool TryGetMimeType(string mimeType, out float result)

return _mimeTypes.TryGetValue(mimeType, out result) || // vendor and parameters
mimeType.Contains('+', StringComparison.OrdinalIgnoreCase) &&
_mimeTypes.TryGetValue(vendorStripRegexInstance.Replace(mimeType, string.Empty), out result) || // no vendor with parameters
_mimeTypes.TryGetValue(vendorStripRegex().Replace(mimeType, string.Empty), out result) || // no vendor with parameters
mimeType.Contains(';', StringComparison.OrdinalIgnoreCase) &&
mimeType.Split(';', StringSplitOptions.RemoveEmptyEntries)[0] is string noParametersMimeType &&
(_mimeTypes.TryGetValue(noParametersMimeType, out result) || // vendor without parameters
_mimeTypes.TryGetValue(vendorStripRegexInstance.Replace(noParametersMimeType, string.Empty), out result)); // no vendor without parameters
_mimeTypes.TryGetValue(vendorStripRegex().Replace(noParametersMimeType, string.Empty), out result)); // no vendor without parameters
}
}
6 changes: 1 addition & 5 deletions src/Kiota.Builder/Constants.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
using System;

namespace Kiota.Builder;
namespace Kiota.Builder;
public static class Constants
{
public const string DefaultOpenApiLabel = "default";
public const string RawUrlParameterName = "request-raw-url";
public static readonly TimeSpan DefaultRegexTimeout = TimeSpan.FromMilliseconds(100);
public const string TempDirectoryName = "kiota";
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ namespace Kiota.Builder.EqualityComparers;

internal sealed partial class OpenApiServerComparer : IEqualityComparer<OpenApiServer>
{
private static readonly Regex _protocolCleanupRegex = GetCleanupRegex();
[GeneratedRegex("^https?://", RegexOptions.IgnoreCase | RegexOptions.Compiled, 200)]
private static partial Regex GetCleanupRegex();
[GeneratedRegex("^https?://", RegexOptions.IgnoreCase, 500)]
private static partial Regex protocolCleanupRegex();
public bool Equals(OpenApiServer? x, OpenApiServer? y)
{
return x != null && y != null && GetHashCode(x) == GetHashCode(y);
Expand All @@ -19,6 +18,6 @@ public int GetHashCode([DisallowNull] OpenApiServer obj)
{
if (string.IsNullOrEmpty(obj?.Url))
return 0;
return _protocolCleanupRegex.Replace(obj.Url, string.Empty).GetHashCode(StringComparison.OrdinalIgnoreCase);
return protocolCleanupRegex().Replace(obj.Url, string.Empty).GetHashCode(StringComparison.OrdinalIgnoreCase);
}
}
14 changes: 11 additions & 3 deletions src/Kiota.Builder/Extensions/OpenApiOperationExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using Kiota.Builder.Configuration;
using Microsoft.OpenApi.Models;

namespace Kiota.Builder.Extensions;
public static class OpenApiOperationExtensions
{
internal static readonly HashSet<string> SuccessCodes = new(StringComparer.OrdinalIgnoreCase) { "200", "201", "202", "203", "206", "2XX" }; //204 excluded as it won't have a schema
private static string vendorSpecificCleanup(string input)
{
var slashIndex = input.IndexOf('/', StringComparison.OrdinalIgnoreCase);
var plusIndex = input.IndexOf('+', StringComparison.OrdinalIgnoreCase);
if (slashIndex == -1 || plusIndex == -1)
return input;
if (plusIndex < slashIndex)
return input;
return input[0..(slashIndex + 1)] + input[(plusIndex + 1)..];
}
/// <summary>
/// cleans application/vnd.github.mercy-preview+json to application/json
/// </summary>
private static readonly Regex vendorSpecificCleanup = new(@"[^/]+\+", RegexOptions.Compiled, Constants.DefaultRegexTimeout);
internal static OpenApiSchema? GetResponseSchema(this OpenApiOperation operation, StructuredMimeTypesCollection structuredMimeTypes)
{
ArgumentNullException.ThrowIfNull(operation);
Expand Down Expand Up @@ -48,7 +56,7 @@ internal static IEnumerable<OpenApiSchema> GetValidSchemas(this IDictionary<stri
return source
.Where(static c => !string.IsNullOrEmpty(c.Key))
.Select(static c => (Key: c.Key.Split(';', StringSplitOptions.RemoveEmptyEntries)[0], c.Value))
.Where(c => structuredMimeTypes.Contains(c.Key) || structuredMimeTypes.Contains(vendorSpecificCleanup.Replace(c.Key, string.Empty)))
.Where(c => structuredMimeTypes.Contains(c.Key) || structuredMimeTypes.Contains(vendorSpecificCleanup(c.Key)))
.Select(static co => co.Value.Schema)
.Where(static s => s is not null);
}
Expand Down
Loading
Loading