openai-openapi icon indicating copy to clipboard operation
openai-openapi copied to clipboard

Error in generating openapi client with openapi-generator to typescript-fetch

Open xdotli opened this issue 2 years ago • 13 comments

openapi-generator-cli generate -i src/openapi.yaml -g typescript-fetch -o src/openai-client

Exception in thread "main" org.openapitools.codegen.SpecValidationException: There were issues with the specification. The option can be disabled via validateSpec (Maven/Gradle) or --skip-validate-spec (CLI). | Error count: 5, Warning count: 0 Errors: -attribute components.schemas.CreateFineTuneRequest.default is not of type array -attribute components.schemas.CreateAnswerRequest.default is not of type object -attribute components.schemas.CreateCompletionRequest.default is not of type object -attribute components.schemas.CreateChatCompletionRequest.default is not of type object -attribute components.schemas.CreateClassificationRequest.default is not of type object

    at org.openapitools.codegen.config.CodegenConfigurator.toContext(CodegenConfigurator.java:684)
    at org.openapitools.codegen.config.CodegenConfigurator.toClientOptInput(CodegenConfigurator.java:711)
    at org.openapitools.codegen.cmd.Generate.execute(Generate.java:511)
    at org.openapitools.codegen.cmd.OpenApiGeneratorCommand.run(OpenApiGeneratorCommand.java:32)
    at org.openapitools.codegen.OpenAPIGenerator.main(OpenAPIGenerator.java:66)

xdotli avatar Nov 29 '23 21:11 xdotli

Same Problem can replicate

2catycm avatar Jan 26 '24 03:01 2catycm

My errror log is like this:

# Exception in thread "main" org.openapitools.codegen.SpecValidationException: There were issues with the specification. The option can be disabled via validateSpec (Maven/Gradle) or --skip-validate-spec (CLI).
#  | Error count: 2, Warning count: 10
# Errors:
#         -attribute components.schemas.CreateCompletionRequest.default is not of type `object`
#         -attribute components.schemas.CreateChatCompletionRequest.default is not of type `object`
# Warnings:
#         -attribute components.schemas.CreateCompletionRequest.default is not of type `object`
#         -attribute components.schemas.CreateChatCompletionRequest.default is not of type `object`

#         at org.openapitools.codegen.config.CodegenConfigurator.toContext(CodegenConfigurator.java:701)
#         at org.openapitools.codegen.config.CodegenConfigurator.toClientOptInput(CodegenConfigurator.java:728)
#         at org.openapitools.codegen.cmd.Generate.execute(Generate.java:519)
#         at org.openapitools.codegen.cmd.OpenApiGeneratorCommand.run(OpenApiGeneratorCommand.java:32)
#         at org.openapitools.codegen.OpenAPIGenerator.main(OpenAPIGenerator.java:66)

2catycm avatar Jan 26 '24 03:01 2catycm

But I am confused, it seems in the yaml components.schemas.CreateCompletionRequest we didn't define the default value for this schema I am not familar with OpenAPI format, is the default automatically built upon its sub properties' default value? If yes, then why isn't it an object? Is it a bug to OpenAPI generator?

2catycm avatar Jan 26 '24 03:01 2catycm

I seee this https://stackoverflow.com/questions/58991883/jsonschema-xxx-is-not-of-type-object it seems the error indicated it actually not the problem. It is not because the type is not object, but because the object don't have required field

2catycm avatar Jan 26 '24 03:01 2catycm

After some exploration,I figure out the root cause of this error. It is on "model" definition, the OpenAPI generator doesn't support something like this: image

This definition is bad. It wants it to be an enum to restrict typo error, but it also want to support arbitary model name. That's contradiction. I don't know how js define enum, but afterall it seems the two types are conflicting and not recognized by openapi generator

2catycm avatar Jan 26 '24 07:01 2catycm

Two kinds of solution here,

  1. type:string and examples are the enum
  2. is there a "enum" type for json schema? I am not familar with js
  3. Is anyof generally not supported? Or just breaks for “str or str”

2catycm avatar Jan 26 '24 07:01 2catycm

Two kinds of solution here,

  1. type:string and examples are the enum
  2. is there a "enum" type for json schema? I am not familar with js
  3. Is anyof generally not supported? Or just breaks for “str or str”

For 1., it seems there is only a "default" and "example", no "examples" in OpenAPI Spec or Json Schema, right? I cannot figure out when to use "default" and when to use "example", they are so alike.

2catycm avatar Jan 26 '24 07:01 2catycm

But is should have "examples", how can example be single? enum is restrictions, not extensible? or many "enum" is what I think to be examples and extensible?

2catycm avatar Jan 26 '24 07:01 2catycm

enum

I believe this is the cause because the generated python\java\rust code cannot well define the class "CreateChatCompletionRequestModel" they treat is as an attribute of class "CreateChatCompletionRequest" and it is an empty class with nothing.

and the json deserializer doesn't work for "CreateChatCompletionRequestModel" since it is not well defined as string, but an empty struct.

2catycm avatar Jan 26 '24 07:01 2catycm

I am confused with the document of OpenAPI 3.0 https://swagger.io/specification/

It seems neither "example" nor "default" are valid keyword of the OpenAPI 3.0 schema dialect

2catycm avatar Jan 26 '24 07:01 2catycm

The errors -attribute components.schemas.CreateCompletionRequest.default is not of type object -attribute components.schemas.CreateChatCompletionRequest.default is not of type object

disappear for me, if I comment the default in logit_bias for both objects

logit_bias: type: object x-oaiTypeLabel: map #default: null

judithgull avatar Jan 30 '24 15:01 judithgull

The errors -attribute components.schemas.CreateCompletionRequest.default is not of type object -attribute components.schemas.CreateChatCompletionRequest.default is not of type object

disappear for me, if I comment the default in logit_bias for both objects

logit_bias: type: object x-oaiTypeLabel: map #default: null

Had the same issue, can confirm that this solution works. Thanks for the workaround!

scayze avatar Jan 30 '24 19:01 scayze

x-oaiTypeLabel

So when the type is object and the default is null, it would break? Btw, can anyone explain what is x-oaiTypeLabel to me, it seems there is many x-oai, I searched it but found nothing meaningful.

2catycm avatar Feb 02 '24 12:02 2catycm