Skip to content

Commit

Permalink
Avoid redundant prefixes for schema definitions (#84)
Browse files Browse the repository at this point in the history
  • Loading branch information
vearutop authored Oct 3, 2023
1 parent 1c5cea9 commit ae8c373
Show file tree
Hide file tree
Showing 25 changed files with 353 additions and 143 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ require (
github.com/bool64/dev v0.2.31
github.com/stretchr/testify v1.8.2
github.com/swaggest/assertjson v1.9.0
github.com/swaggest/jsonschema-go v0.3.61
github.com/swaggest/refl v1.2.1
github.com/swaggest/jsonschema-go v0.3.62
github.com/swaggest/refl v1.3.0
gopkg.in/yaml.v2 v2.4.0
)

Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/swaggest/assertjson v1.9.0 h1:dKu0BfJkIxv/xe//mkCrK5yZbs79jL7OVf9Ija7o2xQ=
github.com/swaggest/assertjson v1.9.0/go.mod h1:b+ZKX2VRiUjxfUIal0HDN85W0nHPAYUbYH5WkkSsFsU=
github.com/swaggest/jsonschema-go v0.3.61 h1:46Jk7nifEDrGsPEAlWCVprqSaczETWO1LhtAKtxMM5I=
github.com/swaggest/jsonschema-go v0.3.61/go.mod h1:FAsTxvFjOp+aS4DqXrY7MTYC2r7rrmxEzxEgbQVyR/c=
github.com/swaggest/refl v1.2.1 h1:1meX9NaXjM5lmb4kk4RP3OZsXFRke9B1EHAP/pCEKO0=
github.com/swaggest/refl v1.2.1/go.mod h1:CkC6g7h1PW33KprTuYRSw8UUOslRUt4lF3oe7tTIgNU=
github.com/swaggest/jsonschema-go v0.3.62 h1:eIE0aRklWa2eLJg2L/zqyWpKvgUPbq2oKOtrJGJkPH0=
github.com/swaggest/jsonschema-go v0.3.62/go.mod h1:DYuKqdpms/edvywsX6p1zHXCZkdwB28wRaBdFCe3Duw=
github.com/swaggest/refl v1.3.0 h1:PEUWIku+ZznYfsoyheF97ypSduvMApYyGkYF3nabS0I=
github.com/swaggest/refl v1.3.0/go.mod h1:3Ujvbmh1pfSbDYjC6JGG7nMgPvpG0ehQL4iNonnLNbg=
github.com/yudai/gojsondiff v1.0.0 h1:27cbfqXLVEJ1o8I6v3y9lg8Ydm53EKqHXAOMxEGlCOA=
github.com/yudai/gojsondiff v1.0.0/go.mod h1:AY32+k2cwILAkW1fbgxQ5mUmMiZFgLIV+FBNExI05xg=
github.com/yudai/golcs v0.0.0-20170316035057-ecda9a501e82 h1:BHyfKlQyqbsFN5p3IfnEUduWvb9is428/nNb5L3U01M=
Expand Down
14 changes: 0 additions & 14 deletions openapi3/_testdata/req_schema.json

This file was deleted.

29 changes: 24 additions & 5 deletions openapi3/reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,9 +244,9 @@ func (r *Reflector) setupRequest(o *Operation, oc openapi.OperationContext) erro
switch cu.ContentType {
case "":
if err := joinErrors(
r.parseRequestBody(o, oc, cu, mimeFormUrlencoded, oc.Method(), cu.FieldMapping(openapi.InFormData), tagFormData, tagForm),
r.parseParameters(o, oc, cu),
r.parseRequestBody(o, oc, cu, mimeJSON, oc.Method(), nil, tagJSON),
r.parseRequestBody(o, oc, cu, mimeFormUrlencoded, oc.Method(), cu.FieldMapping(openapi.InFormData), tagFormData, tagForm),
); err != nil {
return err
}
Expand All @@ -259,8 +259,8 @@ func (r *Reflector) setupRequest(o *Operation, oc openapi.OperationContext) erro
}
case mimeFormUrlencoded, mimeMultipart:
if err := joinErrors(
r.parseParameters(o, oc, cu),
r.parseRequestBody(o, oc, cu, mimeFormUrlencoded, oc.Method(), cu.FieldMapping(openapi.InFormData), tagFormData, tagForm),
r.parseParameters(o, oc, cu),
); err != nil {
return err
}
Expand Down Expand Up @@ -352,7 +352,9 @@ func (r *Reflector) parseRequestBody(
}

// If `formData` is defined on a request body `json` is ignored.
if tag == tagJSON && refl.HasTaggedFields(input, tagFormData) && !forceJSONRequestBody {
if tag == tagJSON &&
(refl.HasTaggedFields(input, tagFormData) || refl.HasTaggedFields(input, tagForm)) &&
!forceJSONRequestBody {
return nil
}

Expand All @@ -370,7 +372,24 @@ func (r *Reflector) parseRequestBody(

schema, err := r.Reflect(input,
openapi.WithOperationCtx(oc, false, "body"),
jsonschema.DefinitionsPrefix(componentsSchemas+definitionPrefix),
jsonschema.DefinitionsPrefix(componentsSchemas),
jsonschema.InterceptDefName(func(t reflect.Type, defaultDefName string) string {
if tag != tagJSON {
v := reflect.New(t).Interface()

if refl.HasTaggedFields(v, tag) {
return definitionPrefix + defaultDefName
}

for _, at := range additionalTags {
if refl.HasTaggedFields(v, at) {
return definitionPrefix + defaultDefName
}
}
}

return defaultDefName
}),
jsonschema.RootRef,
jsonschema.PropertyNameMapping(mapping),
jsonschema.PropertyNameTag(tag, additionalTags...),
Expand Down Expand Up @@ -416,7 +435,7 @@ func (r *Reflector) parseRequestBody(

s.FromJSONSchema(def)

r.SpecEns().ComponentsEns().SchemasEns().WithMapOfSchemaOrRefValuesItem(definitionPrefix+name, s)
r.SpecEns().ComponentsEns().SchemasEns().WithMapOfSchemaOrRefValuesItem(name, s)
}

if mime == mimeFormUrlencoded && hasFileUpload {
Expand Down
78 changes: 43 additions & 35 deletions openapi3/reflect_deprecated_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ func TestReflector_SetRequest_array(t *testing.T) {
b, err := assertjson.MarshalIndentCompact(s, "", " ", 120)
assert.NoError(t, err)

require.NoError(t, os.WriteFile("_testdata/openapi_req_array_last_run.json", b, 0o600))
require.NoError(t, os.WriteFile("testdata/openapi_req_array_last_run.json", b, 0o600))

expected, err := os.ReadFile("_testdata/openapi_req_array.json")
expected, err := os.ReadFile("testdata/openapi_req_array.json")
require.NoError(t, err)

assertjson.Equal(t, expected, b)
Expand Down Expand Up @@ -69,11 +69,11 @@ func TestReflector_SetRequest_uploadInterface(t *testing.T) {
},
"components":{
"schemas":{
"FormDataMultipartFile":{"type":"string","format":"binary","nullable":true},
"FormDataOpenapi3TestReq":{
"type":"object",
"properties":{"upload1":{"$ref":"#/components/schemas/FormDataMultipartFile"}}
}
"properties":{"upload1":{"$ref":"#/components/schemas/MultipartFile"}}
},
"MultipartFile":{"type":"string","format":"binary","nullable":true}
}
}
}`, s)
Expand All @@ -97,9 +97,9 @@ func TestReflector_SetRequest(t *testing.T) {
b, err := assertjson.MarshalIndentCompact(s, "", " ", 120)
assert.NoError(t, err)

require.NoError(t, os.WriteFile("_testdata/openapi_req2_last_run.json", b, 0o600))
require.NoError(t, os.WriteFile("testdata/openapi_req2_last_run.json", b, 0o600))

expected, err := os.ReadFile("_testdata/openapi_req2.json")
expected, err := os.ReadFile("testdata/openapi_req2.json")
require.NoError(t, err)

assertjson.Equal(t, expected, b)
Expand Down Expand Up @@ -128,7 +128,7 @@ func TestReflector_SetJSONResponse(t *testing.T) {
s.Paths.WithMapOfPathItemValuesItem("/somewhere/{in_path}", pathItem)

js := op.RequestBody.RequestBody.Content["multipart/form-data"].Schema.ToJSONSchema(s)
expected, err := os.ReadFile("_testdata/req_schema.json")
expected, err := os.ReadFile("testdata/req_schema.json")
require.NoError(t, err)
assertjson.EqualMarshal(t, expected, js)

Expand All @@ -145,9 +145,9 @@ func TestReflector_SetJSONResponse(t *testing.T) {
jsb, err := assertjson.MarshalIndentCompact(js, "", " ", 120)
require.NoError(t, err)

require.NoError(t, os.WriteFile("_testdata/resp_schema_last_run.json", jsb, 0o600))
require.NoError(t, os.WriteFile("testdata/resp_schema_last_run.json", jsb, 0o600))

expected, err = os.ReadFile("_testdata/resp_schema.json")
expected, err = os.ReadFile("testdata/resp_schema.json")
require.NoError(t, err)
assertjson.EqualMarshal(t, expected, js)

Expand All @@ -162,9 +162,9 @@ func TestReflector_SetJSONResponse(t *testing.T) {
b, err := assertjson.MarshalIndentCompact(s, "", " ", 120)
assert.NoError(t, err)

require.NoError(t, os.WriteFile("_testdata/openapi_last_run.json", b, 0o600))
require.NoError(t, os.WriteFile("testdata/openapi_last_run.json", b, 0o600))

expected, err = os.ReadFile("_testdata/openapi.json")
expected, err = os.ReadFile("testdata/openapi.json")
require.NoError(t, err)

assertjson.EqualMarshal(t, expected, s)
Expand Down Expand Up @@ -311,31 +311,39 @@ func TestReflector_SetupRequest(t *testing.T) {
require.NoError(t, s.AddOperation(http.MethodPost, "/somewhere/{value-4}", op))

assertjson.EqMarshal(t, `{
"openapi":"3.0.3","info":{"title":"SampleAPI","version":"1.2.3"},
"paths":{
"/somewhere/{value-4}":{
"post":{
"parameters":[
{"name":"value_2","in":"query","schema":{"type":"string"}},
{"name":"value-4","in":"path","required":true,"schema":{"type":"boolean"}},
{"name":"value_5","in":"cookie","schema":{"type":"string"}},
{"name":"X-Value-1","in":"header","schema":{"type":"integer"}}
],
"requestBody":{
"content":{
"multipart/form-data":{
"schema":{
"type":"object",
"properties":{"upload6":{"$ref":"#/components/schemas/FormDataMultipartFile"},"value3":{"type":"number"}}
}
"openapi":"3.0.3","info":{"title":"SampleAPI","version":"1.2.3"},
"paths":{
"/somewhere/{value-4}":{
"post":{
"parameters":[
{"name":"value_2","in":"query","schema":{"type":"string"}},
{
"name":"value-4","in":"path","required":true,
"schema":{"type":"boolean"}
},
{"name":"value_5","in":"cookie","schema":{"type":"string"}},
{"name":"X-Value-1","in":"header","schema":{"type":"integer"}}
],
"requestBody":{
"content":{
"multipart/form-data":{
"schema":{
"type":"object",
"properties":{
"upload6":{"$ref":"#/components/schemas/MultipartFile"},
"value3":{"type":"number"}
}
}
}
}
},
"responses":{"204":{"description":"No Content"}}
}
}
},
"responses":{"204":{"description":"No Content"}}
}
}
},
"components":{
"schemas":{"MultipartFile":{"type":"string","format":"binary","nullable":true}}
}
},
"components":{"schemas":{"FormDataMultipartFile":{"type":"string","format":"binary","nullable":true}}}
}`, s)
}

Expand Down
Loading

0 comments on commit ae8c373

Please sign in to comment.