zio / zio-http

A next-generation Scala framework for building scalable, correct, and efficient HTTP clients and servers
https://zio.dev/zio-http
Apache License 2.0
802 stars 405 forks source link

[gen] Endpoint path modification in EndpointGen when having - in the path. #3217

Open wi101 opened 3 days ago

wi101 commented 3 days ago

Describe the bug

When generating Scala endpoints from an OpenAPI JSON file using EndpointGen, any path segments containing - are automatically replaced with _. This behavior alters the expected endpoint paths and may lead to discrepancies between the OpenAPI specification and the generated code.

To Reproduce Steps to reproduce the behaviour:

  1. Define an example api.json with an endpoint that contains -
    {
    "paths": {
    "/example-path": {
      "get": {
        "summary": "Example endpoint",
        ...
      }
    }
    }
    }
  2. Generate scala code and the endpoints from api.json file.
    for {
      json                        <- readJsonFileAsString("/api.json")
      openApi                     <- ZIO.fromEither(OpenAPI.fromJson(json))
      endpointsAndComponentsFiles <- ZIO.attempt(EndpointGen.fromOpenAPI(openApi))
      directory                   <- ZIO.attempt(Files.createDirectories(Paths.get("src")))
      _                           <- ZIO
                                       .attempt(
                                         CodeGen.writeFiles(
                                           endpointsAndComponentsFiles,
                                           java.nio.file.Paths.get(directory.toString, "main", "scala", "codegen"),
                                           "codegen",
                                          None
                                         )
                                       )
    } yield ()
  3. Observe that the generated endpoint path becomes /example_path.
    val post = Endpoint(Method.POST / "example_path")
    .in[Types]
    .out[Unit](status = Status.Ok)

Expected behaviour The generated endpoint should retain the original - character in the path as defined in the OpenAPI JSON file

Used version zio-http version: 3.0.1