Skip to content

Commit

Permalink
Simplify definition names
Browse files Browse the repository at this point in the history
  • Loading branch information
vearutop committed Aug 8, 2023
1 parent 893f0c9 commit 65df89a
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 158 deletions.
3 changes: 1 addition & 2 deletions openapi3/_testdata/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
{"name":"in_path","in":"path","required":true,"schema":{"type":"integer"}},
{"name":"in_cookie","in":"cookie","deprecated":true,"schema":{"type":"string","deprecated":true}},
{"name":"in_header","in":"header","schema":{"type":"number"}},
{"name":"uuid","in":"header","schema":{"$ref":"#/components/schemas/HeaderOpenapi3TestUUID"}}
{"name":"uuid","in":"header","schema":{"$ref":"#/components/schemas/Openapi3TestUUID"}}
],
"requestBody":{
"content":{
Expand Down Expand Up @@ -102,7 +102,6 @@
"upload2":{"$ref":"#/components/schemas/FormDataMultipartFileHeader"}
}
},
"HeaderOpenapi3TestUUID":{"type":"array","items":{"minimum":0,"type":"integer"},"nullable":true},
"Openapi3TestReq":{"type":"object","properties":{"in_body1":{"type":"integer"},"in_body2":{"type":"string"}}},
"Openapi3TestResp":{
"title":"Sample Response","type":"object",
Expand Down
2 changes: 1 addition & 1 deletion openapi3/_testdata/openapi_req2.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@
}
}
}
}
}
8 changes: 4 additions & 4 deletions openapi3/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -555,14 +555,14 @@ func ExampleReflector_AddOperation_queryObject() {
// - content:
// application/json:
// schema:
// $ref: '#/components/schemas/QueryOpenapi3TestJsonFilter'
// $ref: '#/components/schemas/Openapi3TestJsonFilter'
// in: query
// name: json_filter
// - explode: true
// in: query
// name: deep_object_filter
// schema:
// $ref: '#/components/schemas/QueryOpenapi3TestDeepObjectFilter'
// $ref: '#/components/schemas/Openapi3TestDeepObjectFilter'
// style: deepObject
// - in: path
// name: id
Expand All @@ -575,7 +575,7 @@ func ExampleReflector_AddOperation_queryObject() {
// description: No Content
// components:
// schemas:
// QueryOpenapi3TestDeepObjectFilter:
// Openapi3TestDeepObjectFilter:
// properties:
// baz:
// type: boolean
Expand All @@ -587,7 +587,7 @@ func ExampleReflector_AddOperation_queryObject() {
// quux:
// type: number
// type: object
// QueryOpenapi3TestJsonFilter:
// Openapi3TestJsonFilter:
// properties:
// bar:
// type: integer
Expand Down
13 changes: 5 additions & 8 deletions openapi3/reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -456,13 +456,12 @@ func (r *Reflector) parseParametersIn(
return nil
}

defNamePrefix := strings.Title(string(in))
definitionsPrefix := componentsSchemas + defNamePrefix
definitionsPrefix := componentsSchemas

s, err := r.Reflect(input,
openapi.WithOperationCtx(oc, false, in),
jsonschema.DefinitionsPrefix(definitionsPrefix),
jsonschema.CollectDefinitions(r.collectDefinition(defNamePrefix)),
jsonschema.CollectDefinitions(r.collectDefinition()),
jsonschema.PropertyNameMapping(propertyMapping),
jsonschema.PropertyNameTag(string(in), additionalTags...),
func(rc *jsonschema.ReflectContext) {
Expand Down Expand Up @@ -513,7 +512,7 @@ func (r *Reflector) parseParametersIn(
propertySchema, err := r.Reflect(property,
openapi.WithOperationCtx(oc, false, in),
jsonschema.DefinitionsPrefix(definitionsPrefix),
jsonschema.CollectDefinitions(r.collectDefinition(defNamePrefix)),
jsonschema.CollectDefinitions(r.collectDefinition()),
jsonschema.RootRef,
sanitizeDefName,
)
Expand Down Expand Up @@ -588,10 +587,8 @@ func sanitizeDefName(rc *jsonschema.ReflectContext) {
})(rc)
}

func (r *Reflector) collectDefinition(namePrefix string) func(name string, schema jsonschema.Schema) {
func (r *Reflector) collectDefinition() func(name string, schema jsonschema.Schema) {
return func(name string, schema jsonschema.Schema) {
name = namePrefix + name

if _, exists := r.SpecEns().ComponentsEns().SchemasEns().MapOfSchemaOrRefValues[name]; exists {
return
}
Expand Down Expand Up @@ -784,7 +781,7 @@ func (r *Reflector) parseJSONResponse(resp *Response, oc openapi.OperationContex
openapi.WithOperationCtx(oc, true, openapi.InBody),
jsonschema.RootRef,
jsonschema.DefinitionsPrefix(componentsSchemas),
jsonschema.CollectDefinitions(r.collectDefinition("")),
jsonschema.CollectDefinitions(r.collectDefinition()),
sanitizeDefName,
)
if err != nil {
Expand Down
24 changes: 10 additions & 14 deletions openapi3/reflect_deprecated_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ func TestReflector_SetupRequest_queryNamedObject(t *testing.T) {
"parameters":[
{
"name":"in_query","in":"query","style":"deepObject","explode":true,
"schema":{"$ref":"#/components/schemas/QueryOpenapi3TestLabels"}
"schema":{"$ref":"#/components/schemas/Openapi3TestLabels"}
}
],
"responses":{"204":{"description":"No Content"}}
Expand All @@ -406,12 +406,12 @@ func TestReflector_SetupRequest_queryNamedObject(t *testing.T) {
},
"components":{
"schemas":{
"QueryOpenapi3TestLabels":{"type":"object","additionalProperties":{"type":"number"}}
"Openapi3TestLabels":{"type":"object","additionalProperties":{"type":"number"}}
}
}
}`, s)

js, found := reflector.ResolveJSONSchemaRef("#/components/schemas/QueryOpenapi3TestLabels")
js, found := reflector.ResolveJSONSchemaRef("#/components/schemas/Openapi3TestLabels")
assert.True(t, found)
assertjson.EqMarshal(t, `{"type":"object","additionalProperties":{"type":"number"}}`, js)
}
Expand Down Expand Up @@ -446,17 +446,13 @@ func TestReflector_SetupRequest_jsonQuery(t *testing.T) {
{
"name":"one","in":"query",
"content":{
"application/json":{
"schema":{"$ref":"#/components/schemas/QueryOpenapi3TestFilter"}
}
"application/json":{"schema":{"$ref":"#/components/schemas/Openapi3TestFilter"}}
}
},
{
"name":"two","in":"query",
"content":{
"application/json":{
"schema":{"$ref":"#/components/schemas/QueryOpenapi3TestFilter"}
}
"application/json":{"schema":{"$ref":"#/components/schemas/Openapi3TestFilter"}}
}
},
{
Expand All @@ -470,7 +466,7 @@ func TestReflector_SetupRequest_jsonQuery(t *testing.T) {
},
"components":{
"schemas":{
"QueryOpenapi3TestFilter":{
"Openapi3TestFilter":{
"type":"object",
"properties":{
"labels":{"type":"array","items":{"type":"string"}},
Expand Down Expand Up @@ -823,14 +819,14 @@ func TestReflector_SetRequest_queryObject(t *testing.T) {
"name":"json_filter","in":"query",
"content":{
"application/json":{
"schema":{"$ref":"#/components/schemas/QueryOpenapi3TestJsonFilter"}
"schema":{"$ref":"#/components/schemas/Openapi3TestJsonFilter"}
}
}
},
{
"name":"deep_object_filter","in":"query","style":"deepObject",
"explode":true,
"schema":{"$ref":"#/components/schemas/QueryOpenapi3TestDeepObjectFilter"}
"schema":{"$ref":"#/components/schemas/Openapi3TestDeepObjectFilter"}
},
{
"name":"id","in":"path","required":true,
Expand All @@ -843,15 +839,15 @@ func TestReflector_SetRequest_queryObject(t *testing.T) {
},
"components":{
"schemas":{
"QueryOpenapi3TestDeepObjectFilter":{
"Openapi3TestDeepObjectFilter":{
"type":"object",
"properties":{
"baz":{"type":"boolean"},
"deeper":{"type":"object","properties":{"val":{"type":"string"}}},
"quux":{"type":"number"}
}
},
"QueryOpenapi3TestJsonFilter":{
"Openapi3TestJsonFilter":{
"type":"object",
"properties":{
"bar":{"type":"integer"},
Expand Down
24 changes: 10 additions & 14 deletions openapi3/reflect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,7 @@ func TestReflector_AddOperation_request_queryNamedObject(t *testing.T) {
"parameters":[
{
"name":"in_query","in":"query","style":"deepObject","explode":true,
"schema":{"$ref":"#/components/schemas/QueryOpenapi3TestLabels"}
"schema":{"$ref":"#/components/schemas/Openapi3TestLabels"}
}
],
"responses":{"204":{"description":"No Content"}}
Expand All @@ -526,12 +526,12 @@ func TestReflector_AddOperation_request_queryNamedObject(t *testing.T) {
},
"components":{
"schemas":{
"QueryOpenapi3TestLabels":{"type":"object","additionalProperties":{"type":"number"}}
"Openapi3TestLabels":{"type":"object","additionalProperties":{"type":"number"}}
}
}
}`, s)

js, found := reflector.ResolveJSONSchemaRef("#/components/schemas/QueryOpenapi3TestLabels")
js, found := reflector.ResolveJSONSchemaRef("#/components/schemas/Openapi3TestLabels")
assert.True(t, found)
assertjson.EqMarshal(t, `{"type":"object","additionalProperties":{"type":"number"}}`, js)
}
Expand Down Expand Up @@ -565,17 +565,13 @@ func TestReflector_AddOperation_request_jsonQuery(t *testing.T) {
{
"name":"one","in":"query",
"content":{
"application/json":{
"schema":{"$ref":"#/components/schemas/QueryOpenapi3TestFilter"}
}
"application/json":{"schema":{"$ref":"#/components/schemas/Openapi3TestFilter"}}
}
},
{
"name":"two","in":"query",
"content":{
"application/json":{
"schema":{"$ref":"#/components/schemas/QueryOpenapi3TestFilter"}
}
"application/json":{"schema":{"$ref":"#/components/schemas/Openapi3TestFilter"}}
}
},
{
Expand All @@ -589,7 +585,7 @@ func TestReflector_AddOperation_request_jsonQuery(t *testing.T) {
},
"components":{
"schemas":{
"QueryOpenapi3TestFilter":{
"Openapi3TestFilter":{
"type":"object",
"properties":{
"labels":{"type":"array","items":{"type":"string"}},
Expand Down Expand Up @@ -949,14 +945,14 @@ func TestReflector_AddOperation_request_queryObject_deepObject(t *testing.T) {
"name":"json_filter","in":"query",
"content":{
"application/json":{
"schema":{"$ref":"#/components/schemas/QueryOpenapi3TestJsonFilter"}
"schema":{"$ref":"#/components/schemas/Openapi3TestJsonFilter"}
}
}
},
{
"name":"deep_object_filter","in":"query","style":"deepObject",
"explode":true,
"schema":{"$ref":"#/components/schemas/QueryOpenapi3TestDeepObjectFilter"}
"schema":{"$ref":"#/components/schemas/Openapi3TestDeepObjectFilter"}
},
{
"name":"id","in":"path","required":true,
Expand All @@ -969,15 +965,15 @@ func TestReflector_AddOperation_request_queryObject_deepObject(t *testing.T) {
},
"components":{
"schemas":{
"QueryOpenapi3TestDeepObjectFilter":{
"Openapi3TestDeepObjectFilter":{
"type":"object",
"properties":{
"baz":{"type":"boolean"},
"deeper":{"type":"object","properties":{"val":{"type":"string"}}},
"quux":{"type":"number"}
}
},
"QueryOpenapi3TestJsonFilter":{
"Openapi3TestJsonFilter":{
"type":"object",
"properties":{
"bar":{"type":"integer"},
Expand Down
Loading

0 comments on commit 65df89a

Please sign in to comment.