Skip to content

Commit ce6fd8b

Browse files
committed
Default values in model classes: fix parsing for DateTime et al, numeric and boolean defaults were not applied, add mockserver tests
1 parent 6ce5e7d commit ce6fd8b

58 files changed

Lines changed: 2850 additions & 933 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/dependabot.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ updates:
4949
patterns:
5050
- "*kiota*"
5151
- package-ecosystem: composer
52-
directory: "/it/php"
52+
directories:
53+
- "/it/php"
54+
- "/it/php/basic"
5355
schedule:
5456
interval: daily
5557
open-pull-requests-limit: 10

.github/workflows/integration-tests.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ jobs:
9090
- "./tests/Kiota.Builder.IntegrationTests/ToDoApi.yaml"
9191
- "./tests/Kiota.Builder.IntegrationTests/GeneratesUritemplateHints.yaml"
9292
- "./tests/Kiota.Builder.IntegrationTests/DiscriminatorSample.yaml"
93+
- "./tests/Kiota.Builder.IntegrationTests/ModelWithDefaultValues.json"
9394
- "oas::petstore"
9495
- "apisguru::twitter.com:current"
9596
- "apisguru::notion.com"

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,7 @@ reports/
451451

452452
samples/
453453
it/openapi.yaml
454+
it/openapi.json
454455

