microsoft / microsoft/kiota

Random error during client generation: Sequence contains no matching element

Open
#6,854 0 comments 0 reactions 0 assignees View on GitHub
Csharp type:bug
Dominant language
C#
Stars
3.8k
Forks
333
Avg merge
16h 29m
Merged PRs (30d)
116

Description

### What are you generating using Kiota, clients or plugins?

API Client/SDK

### In what context or format are you using Kiota?

Nuget tool

### Client library/SDK language

Csharp

### Describe the bug

We are generating a C# client for our API in a GitHub pipeline. We are randomly encountering a strange issue with the generation failing.

Console log:

```
kiota generate --log-level information --openapi "swagger.json" --output "Generated" --language CSharp --class-name OurAPIClient --namespace-name "XXX.RestAPI.Client" --additional-data false
info: Kiota.Builder.KiotaBuilder[0]
loaded description from local source
warn: Kiota.Builder.KiotaBuilder[0]
OpenAPI warning: #/ - A servers entry (v3) or host + basePath + schemes properties (v2) was not present in the OpenAPI description. The root URL will need to be set manually with the request adapter.
warn: Kiota.Builder.KiotaBuilder[0]
No server url found in the OpenAPI document. The base url will need to be set when using the client.
crit: Kiota.Builder.KiotaBuilder[0]
error generating the client: One or more errors occurred. (One or more errors occurred. (One or more errors occurred. (One or more errors occurred. (One or more errors occurred. (One or more errors occurred. (Sequence contains no matching element))))))
```

If an error occurs, the Kiota log is not generated, so it is difficult to tell which part of the code is failing.

We tried generating the client locally and debugging Kiota, and only in one case out of at least 50 did we manage to catch the same exception.

Our API have two endpoints which return the type that have the suffix `GetResponse` like `SomethingGetResponse`.

When the `GetExecutorMethodReturnType` method https://github.com/microsoft/kiota/blob/main/src/Kiota.Builder/KiotaBuilder.cs#L1301 is called, an attempt to read the `Discriminator` method is made,

```
var originalFactoryMethod = codeClass.Methods.First(static x => x.Kind is CodeMethodKind.Factory);
```
https://github.com/microsoft/kiota/blob/main/src/Kiota.Builder/KiotaBuilder.cs#L1320

but it fails with the `InvalidOperationException` exception and the error message

> Sequence contains no matching element

We found out that https://github.com/microsoft/kiota/blob/main/src/Kiota.Builder/KiotaBuilder.cs#L1320 is called twice for the `SomethingGetResponse` type.

Our guess is that the error is related to the fact that the `GetExecutorMethodReturnType` method is called from `Parallel.Foreach` and all metadata about the model type is not yet available.

https://github.com/microsoft/kiota/blob/main/src/Kiota.Builder/KiotaBuilder.cs#L765

The parallel foreach explains why the error occurs randomly.

### Expected behavior

No random error
Kiota log is always generated

### How to reproduce

The error occurs randomly, but try to
1. Define two endpoints which returns the type named like `SomethingGetResponse`
2. Run Kiota generator for C#

### Open API description file

I can provide the Open API description, because it's not public API.

### Kiota Version

1.28.0

### Latest Kiota version known to work for scenario above?(Not required)

_No response_

### Known Workarounds

Disable backward compatibility

```
kiota generate --log-level information --openapi "swagger.json" --output "Generated" --language CSharp --class-name OurAPIClient --namespace-name "XXX.RestAPI.Client" --additional-data false --exclude-backward-compatible true
```

### Configuration

- OS: Linux, Windows 11
- architecture: x64

### Debug output

Click to expand log
```

```

### Other information

It's quite annoying, because it affects our developer and devops experience.

Contributor guide

Open the contributing guide

Research direction

Start in src/Kiota.Builder/KiotaBuilder.cs at GetExecutorMethodReturnType around line 1301, the factory lookup around line 1320, and its Parallel.Foreach caller around line 765. Reproduce with two endpoints returning the same SomethingGetResponse type, compare normal and --exclude-backward-compatible true runs, and verify generation no longer fails randomly and logs are produced.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, openapi
Domain
api, devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.