microsoft / microsoft/kiota

Optional Swagger 2.0 request body is generated as required

Open
#8,160 0 comments 0 reactions 0 assignees View on GitHub
Python status:waiting-for-triage 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

Python

### Describe the bug

Kiota generates a required body parameter for a Swagger 2.0 in: body parameter even when required: false.
This occurs with a minimal Swagger 2.0 specification and appears to affect primitive request bodies.

For example, the operation with required: false generates:
```python
async def post(
self,
body: str,
request_configuration: Optional[...] = None
) -> Optional[bytes]:
```

and also rejects None:
```python
if body is None:
raise TypeError("body cannot be null.")
```

### Expected behavior

An OpenAPI request body with required: false should be treated as optional by the generated client.

### How to reproduce

Generate a Python client using the OpenAPI description below and compare the generated OptionalRequestBuilder and RequiredRequestBuilder.

### Open API description file

I originally encountered the issue with this description https://github.com/WebFuzzing/Dataset/blob/master/openapi-swagger/catwatch.json

I was also able to reproduce it with the following minimal Swagger 2.0 description:

```yaml
swagger: "2.0"

info:
title: Kiota Optional Body Repro
version: "1.0.0"

paths:
/optional:
post:
operationId: postOptional
consumes:
- application/json
produces:
- application/json
parameters:
- name: value
in: body
required: false
schema:
type: string
responses:
"200":
description: OK

/required:
post:
operationId: postRequired
consumes:
- application/json
produces:
- application/json
parameters:
- name: value
in: body
required: true
schema:
type: string
responses:
"200":
description: OK
```

### Kiota Version

1.35.0

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

_No response_

### Known Workarounds

_No response_

### Configuration

Fedora 43

### Debug output

This is for the minimal spec, path files are redacted
Click to expand log
```

info: Kiota.Builder.KiotaBuilder[659500454]
Cleaning output directory x
dbug: Kiota.Builder.KiotaBuilder[1405590561]
kiota version 1.35.0
info: Kiota.Builder.KiotaBuilder[1423726202]
loaded description from local source
dbug: Kiota.Builder.KiotaBuilder[1605292753]
step 1 - reading the stream - took 00:00:00.0121812
dbug: Kiota.Builder.KiotaBuilder[1605292753]
step 2 - parsing the document - took 00:00:00.0912558
dbug: Kiota.Builder.KiotaBuilder[1605292753]
step 3 - updating generation configuration from kiota extension - took 00:00:00.0001371
dbug: Kiota.Builder.KiotaBuilder[1605292753]
step 4 - filtering API paths with patterns - took 00:00:00.0026993
info: Kiota.Builder.KiotaBuilder[543457275]
Client root URL set to x
dbug: Kiota.Builder.KiotaBuilder[1605292753]
step 5 - checking whether the output should be updated - took 00:00:00.0246861
dbug: Kiota.Builder.KiotaBuilder[1605292753]
step 6 - create uri space - took 00:00:00.0030403
dbug: Kiota.Builder.KiotaBuilder[1605292753]
InitializeInheritanceIndex 00:00:00.0037432
dbug: Kiota.Builder.KiotaBuilder[1605292753]
CreateRequestBuilderClass 00:00:00.0457715
dbug: Kiota.Builder.KiotaBuilder[1605292753]
CreateWebhookModels 00:00:00.0004489
dbug: Kiota.Builder.KiotaBuilder[1605292753]
MapTypeDefinitions 00:00:00.0083213
dbug: Kiota.Builder.KiotaBuilder[1605292753]
TrimInheritedModels 00:00:00.0130952
dbug: Kiota.Builder.KiotaBuilder[1605292753]
CleanUpInternalState 00:00:00.0003503
dbug: Kiota.Builder.KiotaBuilder[1605292753]
step 7 - create source model - took 00:00:00.0722191
dbug: Kiota.Builder.KiotaBuilder[1306343761]
35ms: Language refinement applied
dbug: Kiota.Builder.KiotaBuilder[1605292753]
step 8 - refine by language - took 00:00:00.0388107
dbug: Kiota.Builder.KiotaBuilder[1605292753]
step 9 - writing files - took 00:00:00.0312407
info: Kiota.Builder.KiotaBuilder[1423726202]
loaded description from local source
dbug: Kiota.Builder.KiotaBuilder[1605292753]
step 10 - writing lock file - took 00:00:00.0238216
Generation completed successfully

```

### Other information

_No response_

Contributor guide

Open the contributing guide

Research direction

Start by running the Nuget tool against the minimal Swagger 2.0 description and compare the generated OptionalRequestBuilder and RequiredRequestBuilder. Confirm that the optional primitive body accepts None without raising while the required body remains required.

Written by the indexing model from the issue text.

Assessment

Tech stack
openapi, python
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.