Skip to content
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

enum generation #37

Open
rssdev10 opened this issue Jul 4, 2023 · 3 comments
Open

enum generation #37

rssdev10 opened this issue Jul 4, 2023 · 3 comments

Comments

@rssdev10
Copy link
Contributor

rssdev10 commented Jul 4, 2023

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"}
      }
@tanmaykm
Copy link
Member

tanmaykm commented Jul 5, 2023

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!

@rssdev10
Copy link
Contributor Author

rssdev10 commented Jul 6, 2023

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

@tanmaykm
Copy link
Member

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.

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

No branches or pull requests

2 participants