Skip to content

Commit

Permalink
Merge pull request #5891 from microsoft/fix/implements-error-inline
Browse files Browse the repository at this point in the history
fix: missing interface declaration for inherited error models
  • Loading branch information
baywet authored Dec 11, 2024
2 parents 0731517 + 2b088ef commit 5dbc92e
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 5 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed

- Fixed a bug in generation when a referenced schema in an allOf was a primitive [#5701](https://github.com/microsoft/kiota/issues/5701).
- Fixed a bug where inherited error models would be missing interface declarations. [#5888](https://github.com/microsoft/kiota/issues/5888)

## [1.21.0] - 2024-12-05

Expand Down Expand Up @@ -1519,4 +1520,3 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

- Initial GitHub release

2 changes: 1 addition & 1 deletion src/Kiota.Builder/Refiners/CommonLanguageRefiner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -946,7 +946,7 @@ parent is CodeType parentType &&
foreach (var implement in currentParent
.StartBlock
.Implements
.Where(pi => !currentClass.Usings.Any(ci => ci.Name.Equals(pi.Name, StringComparison.OrdinalIgnoreCase))))
.Where(pi => !currentClass.Usings.Any(ci => !ci.IsExternal && ci.Name.Equals(pi.Name, StringComparison.OrdinalIgnoreCase))))
{
currentClass.StartBlock.AddImplements((CodeType)implement.Clone());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,11 @@ public async Task InlineParentOnErrorClassesWhichAlreadyInheritAsync()
{
Name = "otherNs",
});
otherModel.StartBlock.AddImplements(new CodeType
{
Name = "IAdditionalDataHolder",
IsExternal = true
});
var declaration = model.StartBlock;
declaration.Inherits = new CodeType
{
Expand All @@ -125,6 +130,7 @@ public async Task InlineParentOnErrorClassesWhichAlreadyInheritAsync()
Assert.Contains(model.Properties, x => x.Name.Equals("OtherProp"));
Assert.Contains(model.Methods, x => x.Name.Equals("otherMethod"));
Assert.Contains(model.Usings, x => x.Name.Equals("otherNs"));
Assert.Contains(model.StartBlock.Implements, x => x.Name.Equals("IAdditionalDataHolder"));
}
[Fact]
public async Task AddsUsingsForErrorTypesForRequestExecutorAsync()
Expand Down
9 changes: 9 additions & 0 deletions tests/Kiota.Builder.Tests/Refiners/GoLanguageRefinerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,11 @@ public async Task InlineParentOnErrorClassesWhichAlreadyInheritAsync()
{
Name = "otherNs",
});
otherModel.StartBlock.AddImplements(new CodeType
{
Name = "IAdditionalDataHolder",
IsExternal = true
});
var declaration = model.StartBlock;
declaration.Inherits = new CodeType
{
Expand All @@ -588,6 +593,10 @@ public async Task InlineParentOnErrorClassesWhichAlreadyInheritAsync()
Assert.Contains(model.Properties, x => x.Name.Equals("otherProp"));
Assert.Contains(model.Methods, x => x.Name.Equals("otherMethod"));
Assert.Contains(model.Usings, x => x.Name.Equals("otherNs"));

var modelInterface = root.FindChildByName<CodeInterface>("somemodelable");
Assert.NotNull(modelInterface);
Assert.Contains(modelInterface.StartBlock.Implements, x => x.Name.Equals("AdditionalDataHolder", StringComparison.OrdinalIgnoreCase));
}
[Fact]
public async Task AddsUsingsForErrorTypesForRequestExecutorAsync()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ public async Task InlineParentOnErrorClassesWhichAlreadyInheritAsync()
{
Name = "otherNs",
});
otherModel.StartBlock.AddImplements(new CodeType
{
Name = "IAdditionalDataHolder",
IsExternal = true
});
var declaration = model.StartBlock;
declaration.Inherits = new CodeType
{
Expand All @@ -95,6 +100,7 @@ public async Task InlineParentOnErrorClassesWhichAlreadyInheritAsync()
Assert.Contains(model.Methods, x => x.Name.Equals("otherMethod"));
Assert.Contains(model.Usings, x => x.Name.Equals("otherNs"));
Assert.Equal("ApiException", model.StartBlock.Inherits.Name);
Assert.Contains(model.StartBlock.Implements, x => x.Name.Equals("AdditionalDataHolder", StringComparison.OrdinalIgnoreCase));
}
[Fact]
public async Task AddsUsingsForErrorTypesForRequestExecutorAsync()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,11 @@ public async Task InlineParentOnErrorClassesWhichAlreadyInheritAsync()
{
Name = "otherNs",
});
otherModel.StartBlock.AddImplements(new CodeType
{
Name = "IAdditionalDataHolder",
IsExternal = true
});
var declaration = model.StartBlock;
declaration.Inherits = new CodeType
{
Expand All @@ -170,6 +175,7 @@ public async Task InlineParentOnErrorClassesWhichAlreadyInheritAsync()
Assert.Contains(model.Properties, x => x.Name.Equals("other_prop"));
Assert.Contains(model.Methods, x => x.Name.Equals("other_method"));
Assert.Contains(model.Usings, x => x.Name.Equals("otherNs"));
Assert.Contains(model.StartBlock.Implements, x => x.Name.Equals("AdditionalDataHolder", StringComparison.OrdinalIgnoreCase));
}
[Fact]
public async Task AddsUsingsForErrorTypesForRequestExecutorAsync()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,16 +177,32 @@ public async Task InlineParentOnErrorClassesWhichAlreadyInheritAsync()
new CodeUsing
{
Name = "otherNs",
},
new CodeUsing
{
Name = "AdditionalDataHolder",
});
otherModel.StartBlock.AddImplements(new CodeType
{
Name = "AdditionalDataHolder",
IsExternal = true
});
model.StartBlock.Inherits = new CodeType
{
TypeDefinition = otherModel
};
await ILanguageRefiner.RefineAsync(new GenerationConfiguration { Language = GenerationLanguage.TypeScript }, root);

Assert.DoesNotContain("otherProp", model.Properties.Select(static x => x.Name), StringComparer.OrdinalIgnoreCase); // we're not inlining since base error for TS is an interface
Assert.DoesNotContain("otherMethod", model.Methods.Select(static x => x.Name), StringComparer.OrdinalIgnoreCase);
Assert.DoesNotContain("otherNs", model.Usings.Select(static x => x.Name), StringComparer.OrdinalIgnoreCase);
var interfaceModel = root.FindChildByName<CodeInterface>("somemodel");
Assert.NotNull(interfaceModel);

Assert.DoesNotContain("otherProp", interfaceModel.Properties.Select(static x => x.Name), StringComparer.OrdinalIgnoreCase); // we're not inlining since base error for TS is an interface
Assert.DoesNotContain("otherMethod", interfaceModel.Methods.Select(static x => x.Name), StringComparer.OrdinalIgnoreCase);
Assert.DoesNotContain("otherNs", interfaceModel.Usings.Select(static x => x.Name), StringComparer.OrdinalIgnoreCase);

var interfaceOtherModel = root.FindChildByName<CodeInterface>("otherModel");
Assert.NotNull(interfaceOtherModel);
Assert.Contains(interfaceOtherModel.StartBlock.Implements, x => x.Name.Equals("AdditionalDataHolder", StringComparison.OrdinalIgnoreCase));
}
[Fact]
public async Task AddsUsingsForErrorTypesForRequestExecutorAsync()
Expand Down

0 comments on commit 5dbc92e

Please sign in to comment.