Skip to content

Commit

Permalink
fix: update type: file to type: string and format: binary (#980)
Browse files Browse the repository at this point in the history
  • Loading branch information
reversearrow authored Jul 10, 2024
1 parent 3e230a6 commit a7bc0ff
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
68 changes: 68 additions & 0 deletions openapi2conv/issue979_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package openapi2conv

import (
"context"
"encoding/json"
"testing"

"github.com/stretchr/testify/require"

"github.com/getkin/kin-openapi/openapi2"
"github.com/getkin/kin-openapi/openapi3"
)

func TestIssue979(t *testing.T) {
v2 := []byte(`
{
"basePath": "/v2",
"host": "test.example.com",
"info": {
"title": "MyAPI",
"version": "0.1",
"x-info": "info extension"
},
"paths": {
"/foo": {
"get": {
"operationId": "getFoo",
"produces": [
"application/pdf",
"application/json"
],
"responses": {
"200": {
"description": "returns all information",
"schema": {
"type": "file"
}
},
"default": {
"description": "OK"
}
},
"summary": "get foo"
}
}
},
"schemes": [
"http"
],
"swagger": "2.0"
}
`)

var doc2 openapi2.T
err := json.Unmarshal(v2, &doc2)
require.NoError(t, err)

doc3, err := ToV3(&doc2)
require.NoError(t, err)
err = doc3.Validate(context.Background())
require.NoError(t, err)

require.Equal(t, &openapi3.Types{"string"}, doc3.Paths.Value("/foo").Get.Responses.Value("200").Value.Content.Get("application/json").Schema.Value.Type)
require.Equal(t, "binary", doc3.Paths.Value("/foo").Get.Responses.Value("200").Value.Content.Get("application/json").Schema.Value.Format)

require.Equal(t, &openapi3.Types{"string"}, doc3.Paths.Value("/foo").Get.Responses.Value("200").Value.Content.Get("application/pdf").Schema.Value.Type)
require.Equal(t, "binary", doc3.Paths.Value("/foo").Get.Responses.Value("200").Value.Content.Get("application/pdf").Schema.Value.Format)
}
3 changes: 3 additions & 0 deletions openapi2conv/openapi2_conv.go
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,9 @@ func ToV3SchemaRef(schema *openapi3.SchemaRef) *openapi3.SchemaRef {
if schema.Value.Items != nil {
schema.Value.Items = ToV3SchemaRef(schema.Value.Items)
}
if schema.Value.Type.Is("file") {
schema.Value.Format, schema.Value.Type = "binary", &openapi3.Types{"string"}
}
for k, v := range schema.Value.Properties {
schema.Value.Properties[k] = ToV3SchemaRef(v)
}
Expand Down

0 comments on commit a7bc0ff

Please sign in to comment.