Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
kuzkry opened this issue Apr 9, 2020 · 1 comment
Open

Comments

@kuzkry
Copy link

kuzkry commented Apr 9, 2020

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.

@auto-labeler
Copy link

auto-labeler bot commented Apr 9, 2020

👍 Thanks for opening this issue!
🏷 I have applied any labels matching special text in your issue.

The team will review the labels and make any necessary changes.

@kuzkry kuzkry changed the title [BUG][C++] required is meaningless in query parameters [BUG][C++][Pistache] required field is meaningless in query parameters Apr 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant