microsoft / kiota

OpenAPI based HTTP Client code generator
https://aka.ms/kiota/docs
MIT License
2.5k stars 180 forks source link

Support Property References #2363

Open JustinGrote opened 1 year ago

JustinGrote commented 1 year ago

Attempting to use the OpenAI specification here: kiota download apisguru::openai.com -o openai.spec.json

Fails with

fail: Kiota.Builder.KiotaBuilder[0]
      OpenAPI error: (null) - The reference string '/components/schemas/CreateCompletionRequest/properties/logit_bias' has invalid format.
fail: Kiota.Builder.KiotaBuilder[0]
      OpenAPI error: (null) - The reference string '/components/schemas/CreateSearchRequest/properties/return_metadata' has invalid format.
fail: Kiota.Builder.KiotaBuilder[0]
      OpenAPI error: (null) - The reference string '/components/schemas/CreateCompletionRequest/properties/user' has invalid format.
fail: Kiota.Builder.KiotaBuilder[0]
      OpenAPI error: (null) - The reference string '/components/schemas/CreateCompletionRequest/properties/user' has invalid format.
fail: Kiota.Builder.KiotaBuilder[0]
      OpenAPI error: (null) - The reference string '/components/schemas/CreateAnswerRequest/properties/expand' has invalid format.
fail: Kiota.Builder.KiotaBuilder[0]
      OpenAPI error: (null) - The reference string '/components/schemas/CreateCompletionRequest/properties/logit_bias' has invalid format.
fail: Kiota.Builder.KiotaBuilder[0]
      OpenAPI error: (null) - The reference string '/components/schemas/CreateAnswerRequest/properties/logprobs' has invalid format.
fail: Kiota.Builder.KiotaBuilder[0]
      OpenAPI error: (null) - The reference string '/components/schemas/CreateCompletionRequest/properties/model' has invalid format.
fail: Kiota.Builder.KiotaBuilder[0]
      OpenAPI error: (null) - The reference string '/components/schemas/CreateSearchRequest/properties/return_metadata' has invalid format.
fail: Kiota.Builder.KiotaBuilder[0]
      OpenAPI error: (null) - The reference string '/components/schemas/CreateAnswerRequest/properties/return_prompt' has invalid format.
fail: Kiota.Builder.KiotaBuilder[0]
      OpenAPI error: (null) - The reference string '/components/schemas/CreateAnswerRequest/properties/search_model' has invalid format.
fail: Kiota.Builder.KiotaBuilder[0]
      OpenAPI error: (null) - The reference string '/components/schemas/CreateCompletionRequest/properties/user' has invalid format.
fail: Kiota.Builder.KiotaBuilder[0]
      OpenAPI error: (null) - The reference string '/components/schemas/CreateCompletionRequest/properties/model' has invalid format.
fail: Kiota.Builder.KiotaBuilder[0]
      OpenAPI error: (null) - The reference string '/components/schemas/CreateCompletionRequest/properties/user' has invalid format.
fail: Kiota.Builder.KiotaBuilder[0]
      OpenAPI error: (null) - The reference string '/components/schemas/CreateImageRequest/properties/n' has invalid format.
fail: Kiota.Builder.KiotaBuilder[0]
      OpenAPI error: (null) - The reference string '/components/schemas/CreateImageRequest/properties/response_format' has invalid format.
fail: Kiota.Builder.KiotaBuilder[0]
      OpenAPI error: (null) - The reference string '/components/schemas/CreateImageRequest/properties/size' has invalid format.
fail: Kiota.Builder.KiotaBuilder[0]
      OpenAPI error: (null) - The reference string '/components/schemas/CreateCompletionRequest/properties/user' has invalid format.
fail: Kiota.Builder.KiotaBuilder[0]
      OpenAPI error: (null) - The reference string '/components/schemas/CreateCompletionRequest/properties/user' has invalid format.
