Skip to content

nil pointer deference when converting OpenAI OpenAPI 3.0 spec to Swagger #1062

@w0rp

Description

@w0rp

I get a nil pointer dereference error when converting the OpenAI OpenAPI 3.0 spec to Swagger, which is available here.

Here is a stack frame I took from delve.

 4  0x00000000008b289a in github.com/getkin/kin-openapi/openapi2conv.FromV3SchemaRef
    at /home/w0rp/go/pkg/mod/github.com/getkin/kin-openapi@v0.131.0/openapi2conv/openapi2_conv.go:839
 5  0x00000000008b529d in github.com/getkin/kin-openapi/openapi2conv.FromV3RequestBodyFormData
    at /home/w0rp/go/pkg/mod/github.com/getkin/kin-openapi@v0.131.0/openapi2conv/openapi2_conv.go:1028
 6  0x00000000008b2227 in github.com/getkin/kin-openapi/openapi2conv.fromV3RequestBodies
    at /home/w0rp/go/pkg/mod/github.com/getkin/kin-openapi@v0.131.0/openapi2conv/openapi2_conv.go:799
 7  0x00000000008b5f5e in github.com/getkin/kin-openapi/openapi2conv.FromV3Operation
    at /home/w0rp/go/pkg/mod/github.com/getkin/kin-openapi@v0.131.0/openapi2conv/openapi2_conv.go:1090
 8  0x00000000008b1796 in github.com/getkin/kin-openapi/openapi2conv.FromV3
    at /home/w0rp/go/pkg/mod/github.com/getkin/kin-openapi@v0.131.0/openapi2conv/openapi2_conv.go:711

I narrowed the issue down to these lines.

		var v2Items *openapi2.SchemaRef
		if val.Items != nil {
			v2Items, _ = FromV3SchemaRef(val.Items, nil) // <- nil passed in here
		}
func FromV3SchemaRef(schema *openapi3.SchemaRef, components *openapi3.Components) (*openapi2.SchemaRef, *openapi2.Parameter) {
	if ref := schema.Ref; ref != "" {
		name := getParameterNameFromNewRef(ref)
		if val, ok := components.Schemas[name]; ok { // <-- Error here because components is nil
			if val.Value.Format == "binary" {
				v2Ref := strings.Replace(ref, "#/components/schemas/", "#/parameters/", 1)
				return nil, &openapi2.Parameter{Ref: v2Ref}
			}
		}

The error disappears if I remove the content of the request bodies from the OpenAPI spec, which I'll attach below.

Diff for adjusting OpenAPI spec to avoid errors
202,205d201
<         content:
<           application/json:
<             schema:
<               $ref: "#/components/schemas/CreateAssistantRequest"
451,454d446
<         content:
<           application/json:
<             schema:
<               $ref: "#/components/schemas/ModifyAssistantRequest"
609,612d600
<         content:
<           application/json:
<             schema:
<               $ref: "#/components/schemas/CreateSpeechRequest"
705,708d692
<         content:
<           multipart/form-data:
<             schema:
<               $ref: "#/components/schemas/CreateTranscriptionRequest"
1295,1298d1278
<         content:
<           multipart/form-data:
<             schema:
<               $ref: "#/components/schemas/CreateTranslationRequest"
1925,1928d1904
<         content:
<           application/json:
<             schema:
<               $ref: "#/components/schemas/CreateChatCompletionRequest"
3167,3170d3142
<         content:
<           application/json:
<             schema:
<               $ref: "#/components/schemas/CreateCompletionRequest"
3311,3314d3282
<         content:
<           application/json:
<             schema:
<               $ref: "#/components/schemas/CreateEmbeddingRequest"
3536,3539d3503
<         content:
<           multipart/form-data:
<             schema:
<               $ref: "#/components/schemas/CreateFileRequest"
3860,3863d3823
<         content:
<           application/json:
<             schema:
<               $ref: "#/components/schemas/CreateFineTuningCheckpointPermissionRequest"
3962,3965d3921
<         content:
<           application/json:
<             schema:
<               $ref: "#/components/schemas/CreateFineTuningJobRequest"
4718,4721d4673
<         content:
<           multipart/form-data:
<             schema:
<               $ref: "#/components/schemas/CreateImageEditRequest"
4807,4810d4758
<         content:
<           application/json:
<             schema:
<               $ref: "#/components/schemas/CreateImageRequest"
4896,4899d4843
<         content:
<           multipart/form-data:
<             schema:
<               $ref: "#/components/schemas/CreateImageVariationRequest"
5208,5211d5151
<         content:
<           application/json:
<             schema:
<               $ref: "#/components/schemas/CreateModerationRequest"
6059,6062d5998
<         content:
<           application/json:
<             schema:
<               $ref: "#/components/schemas/InviteRequest"
6276,6279d6211
<         content:
<           application/json:
<             schema:
<               $ref: "#/components/schemas/ProjectCreateRequest"
6366,6369d6297
<         content:
<           application/json:
<             schema:
<               $ref: "#/components/schemas/ProjectUpdateRequest"
6736,6739d6663
<         content:
<           application/json:
<             schema:
<               $ref: "#/components/schemas/ProjectRateLimitUpdateRequest"
6876,6879d6799
<         content:
<           application/json:
<             schema:
<               $ref: "#/components/schemas/ProjectServiceAccountCreateRequest"
7110,7113d7029
<         content:
<           application/json:
<             schema:
<               $ref: "#/components/schemas/ProjectUserCreateRequest"
7222,7225d7137
<         content:
<           application/json:
<             schema:
<               $ref: "#/components/schemas/ProjectUserUpdateRequest"
8572,8575d8483
<         content:
<           application/json:
<             schema:
<               $ref: "#/components/schemas/UserRoleUpdateRequest"
8666,8669d8573
<         content:
<           application/json:
<             schema:
<               $ref: "#/components/schemas/RealtimeSessionCreateRequest"
8740,8743d8643
<         content:
<           application/json:
<             schema:
<               $ref: "#/components/schemas/RealtimeTranscriptionSessionCreateRequest"
8807,8810d8706
<         content:
<           application/json:
<             schema:
<               $ref: "#/components/schemas/CreateResponse"
10027,10031d9922
<       requestBody:
<         content:
<           application/json:
<             schema:
<               $ref: "#/components/schemas/CreateThreadRequest"
10155,10158d10045
<         content:
<           application/json:
<             schema:
<               $ref: "#/components/schemas/CreateThreadAndRunRequest"
10714,10717d10600
<         content:
<           application/json:
<             schema:
<               $ref: "#/components/schemas/ModifyThreadRequest"
11008,11011d10890
<         content:
<           application/json:
<             schema:
<               $ref: "#/components/schemas/CreateMessageRequest"
11193,11196d11071
<         content:
<           application/json:
<             schema:
<               $ref: "#/components/schemas/ModifyMessageRequest"
11573,11576d11447
<         content:
<           application/json:
<             schema:
<               $ref: "#/components/schemas/CreateRunRequest"
12117,12120d11987
<         content:
<           application/json:
<             schema:
<               $ref: "#/components/schemas/ModifyRunRequest"
12631,12634d12497
<         content:
<           application/json:
<             schema:
<               $ref: "#/components/schemas/SubmitToolOutputsRunRequest"
12983,12986d12845
<         content:
<           application/json:
<             schema:
<               $ref: "#/components/schemas/CreateUploadRequest"
13098,13101d12956
<         content:
<           application/json:
<             schema:
<               $ref: "#/components/schemas/CompleteUploadRequest"
13171,13174d13025
<         content:
<           multipart/form-data:
<             schema:
<               $ref: "#/components/schemas/AddUploadPartRequest"
13324,13327d13174
<         content:
<           application/json:
<             schema:
<               $ref: "#/components/schemas/CreateVectorStoreRequest"
13457,13460d13303
<         content:
<           application/json:
<             schema:
<               $ref: "#/components/schemas/UpdateVectorStoreRequest"
13597,13600d13439
<         content:
<           application/json:
<             schema:
<               $ref: "#/components/schemas/CreateVectorStoreFileBatchRequest"
14356,14359d14194
<         content:
<           application/json:
<             schema:
<               $ref: "#/components/schemas/UpdateVectorStoreFileAttributesRequest"

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions