Skip to content

Pass ContentHints format through to generated OpenAPI schema#110

Open
SimonAllmendinger wants to merge 1 commit intooatpp:masterfrom
SimonAllmendinger:feature/content-hints-schema-format
Open

Pass ContentHints format through to generated OpenAPI schema#110
SimonAllmendinger wants to merge 1 commit intooatpp:masterfrom
SimonAllmendinger:feature/content-hints-schema-format

Conversation

@SimonAllmendinger
Copy link
Copy Markdown

Problem

Even if ContentHints carries a format value (see companion PR oatpp/oatpp#…), the generator currently ignores it. The generated OpenAPI 3.0 spec never contains a format field on response or request body schemas, making it impossible to distinguish string from string + format: binary or string + format: byte.

This matters for client code generators: without format: binary, tools like NSwag and OpenAPI Generator produce string return types for endpoints that return raw binary data (e.g. JPEG images), corrupting the payload through text encoding.

Changes

Two small additions in Generator.cpp:

  • generateResponses() — after generateSchemaForType(), the response schema's format is set from hint.second.format if present.
  • generateRequestBody() — same logic for hint.format on consume schemas.

Both additions are guarded by a null check, so existing endpoints without a format hint are completely unaffected.

Companion PR

Depends on oatpp/oatpp#… which adds the format field to ContentHints and the corresponding addResponse/addConsumes overloads.

Result

With both PRs applied, this endpoint annotation:

info->addResponse<String>(Status::CODE_200, "image/jpeg",
                          "Raw JPEG image bytes", "binary");

produces:

"200": {
  "content": {
    "image/jpeg": {
      "schema": { "type": "string", "format": "binary" }
    }
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant