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 missing imports in java generation for UntypedNode #4776

Merged
merged 1 commit into from
Jun 4, 2024
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fixes a bug where warnings about discriminator not being inherited were generated [#4761](https://github.com/microsoft/kiota/issues/4761)
- Fix scalar member composed type serialization in PHP [#2827](https://github.com/microsoft/kiota/issues/2827)
- Trims unused components from output openApi document when generating plugins [#4672](https://github.com/microsoft/kiota/issues/4672)
- Fixes missing imports for UntypedNode when backingstore is enabled in Java.

## [1.14.0] - 2024-05-02

Expand Down
2 changes: 1 addition & 1 deletion src/Kiota.Builder/Refiners/JavaRefiner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@
private static void SetSetterParametersToNullable(CodeElement currentElement, params Tuple<CodeMethodKind, CodePropertyKind>[] accessorPairs)
{
if (currentElement is CodeMethod method &&
accessorPairs.Any(x => method.IsOfKind(x.Item1) && (method.AccessedProperty?.IsOfKind(x.Item2) ?? false)))

Check warning on line 204 in src/Kiota.Builder/Refiners/JavaRefiner.cs

View workflow job for this annotation

GitHub Actions / Build

Collection-specific "Exists" method should be used instead of the "Any" extension. (https://rules.sonarsource.com/csharp/RSPEC-6605)

Check warning on line 204 in src/Kiota.Builder/Refiners/JavaRefiner.cs

View workflow job for this annotation

GitHub Actions / Build

Collection-specific "Exists" method should be used instead of the "Any" extension. (https://rules.sonarsource.com/csharp/RSPEC-6605)
foreach (var param in method.Parameters)
param.Type.IsNullable = true;
CrawlTree(currentElement, element => SetSetterParametersToNullable(element, accessorPairs));
Expand All @@ -214,7 +214,7 @@
var nUsing = new CodeUsing
{
Name = "EnumSet",
Declaration = new CodeType { Name = "java.util", IsExternal = true },

Check warning on line 217 in src/Kiota.Builder/Refiners/JavaRefiner.cs

View workflow job for this annotation

GitHub Actions / Build

Define a constant instead of using this literal 'java.util' 6 times. (https://rules.sonarsource.com/csharp/RSPEC-1192)

Check warning on line 217 in src/Kiota.Builder/Refiners/JavaRefiner.cs

View workflow job for this annotation

GitHub Actions / Build

Define a constant instead of using this literal 'java.util' 6 times. (https://rules.sonarsource.com/csharp/RSPEC-1192)
};
currentClass.AddUsing(nUsing);
}
Expand All @@ -224,7 +224,7 @@
private static readonly JavaConventionService conventionService = new();
private const string AbstractionsNamespaceName = "com.microsoft.kiota";
private const string SerializationNamespaceName = "com.microsoft.kiota.serialization";
private static readonly AdditionalUsingEvaluator[] defaultUsingEvaluators = {

Check warning on line 227 in src/Kiota.Builder/Refiners/JavaRefiner.cs

View workflow job for this annotation

GitHub Actions / Build

Refactor this field to reduce its Cognitive Complexity from 27 to the 15 allowed. (https://rules.sonarsource.com/csharp/RSPEC-3776)

Check warning on line 227 in src/Kiota.Builder/Refiners/JavaRefiner.cs

View workflow job for this annotation

GitHub Actions / Build

Refactor this field to reduce its Cognitive Complexity from 27 to the 15 allowed. (https://rules.sonarsource.com/csharp/RSPEC-3776)
new (static x => x is CodeProperty prop && prop.IsOfKind(CodePropertyKind.RequestAdapter),
AbstractionsNamespaceName, "RequestAdapter"),
new (static x => x is CodeProperty prop && prop.IsOfKind(CodePropertyKind.PathParameters) || x is CodeMethod method && method.IsOfKind(CodeMethodKind.QueryParametersMapper),
Expand Down Expand Up @@ -264,7 +264,7 @@
AbstractionsNamespaceName, "QueryParameters"),
new (static x => x is CodeClass @class && @class.OriginalComposedType is CodeIntersectionType intersectionType && intersectionType.Types.Any(static y => !y.IsExternal),
SerializationNamespaceName, "ParseNodeHelper"),
new (static x => x is CodeProperty prop && prop.IsOfKind(CodePropertyKind.Custom) && prop.Type.Name.Equals(KiotaBuilder.UntypedNodeName, StringComparison.OrdinalIgnoreCase),
new (static x => (x is CodeMethod @method && @method.IsOfKind(CodeMethodKind.Getter, CodeMethodKind.Setter) && @method.AccessedProperty != null && (@method.AccessedProperty.IsOfKind(CodePropertyKind.Custom) && @method.AccessedProperty.Type.Name.Equals(KiotaBuilder.UntypedNodeName, StringComparison.OrdinalIgnoreCase) )) ,
SerializationNamespaceName, KiotaBuilder.UntypedNodeName),
new (static x => x is CodeMethod method && method.IsOfKind(CodeMethodKind.RequestExecutor, CodeMethodKind.RequestGenerator) && method.Parameters.Any(static y => y.IsOfKind(CodeParameterKind.RequestBody) && y.Type.Name.Equals(MultipartBodyClassName, StringComparison.OrdinalIgnoreCase)),
AbstractionsNamespaceName, MultipartBodyClassName)
Expand Down
10 changes: 6 additions & 4 deletions tests/Kiota.Builder.Tests/Refiners/JavaLanguageRefinerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -703,8 +703,10 @@ public async Task SplitsLongRefiners()
Assert.Equal("String", model.Methods.First(static x => x.IsOverload).Parameters.First().Type.Name);
}

[Fact]
public async Task AddsUsingForUntypedNode()
[Theory]
[InlineData(true)]
[InlineData(false)]
public async Task AddsUsingForUntypedNode(bool usesBackingStore)
{
var model = root.AddClass(new CodeClass
{
Expand All @@ -720,11 +722,11 @@ public async Task AddsUsingForUntypedNode()
IsExternal = true
},
}).First();
await ILanguageRefiner.Refine(new GenerationConfiguration { Language = GenerationLanguage.Java }, root);
await ILanguageRefiner.Refine(new GenerationConfiguration { Language = GenerationLanguage.Java, UsesBackingStore = usesBackingStore }, root);
Assert.Equal(KiotaBuilder.UntypedNodeName, property.Type.Name);
Assert.NotEmpty(model.StartBlock.Usings);
var nodeUsing = model.StartBlock.Usings.Where(static declaredUsing => declaredUsing.Name.Equals(KiotaBuilder.UntypedNodeName, StringComparison.OrdinalIgnoreCase)).ToArray();
Assert.Single(nodeUsing);
Assert.Equal(2, nodeUsing.Length); // one for the getter and another for setter. Writer will unionise
Assert.Equal("com.microsoft.kiota.serialization", nodeUsing[0].Declaration.Name);
}
#endregion
Expand Down
Loading