OpenAPITools / OpenAPITools/openapi-generator
[BUG] [aspnetcore] Object as query parameter not correctly handled
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- PR merge metrics
- PR metrics pending
Description
Bug Report Checklist
- Have you provided a full/minimal spec to reproduce the issue?
- Have you validated the input using an OpenAPI validator (example)?
- Have you tested with the latest master to confirm the issue still exists?
- Have you searched for related issues/PRs?
- What's the actual output vs expected output?
- [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description
Having an object as query parameter the generated csharp-netcore produces a request like this
https://localhost:44301/TestObject?data=class%20TestObject%20%7b%0a%20%20A%3a%201%0a%20%20B%3a%202%0a%7d%0a
Unescaped: https://localhost:44301/TestObject?data=class TestObject { A: 1 B: 2 }
which isn't correctly handled by the aspnetcore server.
The object's properties are always 0.
See Steps to reproduce for more details.
openapi-generator version
Tested with 5.3.0, 5.4.0, 6.0.0-SNAPSHOT
OpenAPI declaration file content or url
openapi: "3.0.0"
info:
version: 0.1.0
title: TestObject API
servers:
- url: http://localhost/
paths:
/TestObject:
get:
operationId: GetTestObject
tags:
- configuration
parameters:
- name: data
in: query
description: Reads a TestObject
required: true
schema:
$ref: "#/components/schemas/TestObject"
responses:
'200':
description: TestObject
content:
text/plain:
schema:
type: boolean
components:
schemas:
TestObject:
type: object
required:
- A
- B
properties:
A:
type: integer
B:
type: integer
Generation Details
java -jar ".\openapi-generator-cli-5.4.0.jar" generate -g aspnetcore -o generated\aspnetcore -i .\api.yml
java -jar ".\openapi-generator-cli-5.4.0.jar" generate -g csharp-netcore -o generated\clientnetcore -i .\api.yml
Steps to reproduce
Generate an aspnetcore server + csharp-netcore client with an object as query parameter.
The minimal csharp-netcore client
ConfigurationApi configurationApi = new ConfigurationApi("https://localhost:44301");
configurationApi.GetTestObject(new TestObject(1, 2));
produces a request like this:
https://localhost:44301/TestObject?data=class%20TestObject%20%7b%0a%20%20A%3a%201%0a%20%20B%3a%202%0a%7d%0a
Unescaped: https://localhost:44301/TestObject?data=class TestObject { A: 1 B: 2 }
which isn't correctly handled by the server, both properties are 0:

The Swagger UI produces this request:
https://localhost:44301/TestObject?A=1&B=2
which also cannot be parsed, both properties are 0.
It is possible to fix the Controller for the Swagger UI by removing
(Name = "data")
public virtual IActionResult GetTestObject([FromQuery][Required()]TestObject data)
This solves the problem for the Swagger UI, but not for the generated client.

What would be the correct URL?
The one from Swagger UI?
https://localhost:44301/TestObject?A=1&B=2
Then it is a client problem.
Related issues/PRs
Maybe related:
https://github.com/OpenAPITools/openapi-generator/issues/10806
https://github.com/OpenAPITools/openapi-generator/issues/10997
Suggest a fix
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the api.yml schema and the generated aspnetcore server and csharp-netcore client produced by the two generation commands. Reproduce the request using ConfigurationApi.GetTestObject and compare its query serialization with the Swagger UI request; done means object properties bind correctly on the server and the generated client sends the supported query form.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp, java, openapi
- Domain
- api, backend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100