455456
idempotency-results/
456457
vscode/npm-package/dist/*

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1313

1414
### Changed
1515

16+
- C#, Java, Go, PHP, Dart, TypeScript, Python and Ruby client: default value initialization in model classes for DateTime/Date/Time/UUID properties did not compile [#7404](https://github.com/microsoft/kiota/issues/7404)
17+
- All languages: default value initialization in model classes for numeric/boolean properties was missing [#7404](https://github.com/microsoft/kiota/issues/7404)
1618
- Fixed a potential NullReferenceException in union model discriminator factory methods when a discriminator mapping key is null or empty across C#, Dart, Go, Java, PHP, and Python writers. [#7641](https://github.com/microsoft/kiota/pull/7641)
1719
- Fixed `kiota download` returning exit code 0 (success) when no results are found or multiple ambiguous matches exist. [#7643](https://github.com/microsoft/kiota/pull/7643)
1820
- Fixed incorrect command hints and telemetry in `kiota plugin generate` handler referencing "client" instead of "plugin". [#7642](https://github.com/microsoft/kiota/pull/7642)

it/Readme.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,48 @@ And finally run the test:
1919
```bash
2020
./it/exec-cmd.ps1 -descriptionUrl ${FILE/URL} -language ${LANG}
2121
```
22+
23+
# MockServer tests
24+
25+
The OpenAPI description can be published to a mock server, and you can execute tests that call this API.
26+
27+
To do so, first define a "MockServerITFolder" property in "config.json"
28+
29+
```
30+
"./tests/Kiota.Builder.IntegrationTests/MySampleAPI.yml": {
31+
"MockServerITFolder": "mysample"
32+
},
33+
```
34+
35+
When calling "exec-cmd.ps1" for a specific language, the scripts checks whether this sub folder exists in
36+
the directory corresponding to the language.
37+
If it exists, it executes the tests found in this directory.
38+
39+
The handling depends on the language:
40+
41+
* C#: place the tests in "it\csharp\mysample". Use the test class "basic\KiotaMockServerTests.cs" as a test file template.
42+
The subdir should also contain a project file and maybe "Usings.cs" if your test class relies on global usings.
43+
But you can use the default files: if "exec-cmd.ps1" finds no csproj file in the subdir, it copies "basic\basic.csproj" to
44+
your test subdir and removes it afterwards. The same is done for "Usings.cs"
45+
* Dart: place the tests in "it\dart\mysample\test". Use the test class "it\dart\basic\test\api_client_test.dart" as a test file template.
46+
No additional files are required.
47+
* Go: place the tests in "it\go\mysample". Use the test class "it\go\basic\client_test.go" as a test file template.
48+
The subdir should also contain "go.mod" and "go.sum".
49+
But you can use the default files: if "exec-cmd.ps1" does not find them in the subdir, it copies them from the "basic" dir to
50+
your test subdir and removes them afterwards.
51+
* Java: place the tests in "it\java\mysample\src\test\java". Use the test class "it\java\basic\src\test\java\BasicAPITest.java" as a test file template.
52+
The subdir should also contain "pom.xml".
53+
But you can use the default file: if "exec-cmd.ps1" does not find it in the subdir, it copies it from the "basic" dir to
54+
your test subdir and removes it afterwards.
55+
* PHP: place the tests in "it\php\mysample\tests". Use the test class "it\php\basic\tests\SampleTest.php" as a test file template.
56+
The subdir should also contain "composer.json" and "phpstan.neon".
57+
But you can use the default files: if "exec-cmd.ps1" does not find them in the subdir, it copies them from the "basic" dir to
58+
your test subdir and removes them afterwards.
59+
* Python: place the tests in "it\python\mysample". Use the test class "it\python\basic\test_sample.py" as a test file template.
60+
No additional files are required.
61+
* Ruby: place the tests in "it\ruby\spec\mysample" (difference to other tests!). Use the test class "it\ruby\spec\defaultvalues\integration_test_defaultvalues.rb" as a test file template.
62+
No additional files are required.
63+
* Typescript: not supported.
64+
65+
If you create e.g. a custom "csproj" file for your test (might be necessary if you need additional dependencies), add this file
66+
to the Dependabot config so that dependencies are updated.

it/config.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626
}
2727
]
2828
},
29+
"./tests/Kiota.Builder.IntegrationTests/ModelWithDefaultValues.json": {
30+
"MockServerITFolder": "defaultvalues"
31+
},
2932
"apisguru::github.com:api.github.com": {
3033
"MockServerITFolder": "gh",
3134
"Suppressions": [
@@ -46,6 +49,14 @@
4649
{
4750
"Pattern": "/user/gpg_keys/**",
4851
"Rationale": "https://github.com/github/rest-api-description/issues/2247"
52+
},
53+
{
54+
"Pattern": "/repos/{owner}/{repo}/releases/{release_id}#PATCH",
55+
"Rationale": "https://github.com/microsoft/kiota/pull/7414#issuecomment-4033965839 and https://github.com/github/rest-api-description/issues/6100 (default value for enum is not quoted)"
56+
},
57+
{
58+
"Pattern": "/repos/{owner}/{repo}/releases#POST",
59+
"Rationale": "https://github.com/microsoft/kiota/pull/7414#issuecomment-4033965839 (default value for enum is not quoted, fixed in recent file as of 03/2026 but not updated to APIGurus)"
4960
}
5061
]
5162
},

it/csharp/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
##
44
## Get latest from https://github.com/github/gitignore/blob/main/VisualStudio.gitignore
55

6+
#generated api code if integration test is run locally
7+
client/
8+
69
# User-specific files
710
*.rsuser
811
*.suo
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using App.Client;
2+
using Microsoft.Kiota.Http.HttpClientLibrary;
3+
using Microsoft.Kiota.Abstractions.Authentication;
4+
using App.Client.Models;
5+
6+
namespace Kiota.IT.MockServerTests;
7+
public class KiotaMockServerTests
8+
{
9+
/// <summary>
10+
/// Tests that default values of a model class are applied when creating a new instance.
11+
/// </summary>
12+
[Fact]
13+
public async Task DefaultValuesInModelClassTest()
14+
{
15+
var requestAdapter = new HttpClientRequestAdapter(new AnonymousAuthenticationProvider());
16+
requestAdapter.BaseUrl = "http://localhost:1080";
17+
var client = new ApiClient(requestAdapter);
18+
19+
//Call a sample endpoint - not really needed here.
20+
List<WeatherForecast>? modelList = await client.Api.V1.WeatherForecast.GetAsync(cancellationToken: TestContext.Current.CancellationToken);
21+
Assert.NotNull(modelList);
22+
Assert.Single(modelList);
23+
24+
//Now the real test: create a model class and verify that all properties have the default values.
25+
WeatherForecast model = new WeatherForecast();
26+
Assert.True(model.BoolValue);
27+
28+
Assert.NotNull(model.DateOnlyValue);
29+
Assert.Equal("1900-01-01", model.DateOnlyValue.Value.ToString());
30+
31+
Assert.NotNull(model.DateValue);
32+
Assert.Equal("1900-01-01 00:00", model.DateValue.Value.ToString("yyyy-MM-dd HH:mm"));
33+
34+
Assert.Equal(25.5, model.DecimalValue);
35+
Assert.Equal(25.5, model.DoubleValue);
36+
Assert.Equal(WeatherForecast_enumValue.One, model.EnumValue);
37+
Assert.Equal(25.5f, model.FloatValue);
38+
39+
Assert.NotNull(model.GuidValue);
40+
Assert.Equal("00000000-0000-0000-0000-000000000000", model.GuidValue.Value.ToString());
41+
42+
Assert.Equal(255, model.LongValue);
43+
Assert.Equal("Test", model.Summary);
44+
Assert.Equal(15, model.TemperatureC);
45+
46+
Assert.NotNull(model.TimeValue);
47+
Assert.Equal("00:00:00", model.TimeValue.Value.ToString());
48+
49+
}
50+
}

it/csharp/dotnet.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
<PackageReference Include="Microsoft.kiota.Serialization.Multipart" Version="1.22.1" />
1919
<PackageReference Include="Microsoft.Kiota.Serialization.Text" Version="1.22.1" />
2020

21+
<!-- Exclude code from subprojects, that might be used for MockServer tests -->
2122
<Compile Remove="basic\**" />
23+
<Compile Remove="defaultvalues\**" />
2224
</ItemGroup>
2325

2426
</Project>

it/dart/.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
pubspec.lock
22
.dart_tool/
3-
src/
3+
src/
4+
5+
#generated api code if integration test is run locally
6+
lib/

0 commit comments

Comments
 (0)