ogen icon indicating copy to clipboard operation
ogen copied to clipboard

feat(gen): Generate single type for a parameter definition

Open urandom opened this issue 1 year ago • 2 comments

Description

Given the following definition:

components:
  securitySchemes:
  parameters:
    PathParam:
      in: path
      name: path-param
      required: true
      schema:
        type: string
        enum: ["val1", "val2"]

The generator creates a separate type for this definition whenever this parameter is used. If two operations (oper1, oper2) use this parameter, there will be two generated types (Oper1PathParam, Oper2PathParam). This makes it very difficult to handle such parameters in a common middleware, since they cannot be converted to a string, as that is just an underlying type.

urandom avatar Feb 09 '24 14:02 urandom

Having a dedicated schema definition that is referenced there is a workaround for this problem. I'm just sure why a separate definition is required in this case.

urandom avatar Feb 09 '24 14:02 urandom

This would significantly reduce the size of my generated source files and improve debuggability since I reuse a lot of my parameters over many endpoints.

Although I'm not 100% sure how much inlining the Go compiler does, it may even noticeably reduce the binary size which is something I wanted to explore contributing to ogen when I have time. Parameter reuse was my first idea when looking into the generated code.

ayuhito avatar Apr 27 '24 17:04 ayuhito