OpenAPITools / OpenAPITools/openapi-generator

[BUG][C++][Pistache] required field is meaningless in query parameters

Open
#5,880 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Server: C++
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Description

It seems that the required field is broken for query parameters as using it in all variants has no impact on generated code. Pameter type can be anything so I've made it for boolean.

openapi-generator version

I use OpenAPI Generator 4.3.0. I've got no idea if this is regression.

OpenAPI declaration file content or url
openapi: 3.0.0
info:
  description: Some description
  version: 1.0.0
  title: Some title

tags:
  - name: foo

paths:
  "/bar":
    get:
      operationId: barGet
      tags:
        - foo
      summary: Do something
      parameters:
        - in: query
          name: duck
          required: true
          description: property value
          schema:
            type: boolean
      responses:
        200:
          description: Successful operation
          content:
            text/plain:
              schema:
                type: string
servers:
  - url: http://localhost:8080

The problem is that no matter if I make my "duck" parameter mandatory (required: true) or optional (required: false or leave it out), the generated code is exactly the same:

void FooApi::bar_get_handler(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter response) {

    // Getting the query params
    auto duckQuery = request.query().get("duck");
    Pistache::Optional<bool> duck;
    if(!duckQuery.isEmpty()){
        bool valueQuery_instance;
        if(fromStringValue(duckQuery.get(), valueQuery_instance)){
            duck = Pistache::Some(valueQuery_instance);
        }
    }
    // ...

The only difference is in the generated comment in .h file which is:

  • for required: true
/// <param name="duck">property value</param>
  • for required: false or "nothing"
/// <param name="duck">property value (optional, default to false)</param>
Command line used for generation

java -jar openapi-generator-cli.jar generate -i issue.yaml -g cpp-pistache-server -o generated

Steps to reproduce

N/A

Related issues/PRs

I haven't found one.

Suggest a fix/enhancement

I might be wrong here but I suppose it's a bug as I'd expect the generated handler to return some sort of user error code (400? 404?) if the user didn't provide parameter for the required: true field.
The generated code is IMO fine for optional parameters now, but perhaps mandatory fields could be handled like that:

response.send(Pistache::Http::Code::Bad_Request, R"(Field "duck" wasn't provided)"); // "duck" was the first unsatisfied

or something more generic when there are multiple required parameters.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the supplied OpenAPI declaration in issue.yaml and run the shown cpp-pistache-server generation command at version 4.3.0. Compare the generated handler and header for required and optional query parameters, then inspect the generator path responsible for query-parameter handling. Done means required parameters are distinguished from optional ones in generated behavior, with coverage for the reported boolean query case.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.