Skip to content

Commit

Permalink
Merge pull request #5966 from kiota-community/features/dart
Browse files Browse the repository at this point in the history
Add code generation for Dart
  • Loading branch information
andrueastman authored Jan 9, 2025
2 parents 8b04b37 + fc6cb76 commit 850a9d1
Show file tree
Hide file tree
Showing 33 changed files with 4,553 additions and 4 deletions.
1 change: 1 addition & 0 deletions .github/workflows/idempotency-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ jobs:
- ruby
- php
- python
- dart
description:
- "./tests/Kiota.Builder.IntegrationTests/InheritingErrors.yaml"
- "./tests/Kiota.Builder.IntegrationTests/NoUnderscoresInModel.yaml"
Expand Down
6 changes: 6 additions & 0 deletions .github/workflows/integration-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ jobs:
- ruby
- php
- python
- dart
description:
- "./tests/Kiota.Builder.IntegrationTests/InheritingErrors.yaml"
- "./tests/Kiota.Builder.IntegrationTests/EnumHandling.yaml"
Expand Down Expand Up @@ -109,6 +110,11 @@ jobs:
uses: actions/setup-python@v5
with:
python-version: "3.11"
- name: Setup Dart
if: matrix.language == 'dart'
uses: dart-lang/setup-dart@v1
with:
sdk: "stable"

- name: Check if test is suppressed
id: check-suppressed
Expand Down
23 changes: 22 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,27 @@
"console": "internalConsole",
"stopAtEntry": false
},
{
"name": "Launch Dart",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
"program": "${workspaceFolder}/src/kiota/bin/Debug/net8.0/kiota.dll",
"args": [
"generate",
"--openapi",
"https://raw.githubusercontent.com/microsoftgraph/msgraph-sdk-powershell/dev/openApiDocs/v1.0/Mail.yml",
"--language",
"dart",
"-o",
"${workspaceFolder}/samples/msgraph-mail/java/utilities/src/main/java/graphjavav4/utilities",
"-n",
"graphdart4.utilities"
],
"cwd": "${workspaceFolder}/src/kiota",
"console": "internalConsole",
"stopAtEntry": false
},
{
"name": "Launch CLI (CSharp)",
"type": "coreclr",
Expand Down Expand Up @@ -385,4 +406,4 @@
"processId": "${command:pickProcess}"
}
]
}
}
2 changes: 1 addition & 1 deletion it/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ Generate the code:
And finally run the test:

```bash
./it/exec-cmd.ps1 -language ${LANG}
./it/exec-cmd.ps1 -descriptionUrl ${FILE/URL} -language ${LANG}
```
3 changes: 3 additions & 0 deletions it/dart/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pubspec.lock
.dart_tool/
src/
24 changes: 24 additions & 0 deletions it/dart/basic/test/api_client_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import 'package:microsoft_kiota_abstractions/microsoft_kiota_abstractions.dart';
import 'package:microsoft_kiota_http/microsoft_kiota_http.dart';
import 'package:test/test.dart';
import '../lib/api_client.dart';
import '../lib/models/error.dart';

void main() {
group('apiclient', () {
test('basic endpoint test', () {
final requestAdapter = HttpClientRequestAdapter(
client: KiotaClientFactory.createClient(),
authProvider: AnonymousAuthenticationProvider(),
pNodeFactory: ParseNodeFactoryRegistry.defaultInstance,
sWriterFactory: SerializationWriterFactoryRegistry.defaultInstance,
);
requestAdapter.baseUrl = "http://localhost:1080";
var client = ApiClient(requestAdapter);
expect(
() => client.api.v1.topics.getAsync(),
throwsA(predicate(
(e) => e is Error && e.id == 'my-sample-id' && e.code == 123)));
});
});
}
22 changes: 22 additions & 0 deletions it/dart/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: kiota_dart_generate
description: api generation
version: 0.0.1
publish_to: none

environment:
sdk: ^3.6.0

# Add regular dependencies here.
dependencies:
microsoft_kiota_abstractions: ^0.0.1
microsoft_kiota_http: ^0.0.1
microsoft_kiota_serialization_form: ^0.0.1
microsoft_kiota_serialization_text: ^0.0.1
microsoft_kiota_serialization_json: ^0.0.1
microsoft_kiota_serialization_multipart: ^0.0.1
http: ^1.2.2
uuid: ^4.5.1

dev_dependencies:
lints: ^5.1.1
test: ^1.25.14
23 changes: 23 additions & 0 deletions it/exec-cmd.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,29 @@ elseif ($language -eq "python") {
Pop-Location
}
}
elseif ($language -eq "dart") {
Invoke-Call -ScriptBlock {
dart pub get
dart analyze lib/
} -ErrorAction Stop

if ($mockServerTest) {
Push-Location $itTestPath

$itTestPathSources = Join-Path -Path $testPath -ChildPath "lib"
$itTestPathDest = Join-Path -Path $itTestPath -ChildPath "lib"
if (Test-Path $itTestPathDest) {
Remove-Item $itTestPathDest -Force -Recurse
}
Copy-Item -Path $itTestPathSources -Destination $itTestPathDest -Recurse

Invoke-Call -ScriptBlock {
dart test
} -ErrorAction Stop

Pop-Location
}
}
Pop-Location

if (!([string]::IsNullOrEmpty($mockSeverITFolder))) {
Expand Down
3 changes: 3 additions & 0 deletions it/get-additional-arguments.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ if ($language -eq "csharp") {
elseif ($language -eq "java") {
$command = " --output `"./it/$language/src/apisdk`""
}
elseif ($language -eq "dart") {
$command = " --output `"./it/$language/lib`""
}
elseif ($language -eq "go") {
$command = " --output `"./it/$language/client`" --namespace-name `"integrationtest/client`""
}
Expand Down
3 changes: 2 additions & 1 deletion src/Kiota.Builder/GenerationLanguage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ public enum GenerationLanguage
Go,
Swift,
Ruby,
CLI
CLI,
Dart,
}
20 changes: 20 additions & 0 deletions src/Kiota.Builder/PathSegmenters/DartPathSegmenter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using Kiota.Builder.CodeDOM;
using Kiota.Builder.Extensions;
using Kiota.Builder.Writers.Go;

namespace Kiota.Builder.PathSegmenters;

public class DartPathSegmenter(string rootPath, string clientNamespaceName) : CommonPathSegmenter(rootPath, clientNamespaceName)
{
public override string FileSuffix => ".dart";

public override string NormalizeNamespaceSegment(string segmentName) => segmentName.ToCamelCase();

public override string NormalizeFileName(CodeElement currentElement) => GetLastFileNameSegment(currentElement).ToSnakeCase();

internal string GetRelativeFileName(CodeNamespace @namespace, CodeElement element)
{
return NormalizeFileName(element);
}
}
12 changes: 12 additions & 0 deletions src/Kiota.Builder/Refiners/DartExceptionsReservedNamesProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;

namespace Kiota.Builder.Refiners;
public class DartExceptionsReservedNamesProvider : IReservedNamesProvider
{
private readonly Lazy<HashSet<string>> _reservedNames = new(static () => new(StringComparer.OrdinalIgnoreCase)
{
"toString"
});
public HashSet<string> ReservedNames => _reservedNames.Value;
}
Loading

0 comments on commit 850a9d1

Please sign in to comment.