OpenAPITools / OpenAPITools/openapi-generator

Enum value names with spaces are legal in JSON/YAML but illegal in C

Open
#12,263 2 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

Spec:

openapi: "3.0.0"
info:
  description: "test"
  version: "1.0.0"
  title: "myTest"
  contact:
    email: "my@mail.com"
servers:
- url: "http://localhost:9998/v1"
tags:
- name: "users"
paths: {}
components:
  schemas:
    UserDetail:
      type: "object"
      description: "Detail of a User"
      properties:
        name:
          type: "string"
          description: "the name of the user"
        type:
          description: "the type of the user"
          type: "string"
          enum:
          - zero
          - 'one potato'
          - 'two potato'

What's the actual output vs expected output?

Expected output is code that compiles in the target language (e.g. C). Actual output has syntax errors.

$ java -jar ../modules/openapi-generator-cli/target/openapi-generator-cli.jar generate -g c -i enum_with_spaces.yaml
$ cat model/user_detail.h
...
// Enum TYPE for user_detail

// Enum TYPE for user_detail

typedef enum  { mytest_user_detail_TYPE_NULL = 0, mytest_user_detail_TYPE_zero, mytest_user_detail_TYPE_one potato, mytest_user_detail_TYPE_two potato } mytest_user_detail_TYPE_e;
...

The whistespace in the YAML enum value is transferred directly to the C code which then doesn't compile. The problem is this method in DefaultCodegen.java:

    @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("\"", "\\\""));
    }

Seems like you could fix it by simply replacing spaces with underscores (for instance), but that might depend on something I don't know about. You would want the JSON sent to the server by a client to be the same as before.

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 tracing DefaultCodegen.escapeText and the C generator path that produces model/user_detail.h. Compare how enum values are transformed for C with the original OpenAPI values, and inspect any nearby generator tests. Done should produce compilable C identifiers while preserving the original enum strings in client-server JSON.

Written by the indexing model from the issue text.

Assessment

Tech stack
c, java, openapi
Domain
api, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.