JuliaComputing/OpenAPI.jl

enum generation

Opened this issue · 3 comments

Probably wrong generation of the enum type.

    DocumentMetadata:
      type: object
      properties:
        source:
          $ref: "#/components/schemas/Source"
    Source:
      title: Source
      enum:
        - email
        - file
        - chat
      type: string
      description: An enumeration.

Expectation is to have

      "metadata": {
       "source": "file"
     }

and an equivalent code in Python

class Source(str, Enum):
    email = "email"
    file = "file"
    chat = "chat"

But in fact we have:

Base.@kwdef mutable struct Source <: OpenAPI.APIModel

    function Source()
        return new()
    end
end # type Source

const _property_types_Source = Dict{Symbol,String}()
OpenAPI.property_type(::Type{ Source }, name::Symbol) = Union{Nothing,eval(Base.Meta.parse(_property_types_Source[name]))}

function check_required(o::Source)
    true
end

function OpenAPI.validate_property(::Type{ Source }, name::Symbol, val)
end

And de-serialization is working for the code only:

      "metadata": {
        "source": {"file":"file"}
      }

Thanks for reporting.

From what I see, model fields that are enums (e.g. here and here are generated correctly like here and here. However for enums in API parameters like here and here the required validations are not generated.

I have added an entry in the TODO section for enum improvements. PRs are welcome!

As additional information, we are using extended specification from here https://github.com/openai/chatgpt-retrieval-plugin/blob/main/.well-known/openapi.yaml#L41C1-L41C6

As for now, we replaced $ref: "#/components/schemas/Source" by a simple string type

Hi @rssdev10, I gave the specs a try with the latest version of openapi-generator, and I see the Source model generated as:

if !isdefined(@__MODULE__, :Source)
    const Source = String
else
    @warn("Skipping redefinition of Source to String")
end

While that's not complete (lacks validations for the enum values), but it is not incorrect as it seems to have been in some earlier version of the generator. Maybe updating the openapi-generator version would help in this case. Serialization and deserializations do not throw errors.

julia> using OpenAPI, JSON

julia> include("src/OpenAPIRetrievalPluginClient.jl")
Main.OpenAPIRetrievalPluginClient

julia> meta = OpenAPIRetrievalPluginClient.DocumentChunkMetadata(; source="file");

julia> # serialization

julia> meta
{
  "source": "file"
}

julia> # de-serialization

julia> meta2 = OpenAPI.from_json(OpenAPIRetrievalPluginClient.DocumentChunkMetadata, JSON.parse("{\"source\":\"chat\"}"))
{
  "source": "chat"
}

I would still keep this issue open though, to track the issue that validations for enums are not generated properly.