-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- adds support for go snippets generation (#749)
* - draft go snippets generation Signed-off-by: Vincent Biret <[email protected]> * - additional unit tests fixes for go snippets generation Signed-off-by: Vincent Biret <[email protected]> * - fixes object generation for go snippets Signed-off-by: Vincent Biret <[email protected]> * - code linting Signed-off-by: Vincent Biret <[email protected]> * - fixes debugging experience for API * - adds go to the list of languages in snipet generation description * - fixes a bug where empty bodies would be added for go generation, code linting * - fixes a bug where openapi snippet generation would fail for old odata index format * - fixes a bug where missing content type could derail go snippet generation - fixes a bug where empty collections could derail go snippets generation Signed-off-by: Vincent Biret <[email protected]> * - replicates json payload tolerance change to CSharp and TypeScript snippets generators * - fixes a bug where go snippets generation would fail on terminal slash - fixes a bug where go snippets generation would fail on path casing - fixes a bug where go snippets generation would fail on odata filter functions Signed-off-by: Vincent Biret <[email protected]> * - adds go to the list of snippets to generate * - adds a bypass for go snippets generation to use openapi Signed-off-by: Vincent Biret <[email protected]> * - fixes supported languages validation Signed-off-by: Vincent Biret <[email protected]> * Update CodeSnippetsReflection.OpenAPI/LanguageGenerators/GoGenerator.cs Co-authored-by: Eastman <[email protected]> * Update CodeSnippetsReflection.OpenAPI/LanguageGenerators/GoGenerator.cs Co-authored-by: Eastman <[email protected]> * - code linting: removes constants Signed-off-by: Vincent Biret <[email protected]> * consistent whitespacing Co-authored-by: Eastman <[email protected]> Co-authored-by: Andrew Omondi <[email protected]>
- Loading branch information
1 parent
d216534
commit 92ca8f8
Showing
18 changed files
with
765 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
274 changes: 274 additions & 0 deletions
274
CodeSnippetsReflection.OpenAPI.Test/GoGeneratorTests.cs
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
CodeSnippetsReflection.OpenAPI/KiotaOpenApiOperationExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// THIS CLASS IS COPIED FROM KIOTA TO GET THE SAME NAMING CONVENTIONS, WE SHOULD FIND A WAY TO MUTUALIZE THE CODE | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Microsoft.OpenApi.Models; | ||
|
||
namespace CodeSnippetsReflection.OpenAPI { | ||
public static class OpenApiOperationExtensions { | ||
private static readonly HashSet<string> successCodes = new() {"200", "201", "202"}; //204 excluded as it won't have a schema | ||
public static OpenApiSchema GetResponseSchema(this OpenApiOperation operation) | ||
{ | ||
// Return Schema that represents all the possible success responses! | ||
// For the moment assume 200s and application/json | ||
var schemas = operation.Responses.Where(r => successCodes.Contains(r.Key)) | ||
.SelectMany(re => re.Value.Content) | ||
.Where(c => c.Key == "application/json") | ||
.Select(co => co.Value.Schema) | ||
.Where(s => s is not null); | ||
|
||
return schemas.FirstOrDefault(); | ||
} | ||
} | ||
|
||
} |
13 changes: 13 additions & 0 deletions
13
CodeSnippetsReflection.OpenAPI/KiotaOpenApiReferenceExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// THIS CLASS IS COPIED FROM KIOTA TO GET THE SAME NAMING CONVENTIONS, WE SHOULD FIND A WAY TO MUTUALIZE THE CODE | ||
using CodeSnippetsReflection.StringExtensions; | ||
using Microsoft.OpenApi.Models; | ||
|
||
namespace CodeSnippetsReflection.OpenAPI { | ||
public static class OpenApiReferenceExtensions { | ||
public static string GetClassName(this OpenApiReference reference) { | ||
var referenceId = reference?.Id; | ||
return referenceId?[((referenceId?.LastIndexOf('.') ?? 0) + 1)..] | ||
?.ToFirstCharacterUpperCase(); | ||
} | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
CodeSnippetsReflection.OpenAPI/KiotaOpenApiUrlTreeNodeExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// ------------------------------------------------------------------------------------------------------------------------------------------------------ | ||
// Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. See License in the project root for license information. | ||
// ------------------------------------------------------------------------------------------------------------------------------------------------------ | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text.RegularExpressions; | ||
using CodeSnippetsReflection.StringExtensions; | ||
using Microsoft.OpenApi.Models; | ||
using Microsoft.OpenApi.Services; | ||
|
||
// THIS CLASS IS COPIED FROM KIOTA TO GET THE SAME NAMING CONVENTIONS, WE SHOULD FIND A WAY TO MUTUALIZE THE CODE | ||
namespace CodeSnippetsReflection.OpenAPI { | ||
|
||
public static class KiotaOpenApiUrlTreeNodeExtensions { | ||
private static readonly Regex PathParametersRegex = new(@"(?:\w+)?=?'?\{(?<paramName>\w+)\}'?,?", RegexOptions.Compiled); | ||
private static readonly char requestParametersChar = '{'; | ||
private static readonly char requestParametersEndChar = '}'; | ||
private static readonly char requestParametersSectionChar = '('; | ||
private static readonly char requestParametersSectionEndChar = ')'; | ||
private static readonly MatchEvaluator requestParametersMatchEvaluator = (match) => { | ||
return "With" + match.Groups["paramName"].Value.ToFirstCharacterUpperCase(); | ||
}; | ||
private static readonly Regex idClassNameCleanup = new(@"Id\d?$", RegexOptions.Compiled); | ||
///<summary> | ||
/// Returns the class name for the node with more or less precision depending on the provided arguments | ||
///</summary> | ||
public static string GetClassName(this OpenApiUrlTreeNode currentNode, string suffix = default, string prefix = default, OpenApiOperation operation = default) { | ||
var rawClassName = (operation?.GetResponseSchema()?.Reference?.GetClassName() ?? | ||
CleanupParametersFromPath(currentNode.Segment)?.ReplaceValueIdentifier()) | ||
.TrimEnd(requestParametersEndChar) | ||
.TrimStart(requestParametersChar) | ||
.TrimStart('$') //$ref from OData | ||
.Split('-') | ||
.First(); | ||
if((currentNode?.DoesNodeBelongToItemSubnamespace() ?? false) && idClassNameCleanup.IsMatch(rawClassName)) | ||
rawClassName = idClassNameCleanup.Replace(rawClassName, string.Empty); | ||
return prefix + rawClassName?.Split('.', StringSplitOptions.RemoveEmptyEntries)?.LastOrDefault() + suffix; | ||
} | ||
public static bool DoesNodeBelongToItemSubnamespace(this OpenApiUrlTreeNode currentNode) => currentNode.IsPathSegmentWithSingleSimpleParameter(); | ||
public static bool IsPathSegmentWithSingleSimpleParameter(this OpenApiUrlTreeNode currentNode) => | ||
currentNode?.Segment.IsPathSegmentWithSingleSimpleParameter() ?? false; | ||
private static bool IsPathSegmentWithSingleSimpleParameter(this string currentSegment) | ||
{ | ||
return (currentSegment?.StartsWith(requestParametersChar) ?? false) && | ||
currentSegment.EndsWith(requestParametersEndChar) && | ||
currentSegment.Count(x => x == requestParametersChar) == 1; | ||
} | ||
private static string CleanupParametersFromPath(string pathSegment) { | ||
if((pathSegment?.Contains(requestParametersChar) ?? false) || | ||
(pathSegment?.Contains(requestParametersSectionChar) ?? false)) | ||
return PathParametersRegex.Replace(pathSegment, requestParametersMatchEvaluator) | ||
.TrimEnd(requestParametersSectionEndChar) | ||
.Replace(requestParametersSectionChar.ToString(), string.Empty); | ||
return pathSegment; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.