Skip to content

Commit

Permalink
Merge pull request #1378 from microsoft/bugfix/namespacing
Browse files Browse the repository at this point in the history
- fixes #1273 a bug where unnecessary namespaces would be added to models generation
  • Loading branch information
baywet authored Mar 15, 2022
2 parents 20ad6cf + aa27223 commit 156b142
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed

- Fixed a bug where unnecessary namespaces would be added to models generation #1273

## [0.0.18] - 2022-03-14

### Added
Expand Down
4 changes: 2 additions & 2 deletions src/Kiota.Builder/KiotaBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -759,8 +759,8 @@ private string GetModelsNamespaceNameFromReferenceId(string referenceId) {
referenceId = referenceId[config.ClientClassName.Length..];
referenceId = referenceId.Trim(nsNameSeparator);
var lastDotIndex = referenceId.LastIndexOf(nsNameSeparator);
var namespaceSuffix = lastDotIndex != -1 ? referenceId[..lastDotIndex] : referenceId;
return $"{modelsNamespace.Name}.{namespaceSuffix}";
var namespaceSuffix = lastDotIndex != -1 ? $".{referenceId[..lastDotIndex]}" : string.Empty;
return $"{modelsNamespace.Name}{namespaceSuffix}";
}
private CodeType CreateModelDeclarationAndType(OpenApiUrlTreeNode currentNode, OpenApiSchema schema, OpenApiOperation operation, CodeNamespace codeNamespace, string classNameSuffix = "", OpenApiResponse response = default) {
var className = currentNode.GetClassName(operation: operation, suffix: classNameSuffix, response: response, schema: schema);
Expand Down
110 changes: 110 additions & 0 deletions tests/Kiota.Builder.Tests/KiotaBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1025,4 +1025,114 @@ public void MapsPrimitiveFormats(string type, string format, string expected){
Assert.Equal(expected, method.ReturnType.Name);
Assert.True(method.ReturnType.AllTypes.First().IsExternal);
}
[Fact]
public void DoesntGenerateNamesapacesWhenNotRequired(){
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
};
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 myObjectModel = modelsNS.FindChildByName<CodeClass>("Myobject", false);
Assert.NotNull(myObjectModel);
var modelsSubNS = codeModel.FindNamespaceByName("TestSdk.Models.Myobject");
Assert.Null(modelsSubNS);
}
[Fact]
public void GeneratesNamesapacesWhenRequired(){
var myObjectSchema = new OpenApiSchema {
Type = "object",
Properties = new Dictionary<string, OpenApiSchema> {
{
"name", new OpenApiSchema {
Type = "string"
}
}
},
Reference = new OpenApiReference {
Id = "subns.myobject",
Type = ReferenceType.Schema
},
UnresolvedReference = false
};
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> {
{
"subns.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);
Assert.Null(codeModel.FindChildByName<CodeClass>("Myobject", false));
var modelsSubNS = codeModel.FindNamespaceByName("TestSdk.Models.subns");
Assert.NotNull(modelsSubNS);
Assert.NotNull(modelsSubNS.FindChildByName<CodeClass>("Myobject", false));
}
}

0 comments on commit 156b142

Please sign in to comment.