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

- fixes a bug where line returns in descriptions could break the generated code #1512

Merged
merged 1 commit into from
Apr 11, 2022
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed

- Fixed a bug where line returns in descriptions could break the generated code. [#1504](https://github.com/microsoft/kiota/issues/1504)

## [0.0.22] - 2022-04-08

### Added
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,13 @@ public static string GetClassName(this OpenApiUrlTreeNode currentNode, string su
}
return prefix + rawClassName?.Split('.', StringSplitOptions.RemoveEmptyEntries)?.LastOrDefault() + suffix;
}
private static readonly Regex descriptionCleanupRegex = new (@"[\r\n\t]", RegexOptions.Compiled);
public static string CleanupDescription(this string description) => string.IsNullOrEmpty(description) ? description : descriptionCleanupRegex.Replace(description, string.Empty);
public static string GetPathItemDescription(this OpenApiUrlTreeNode currentNode, string label, string defaultValue = default) =>
!string.IsNullOrEmpty(label) && (currentNode?.PathItems.ContainsKey(label) ?? false) ?
currentNode.PathItems[label].Description ??
(currentNode.PathItems[label].Description ??
currentNode.PathItems[label].Summary ??
defaultValue :
defaultValue).CleanupDescription() :
defaultValue;
public static bool DoesNodeBelongToItemSubnamespace(this OpenApiUrlTreeNode currentNode) => currentNode.IsPathSegmentWithSingleSimpleParameter();
public static bool IsPathSegmentWithSingleSimpleParameter(this OpenApiUrlTreeNode currentNode) =>
Expand Down
20 changes: 10 additions & 10 deletions src/Kiota.Builder/KiotaBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ private static void AddPathParametersToMethod(OpenApiUrlTreeNode currentNode, Co
var mParameter = new CodeParameter {
Name = parameter.Name,
Optional = asOptional,
Description = parameter.Description,
Description = parameter.Description.CleanupDescription(),
Kind = CodeParameterKind.Path,
UrlTemplateParameterName = parameter.Name,
};
Expand Down Expand Up @@ -555,7 +555,7 @@ private CodeProperty CreateProperty(string childIdentifier, string childType, st
Name = propertyName,
DefaultValue = defaultValue,
Kind = kind,
Description = typeSchema?.Description ?? $"The {propertyName} property",
Description = typeSchema?.Description.CleanupDescription() ?? $"The {propertyName} property",
};
if(propertyName != childIdentifier)
prop.SerializationName = childIdentifier;
Expand Down Expand Up @@ -642,7 +642,7 @@ private void CreateOperationMethods(OpenApiUrlTreeNode currentNode, OperationTyp
Name = operationType.ToString(),
Kind = CodeMethodKind.RequestExecutor,
HttpMethod = method,
Description = operation.Description ?? operation.Summary,
Description = (operation.Description ?? operation.Summary).CleanupDescription(),
}).FirstOrDefault();
AddErrorMappingsForExecutorMethod(currentNode, operation, executorMethod);
if (schema != null)
Expand Down Expand Up @@ -687,7 +687,7 @@ private void CreateOperationMethods(OpenApiUrlTreeNode currentNode, OperationTyp
Kind = CodeMethodKind.RequestGenerator,
IsAsync = false,
HttpMethod = method,
Description = operation.Description ?? operation.Summary,
Description = (operation.Description ?? operation.Summary).CleanupDescription(),
ReturnType = new CodeType { Name = "RequestInformation", IsNullable = false, IsExternal = true},
};
if (config.Language == GenerationLanguage.Shell)
Expand All @@ -706,7 +706,7 @@ private static void SetPathQueryAndHeaderParameters(CodeMethod target, OpenApiUr
{
Name = x.Name.TrimStart('$'),
Type = GetQueryParameterType(x.Schema),
Description = x.Description,
Description = x.Description.CleanupDescription(),
Kind = GetParameterKindFromLocation(x.In),
Optional = !x.Required
})
Expand All @@ -717,7 +717,7 @@ private static void SetPathQueryAndHeaderParameters(CodeMethod target, OpenApiUr
{
Name = x.Name.TrimStart('$'),
Type = GetQueryParameterType(x.Schema),
Description = x.Description,
Description = x.Description.CleanupDescription(),
Kind = GetParameterKindFromLocation(x.In),
Optional = !x.Required
}))
Expand Down Expand Up @@ -746,7 +746,7 @@ private void AddRequestBuilderMethodParameters(OpenApiUrlTreeNode currentNode, O
Type = requestBodyType,
Optional = false,
Kind = CodeParameterKind.RequestBody,
Description = requestBodySchema.Description
Description = requestBodySchema.Description.CleanupDescription()
});
method.ContentType = nonBinaryRequestBody.Value.Key;
} else if (operation.RequestBody?.Content?.ContainsKey(RequestBodyBinaryContentType) ?? false) {
Expand Down Expand Up @@ -958,7 +958,7 @@ private CodeClass AddModelClass(OpenApiUrlTreeNode currentNode, OpenApiSchema sc
var newClass = currentNamespace.AddClass(new CodeClass {
Name = declarationName,
Kind = CodeClassKind.Model,
Description = schema.Description ?? (string.IsNullOrEmpty(schema.Reference?.Id) ?
Description = schema.Description.CleanupDescription() ?? (string.IsNullOrEmpty(schema.Reference?.Id) ?
currentNode.GetPathItemDescription(Constants.DefaultOpenApiLabel) :
null),// if it's a referenced component, we shouldn't use the path item description as it makes it indeterministic
}).First();
Expand Down Expand Up @@ -1126,14 +1126,14 @@ private CodeClass CreateOperationParameter(OpenApiUrlTreeNode node, OperationTyp
{
Name = operationType.ToString() + "QueryParameters",
Kind = CodeClassKind.QueryParameters,
Description = operation.Description ?? operation.Summary
Description = (operation.Description ?? operation.Summary).CleanupDescription(),
};
foreach (var parameter in parameters)
{
var prop = new CodeProperty
{
Name = FixQueryParameterIdentifier(parameter),
Description = parameter.Description,
Description = parameter.Description.CleanupDescription(),
Type = new CodeType
{
IsExternal = true,
Expand Down
56 changes: 56 additions & 0 deletions tests/Kiota.Builder.Tests/KiotaBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1827,6 +1827,62 @@ public void ModelsDoesntUsePathDescriptionWhenAvailable(){
Assert.NotNull(responseClass);
Assert.Null(responseClass.Description);
}
[Fact]
public void CleansUpInvalidDescriptionCharacters(){
var myObjectSchema = new OpenApiSchema {
Type = "object",
Properties = new Dictionary<string, OpenApiSchema> {
{
"name", new OpenApiSchema {
Type = "string",
}
}
},
Reference = new OpenApiReference {
Id = "myobject",
Type = ReferenceType.Schema
},
UnresolvedReference = false,
Description = @" some description with invalid characters:
",
};
var document = new OpenApiDocument() {
Paths = new OpenApiPaths() {
["answer"] = new OpenApiPathItem() {
Operations = {
[OperationType.Get] = new OpenApiOperation() {
Responses = new OpenApiResponses
{
["200"] = new OpenApiResponse {
Content = {
["application/json"] = new OpenApiMediaType {
Schema = myObjectSchema
}
}
},
}
}
}
}
},
Components = new() {
Schemas = new Dictionary<string, OpenApiSchema> {
{
"myobject", myObjectSchema
}
}
}
};
var mockLogger = new Mock<ILogger<KiotaBuilder>>();
var builder = new KiotaBuilder(mockLogger.Object, new GenerationConfiguration() { ClientClassName = "TestClient", ClientNamespaceName = "TestSdk", ApiRootUrl = "https://localhost" });
var node = builder.CreateUriSpace(document);
var codeModel = builder.CreateSourceModel(node);
var modelsNS = codeModel.FindNamespaceByName("TestSdk.Models");
Assert.NotNull(modelsNS);
var responseClass = modelsNS.Classes.FirstOrDefault(x => x.IsOfKind(CodeClassKind.Model));
Assert.NotNull(responseClass);
Assert.Equal("some description with invalid characters: ", responseClass.Description);
}
[InlineData("application/json")]
[InlineData("application/json;odata.metadata=minimal;odata.streaming=true;IEEE754Compatible=false;charset=utf-8")]
[InlineData("application/vnd.github.mercy-preview+json")]
Expand Down