OpenAPITools / OpenAPITools/openapi-generator
[BUG] [csharp] [netcore] Default Accept Headers do not respect 2xx vs non-2xx response code differences
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- PR merge metrics
- PR metrics pending
Description
Consider a schema such as:
"/process-definition/key/{key}/rendered-form": {
"get": {
"operationId": "getRenderedStartFormByKey",
"tags": [
"Process Definition"
],
"summary": "Get Rendered Start Form",
"description": "Retrieves the rendered form for the latest version of the process definition which belongs to no tenant.\nThis method can be used to get the HTML rendering of a\n[Generated Task Form](https://docs.camunda.org/manual/latest/user-guide/task-forms/#generated-task-forms).",
"parameters": [
{
"name": "key",
"in": "path",
"schema": {
"type": "string"
},
"required": true,
"description": "The key of the process definition (the latest version thereof) to be retrieved."
}
],
"responses": {
"200": {
"content": {
"application/xhtml+xml": {
"schema": {
"type": "string",
"format": "binary",
"description": "For `application/xhtml+xml` Responses, a byte stream is returned."
},
"examples": {
"example-1": {
"summary": "Status 200 Response",
"description": "A `/process-definition/key/anKey/rendered-form` HTML\n GET response body providing the rendered (generated) form content.",
"value": "\u003cform class\u003d\"form-horizontal\"\u003e\n \u003cdiv class\u003d\"control-group\"\u003e\n \u003clabel class\u003d\"control-label\"\u003eCustomer ID\u003c/label\u003e\n \u003cdiv class\u003d\"controls\"\u003e\n \u003cinput form-field type\u003d\"string\" name\u003d\"customerId\"\u003e\u003c/input\u003e\n \u003c/div\u003e\n \u003c/div\u003e\n \u003cdiv class\u003d\"control-group\"\u003e\n \u003clabel class\u003d\"control-label\"\u003eAmount\u003c/label\u003e\n \u003cdiv class\u003d\"controls\"\u003e\n \u003cinput form-field type\u003d\"number\" name\u003d\"amount\"\u003e\u003c/input\u003e\n \u003c/div\u003e\n \u003c/div\u003e\n \u003c/form\u003e"
}
}
}
},
"description": "Request successful."
},
"400": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/ExceptionDto"
}
}
},
"description": "Process definition has no form field metadata defined. See the\n[Introduction](https://docs.camunda.org/manual/latest/reference/rest/overview/#error-handling)\nfor the error response format."
},
"404": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/ExceptionDto"
}
}
},
"description": "Process definition with given key does not exist. See the\n[Introduction](https://docs.camunda.org/manual/latest/reference/rest/overview/#error-handling)\nfor the error response format."
}
}
}
}
The 200 response content is application/xhtml+xml and the 4xx response types are json.
The Code generator creates something like:
/// <summary>
/// Get Rendered Start Form Retrieves the rendered form for a process definition. This method can be used to get the HTML rendering of a [Generated Task Form](https://docs.camunda.org/manual/latest/user-guide/task-forms/#generated-task-forms).
/// </summary>
/// <exception cref="Camunda.Http.Api.Client.ApiException">Thrown when fails to make API call</exception>
/// <param name="id">The id of the process definition to get the rendered start form for.</param>
/// <param name="cancellationToken">Cancellation Token to cancel the request.</param>
/// <returns>Task of ApiResponse (System.IO.Stream)</returns>
public async System.Threading.Tasks.Task<Camunda.Http.Api.Client.ApiResponse<System.IO.Stream>> GetRenderedStartFormWithHttpInfoAsync(string id, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken))
{
// verify the required parameter 'id' is set
if (id == null)
throw new Camunda.Http.Api.Client.ApiException(400, "Missing required parameter 'id' when calling ProcessDefinitionApi->GetRenderedStartForm");
Camunda.Http.Api.Client.RequestOptions localVarRequestOptions = new Camunda.Http.Api.Client.RequestOptions();
String[] _contentTypes = new String[] {
};
// to determine the Accept header
String[] _accepts = new String[] {
"application/xhtml+xml",
"application/json"
};
var localVarContentType = Camunda.Http.Api.Client.ClientUtils.SelectHeaderContentType(_contentTypes);
if (localVarContentType != null) localVarRequestOptions.HeaderParameters.Add("Content-Type", localVarContentType);
var localVarAccept = Camunda.Http.Api.Client.ClientUtils.SelectHeaderAccept(_accepts);
if (localVarAccept != null) localVarRequestOptions.HeaderParameters.Add("Accept", localVarAccept);
localVarRequestOptions.PathParameters.Add("id", Camunda.Http.Api.Client.ClientUtils.ParameterToString(id)); // path parameter
// make the HTTP request
var localVarResponse = await this.AsynchronousClient.GetAsync<System.IO.Stream>("/process-definition/{id}/rendered-form", localVarRequestOptions, this.Configuration, cancellationToken).ConfigureAwait(false);
if (this.ExceptionFactory != null)
{
Exception _exception = this.ExceptionFactory("GetRenderedStartForm", localVarResponse);
if (_exception != null) throw _exception;
}
return localVarResponse;
}
and the SelectHeaderAccept (ClientUtils.cs) method does:
public static String SelectHeaderAccept(String[] accepts)
{
if (accepts.Length == 0)
return null;
if (accepts.Contains("application/json", StringComparer.OrdinalIgnoreCase))
return "application/json";
return String.Join(",", accepts);
}
As a result the endpoint fails because the Request Accept header is application/json.
Should the generator understand the 2xx codes vs error codes and excepted response typed?
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 generated C# client behavior described in the issue and inspect ClientUtils.cs, especially SelectHeaderAccept, along with the generator path that assembles response media types. Confirm how 2xx and error responses are represented, then verify that generated clients negotiate application/xhtml+xml for success and application/json for errors.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp, openapi
- Domain
- api, tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100