fail: Kiota.Builder.KiotaBuilder[0]
      OpenAPI error: (null) - The reference string '/components/schemas/CreateImageRequest/properties/n' has invalid format.
fail: Kiota.Builder.KiotaBuilder[0]
      OpenAPI error: (null) - The reference string '/components/schemas/CreateImageRequest/properties/response_format' has invalid format.
fail: Kiota.Builder.KiotaBuilder[0]
      OpenAPI error: (null) - The reference string '/components/schemas/CreateImageRequest/properties/size' has invalid format.
fail: Kiota.Builder.KiotaBuilder[0]
      OpenAPI error: (null) - The reference string '/components/schemas/CreateCompletionRequest/properties/user' has invalid format.
fail: Kiota.Builder.KiotaBuilder[0]
      OpenAPI error: (null) - The reference string '/components/schemas/CreateCompletionRequest/properties/user' has invalid format.
crit: Kiota.Builder.KiotaBuilder[0]

I believe this is because it is a reference to a property and maybe kiota doesn't support that yet? Works just fine in other generators.

baywet commented 1 year ago

Hi @JustinGrote. Thanks for your interest in Kiota and for reaching out. We're using OpenAPI.net to parse OpenAPI descriptions and the issue comes from here

"user": {
            "$ref": "#/components/schemas/CreateCompletionRequest/properties/user"
          }

This property defined in one component schema is referencing another component schema property. I'm not sure whether this is a valid reference or not and I'm trying to get clarity with @darrelmiller. Either it's a valid reference and OpenAPI.net doesn't support it. In which case I'll transfer the issue over there to address it. Or it's invalid in which case the description would need to be updated. Spectral doesn't seem to be complaining about it, which would lead me to think it's valid.

baywet commented 1 year ago

Also as a side note, I believe you meant to mention the generate command and not the download one as the download one doesn't do any kind of parsing.

JustinGrote commented 1 year ago

Also as a side note, I believe you meant to mention the generate command and not the download one as the download one doesn't do any kind of parsing.

You are correct, thanks, and nswag generates a client properly including these property references.

JustinGrote commented 1 year ago

Follow up, @darrelmiller do you know where this needs to land? Again nswag handles it just fine, I'm using it here but would be open to trying Kiota if I can get this item fixed because it's fairly pervasive in the spec, and the swagger.io editor doesn't show any errors when opening it so I assume that it is to spec. https://github.com/JustinGrote/PowerShellAssistant/tree/main/src/OpenAI.Client

EDIT: A newer spec has been published here that removes the property references: https://github.com/openai/openai-openapi/blob/master/openapi.yaml

This one also does not generate due to the following error, but that's a separate issue.

crit: Kiota.Builder.KiotaBuilder[0]
      error generating the client: One or more errors occurred. (Error creating property expand for model CreateClassificationRequest in API path \classifications, the schema is invalid.) (Error creating property expand for model CreateAnswerRequest in API path \answers, the schema is invalid.)

Excluding the \classifications and \answers endpoints with --exclude-path seems to allow the client to build fine at that point.

darrelmiller commented 1 year ago

The OpenAPI specification states that $ref can be used to point to components. OpenAPI.net has interpreted that to mean that $ref cannot point to sub-schemas of components.

We still have issues with the oneOf in the completion response of the OpenAI api and the schemas that define an array of empty schemas. I'm thinking of doing a PR to their OpenAPI to fix that.

baywet commented 1 year ago

Thanks for the context @darrelmiller . Should we transfer this to the openapi.net for further discussion and potential resolution?

darrelmiller commented 1 year ago

@baywet We don't have plans to support non-component $refs in OpenAPI.Net. I would like to keep an issue open related to being able to support OpenAI API. The issue that @JustinGrote is experiencing has been resolved in the latest build. However, now we have this issue https://github.com/microsoft/kiota/issues/2462

I will create a new issue that is a meta issue for supporting OpenAI.

sebastienlevert commented 5 months ago

@darrelmiller where are we with this issue? Do we have enhancements that improved this situation?