OpenAPITools / OpenAPITools/openapi-generator

[BUG] [cpp-httplib-server] Throw exception if optional property not present in json payload

Open
#23,991 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
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?
  • 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?
Description

Many client generators omit optional properties (those not listed in required) when they are unset, which is spec-compliant — the JSON node for these properties is then absent. In this case the deserialization code generated by the cpp-httplib-server generator crashes instead of handling the missing node.

openapi-generator version

v7.23.0

OpenAPI declaration file content or url
openapi: "3.0.0"
info:
  title: Enum Bug Reproduction
  version: "1.0.0"


paths:
  /example:
    post:
      operationId: postExample
      parameters:
        - in: query
          name: status
          schema:
            $ref: '#/components/schemas/PostBody'
      responses:
        '200':
          description: OK

components:
  schemas:
    PostBody:
      type: object
      properties:
        status:
          type: string
        data:
          type: string
      required:
        - status
Generation Details

generation script:

#!/usr/bin/env pwsh
param()

$ErrorActionPreference = 'Stop'

$SpecDir = Resolve-Path (Join-Path $PSScriptRoot 'openapi')
$OutputDir = Resolve-Path (Join-Path $PSScriptRoot 'source')

Write-Output "[generate-models] Spec:   $SpecDir\openapi.yaml"
Write-Output "[generate-models] Output: $OutputDir"

Write-Output "[generate-models] Using Docker..."
docker run --rm `
    -v "${SpecDir}:/spec" `
    -v "${OutputDir}:/output" `
    "openapitools/openapi-generator-cli:latest" generate `
    -i /spec/openapi.yaml `
    -g cpp-httplib-server `
    -o /output `
    --global-property 'models,modelDocs=false,modelTests=false' `
    --additional-properties='enumClassPrefix=true'

if ($LASTEXITCODE -ne 0) {
    throw "[generate-models] ERROR: Generation failed."
}

Write-Output "[generate-models] Done."
Steps to reproduce
    std::string jsonString = "{\"status\":\"accepted\"}";
    nlohmann::json jsonObject = nlohmann::json::parse(jsonString);
    models::PostBody deserializedBody = jsonObject.get<models::PostBody>();
Related issues/PRs
Suggest a fix

Probably instead of NLOHMANN_DEFINE_TYPE_INTRUSIVE it would be necessary to implement explicit friend methods. maybe something like this:

    friend void to_json(nlohmann::json& j, const {{vendorExtensions.modelClassName}}& o)
    {
        {{#vars}}j["{{baseName}}"] = o.{{nameInCamelCase}};
        {{/vars}}
    }

    friend void from_json(const nlohmann::json& j, {{vendorExtensions.modelClassName}}& o)
    {
        {{#requiredVars}}j.at("{{baseName}}").get_to(o.{{nameInCamelCase}});
        {{/requiredVars}}
        {{#optionalVars}}if (j.contains("{{baseName}}") && !j.at("{{baseName}}").is_null())
        {
            j.at("{{baseName}}").get_to(o.{{nameInCamelCase}});
        }
        {{/optionalVars}}
    }

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 modules/openapi-generator/src/main/resources/cpp-httplib-server/model-header.mustache, especially the NLOHMANN_DEFINE_TYPE_INTRUSIVE usage. Generate the provided PostBody model and reproduce deserialization of JSON containing only the required status property. Done means absent optional properties no longer throw while required properties remain enforced.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, json, openapi
Domain
api, backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.