tazatechnology / openapi_spec

Dart based OpenAPI specification generator and parser
BSD 3-Clause "New" or "Revised" License
9 stars 6 forks source link

Support customizing factory constructors names from union types #33

Open davidmigloz opened 11 months ago

davidmigloz commented 11 months ago

I had in my mind a feature request to allow to specify custom factory names, which (together with the already implemented titles) would be the ultimate piece for full customization support.

My idea was to use Specification Extensions to define the factory name in the schema (e.g. using something like x-factoryName):

function_call:
  title: ChatCompletionFunctionCall
  description: Controls how the model calls functions.
  oneOf:
    - type: string
      title: ChatCompletionFunctionCallMode
      x-factoryName: mode
      enum: [none, auto]
    - $ref: "#/components/schemas/ChatCompletionFunctionCallOption"
ChatCompletionFunctionCallOption:
  type: object
  x-factoryName: functionCallOption
  properties:
    name:
      type: string
      description: The name of the function to call.
  required:
    - name

_Originally posted by @davidmigloz in https://github.com/tazatechnology/openapi_spec/issues/32#issuecomment-1791059764_

          > My idea was to use [Specification Extensions](https://swagger.io/specification/#specification-extensions) to define the factory name in the schema (e.g. using something like `x-factoryName`):

Yea this package does not introduce any sort of package specific customization like that. The idea was to just be compatible with any API schema out of the box, without introducing any extra dependency or custom definitions.

@davidmigloz alternatively, we can do it at the code generation level with an onUnionConstructorName function that would allow a user to override the constructor of a specific union. Feel free to open an issue and we can move that chat there.

Thanks again for the fast implementation. lgtm!

Will cut a new release with these changes.

_Originally posted by @walsha2 in https://github.com/tazatechnology/openapi_spec/issues/32#issuecomment-1791177913_

davidmigloz commented 11 months ago

I see your point, it may be better to do it at the code generation level to keep the spec pure and clean. onUnionConstructorName sounds like a good idea, in line with the other functions.

walsha2 commented 11 months ago

Sounds good. I'll get that in, I could actually use such a function myself!