OpenAPITools / OpenAPITools/openapi-generator

[BUG] [cpp-httplib-server] Primitive and binary response types incorrectly generated as models

Open
#24,854 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?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

The cpp-httplib-server generator incorrectly generates or references model headers for OpenAPI primitive/built-in types.

In particular, responses using primitive string or binary/file schemas can result in generated code containing invalid model includes such as:

#include "model/String.h"

or:

#include "model/File.h"

These models are not defined in the OpenAPI specification and should not be generated.

For example, a response defined as:

content:
  text/plain:
    schema:
      type: string

should be mapped to the corresponding C++ primitive type, such as std::string, rather than a generated String model.

Similarly, a binary response such as:

content:
  application/octet-stream:
    schema:
      type: string
      format: binary

should be mapped to the generator's appropriate binary/file representation rather than resulting in a reference to a non-existent model/File.h.

This makes the generated server code fail to compile because the referenced model headers do not exist.

openapi-generator version

OpenAPI Generator: 7.25.0

Generator:

cpp-httplib-server

OpenAPI version:

3.1.0
OpenAPI declaration file content or url

Minimal reproducible specification:

openapi: 3.1.0

info:
  title: Primitive Response Test
  version: 1.0.0

paths:

  /text:
    get:
      operationId: getText
      responses:
        '200':
          description: Successful response
          content:
            text/plain:
              schema:
                type: string

  /file:
    get:
      operationId: getFile
      responses:
        '200':
          description: Binary response
          content:
            application/octet-stream:
              schema:
                type: string
                format: binary
Generation Details

Generator:

cpp-httplib-server

Example CLI command:

docker run --rm \
    --user "$(id -u):$(id -g)" \
    -v "$SCRIPT_DIR:/local" \
    openapitools/openapi-generator-cli:v7.25.0 \
    generate \
    -i /local/v1.0/vsat_endpoint_v1.json \
    -g cpp-httplib-server \
    -o /local/generated/generated_api_server_cpp_httplib 
Steps to reproduce
  1. Create the openapi.yaml shown above.
  2. Generate the server using:
    --user "$(id -u):$(id -g)" \
    -v "$SCRIPT_DIR:/local" \
    openapitools/openapi-generator-cli:v7.25.0 \
    generate \
    -i /local/v1.0/vsat_endpoint_v1.json \
    -g cpp-httplib-server \
    -o /local/generated/generated_api_server_cpp_httplib 
  1. Inspect the generated C++ source code.
  2. The generated code contains references to model headers for primitive/built-in types, for example:
#include "model/String.h"

and:

#include "model/File.h"
  1. These headers are not present in the generated project.
  2. Compilation therefore fails because the generated code references non-existent model classes.
Actual output vs expected output

Actual:

The generator generates/references model classes for primitive or built-in OpenAPI types.

Examples include:

#include "model/String.h"

and:

#include "model/File.h"

However, the OpenAPI specification does not define String or File as schemas.

As a result, the generated C++ server code contains references to non-existent headers/classes and does not compile.

Expected:

Primitive and built-in OpenAPI types should be mapped to their corresponding C++ representations without generating model classes.

For example:

schema:
  type: string

should resolve to something equivalent to:

std::string

and:

schema:
  type: string
  format: binary

should resolve to the generator's appropriate binary/file representation without creating or referencing:

model/File.h

No String.h or File.h model should be generated or referenced unless the user explicitly defines corresponding schemas in the OpenAPI document.

Related issues/PRs

I have not yet identified a related issue or PR specifically covering primitive string and binary/file response schemas in the cpp-httplib-server generator.

Suggest a fix

The generator should correctly distinguish between:

  1. Primitive OpenAPI types (string, integer, number, boolean, etc.)
  2. OpenAPI formatted primitive types such as string/binary
  3. User-defined schemas/models

Primitive types should be resolved directly to their corresponding C++ types rather than being passed through the model generation mechanism.

In particular:

type: string

should resolve to the generator's C++ string type, and:

type: string
format: binary

should resolve to the appropriate binary representation.

The response type resolution and model/include generation logic should avoid producing model references such as:

#include "model/String.h"
#include "model/File.h"

when the corresponding types originate from primitive OpenAPI schemas rather than user-defined models.

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 by reproducing the issue with the minimal OpenAPI 3.1.0 specification and the cpp-httplib-server generator command described in the report. Trace the response type resolution and model/include generation logic for primitive string and binary schemas. Done means generated server code uses appropriate C++ representations and no longer references nonexistent String.h or File.h models.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
api, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.