Skip to content

[scala][http4s] fix escaping of reserved words for correct model dese… #21518

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

donilg
Copy link
Contributor

@donilg donilg commented Jul 3, 2025

There is an example of correct openAPI specification:

{
  "openapi": "3.0.0",
  "info": {
    "title": "Simple API",
    "description": "A simple API with GET and POST endpoints",
    "version": "1.0.0"
  },
  "servers": [
    {
      "url": "https://api.example.com/v1"
    }
  ],
  "paths": {
    "/users": {
      "get": {
        "summary": "Method that has models with a reserved word",
        "parameters": [
          {
            "name": "typeName",
            "in": "query",
            "description": "param",
            "required": true,
            "schema": {
              "type": "string",
              "enum": [
                "active",
                "inactive",
                "pending"
              ]
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Successful response",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/User"
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "User": {
        "type": "object",
        "properties": {
          "type": {
            "type": "string",
            "enum": [
              "active",
              "inactive",
              "pending"
            ]
          }
        },
        "required": [
          "type"
        ]
      }
    }
  }
}

JSON response is generated for this OpenAPI specification:

{"_type":"active"}

This is incorrect since we specified that the field should be named type, not _type.
I changed the escaping of reserved words to standard Scala backticks notation so that the JSON decoder could properly map the domain to JSON.

{"type":"active"}

PR checklist

  • Read the contribution guidelines.
  • Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
  • Run the following to build the project and update samples:
    ./mvnw clean package || exit
    ./bin/generate-samples.sh ./bin/configs/*.yaml || exit
    ./bin/utils/export_docs_generators.sh || exit
    
  • File the PR against the correct branch: master (upcoming 7.x.0 minor release - breaking changes with fallbacks), 8.0.x (breaking changes without fallbacks)
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

Hello! 👋
I'd love to get your eyes on this PR when you have a moment.
Thanks in advance!

@clasnake (2017/07), @shijinkui (2018/01), @ramzimaalej (2018/03), @chameleon82 (2020/03), @Bouillie (2020/04) @Fish86 (2023/06)

@donilg donilg force-pushed the scala-http4s-reserve-word-model branch from d3de2ae to de9ff5a Compare July 4, 2025 04:12
@wing328
Copy link
Member

wing328 commented Jul 6, 2025

thanks for the pr

can you please review the CI build failure when you've time?

@donilg donilg force-pushed the scala-http4s-reserve-word-model branch 2 times, most recently from 7153a80 to 738f4d0 Compare July 7, 2025 12:09
}
};

return compiler.withEscaper(SCALA);
Copy link
Member

@wing328 wing328 Jul 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I created

    private static class BacktickLambda extends AbstractScalaCodegen.CustomLambda {
        @Override
        public String formatFragment(String fragment) {
            if (fragment.startsWith("`") && fragment.endsWith("`")) {
                String unescaped = fragment.substring(1, fragment.length() - 1);
                return "`" + Escapers.HTML.escape(unescaped) + "`";
            }
            return Escapers.HTML.escape(fragment);
        }
    }

added it to the properties

additionalProperties.put("fnBacktick", new BacktickLambda());

After that, I used it in the templates

{{#fnBacktick}}{{paramName}}{{/fnBacktick}}

and I ended up with non-compiling code

case class User(
  `type`: String
)

Copy link
Contributor Author

@donilg donilg Jul 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RE:

{{#fnBacktick}}{{paramName}}{{/fnBacktick}}

what about using the following instead to use the original value instead of HTML-escaped value?

{{#fnBacktick}}{{{paramName}}}{{/fnBacktick}}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@donilg donilg force-pushed the scala-http4s-reserve-word-model branch from 738f4d0 to e60c51a Compare July 10, 2025 11:29
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.

2 participants