Skip to content

[BUG][JAVA/TYPESCRIPT] Invalid consumes headers generated #10598

Open

Description

@whydoievenneedthis
Description

When image/* is specified for requestBody content type, the generated spring server and typescript client (probably others as well) end up invalid.

On spring side:

    @RequestMapping(
        method = RequestMethod.POST,
        value = "/v1",
        consumes = { "image/_*" }
    )

On typescript side:

        // to determine the Content-Type header
        const consumes: string[] = [
            'image/_*'
        ];
openapi-generator version

5.2.1

OpenAPI declaration file content or url
{
  "paths": {
    "/v1": {
      "post": {
        "summary": "uploadImage",
        "operationId": "uploadImage",
        "requestBody": {
          "content": {
            "image/*": {
              "schema": {
                "type": "string",
                "format": "binary"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "OK"
          }
        }
      }
    }
  }
}
Generation Details

Generator names were "spring" and "typescript-angular".

Suggest a fix

Tweak DefaultCodegen.addConsumesInfo.
Issue is with this line:

mediaType.put("mediaType", escapeText(escapeQuotationMark(key)));

The method escapeText is explicitly breaking /* and */.

Activity

  1. berlincho commented on Nov 14, 2021

    @berlincho
    Contributor

    I will try to fix it!

  2. berlincho commented on Nov 24, 2021

    @berlincho
    Contributor

    The cause is escapeUnsafeCharacters in escapeText. Different languages override the function.

    // override with any special text escaping logic
    @Override
    @SuppressWarnings("static-method")
    public String escapeText(String input) {
    if (input == null) {
    return input;
    }
    // remove \t, \n, \r
    // replace \ with \\
    // replace " with \"
    // outer unescape to retain the original multi-byte characters
    // finally escalate characters avoiding code injection
    return escapeUnsafeCharacters(
    StringEscapeUtils.unescapeJava(
    StringEscapeUtils.escapeJava(input)
    .replace("\\/", "/"))
    .replaceAll("[\\t\\n\\r]", " ")
    .replace("\\", "\\\\")
    .replace("\"", "\\\""));
    }

    For example in Java,

    @Override
    public String escapeUnsafeCharacters(String input) {
    return input.replace("*/", "*_/").replace("/*", "/_*");
    }

    In Typescript,

    @Override
    public String escapeUnsafeCharacters(String input) {
    return input.replace("*/", "*_/").replace("/*", "/_*");
    }

  3. berlincho commented on Nov 24, 2021

    @berlincho
    Contributor
  4. whydoievenneedthis commented on Nov 24, 2021

    @whydoievenneedthis
    Author

    Split at '/', escape the fragments only and concatenate it all back together. I am assuming that the escapeUnsafeCharacters methods are trying to somehow prevent block comments from appearing at the wrong place, which should not be a concern for the media types, so this should be safe. It would also allow to drop the custom handling of the "*/*" use case.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions