Skip to content

Commit

Permalink
Refactor: change add status fucntion to get response and add into api…
Browse files Browse the repository at this point in the history
… doc
  • Loading branch information
VoidGun committed Oct 17, 2024
1 parent 647f1bf commit 23e6a98
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 35 deletions.
34 changes: 19 additions & 15 deletions net/goai/goai_path.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,13 @@ func (oai *OpenApiV3) addPath(in addPathInput) error {
}

var (
mime string
path = Path{XExtensions: make(XExtensions)}
inputMetaMap = gmeta.Data(inputObject.Interface())
outputMetaMap = gmeta.Data(outputObject.Interface())
isInputStructEmpty = oai.doesStructHasNoFields(inputObject.Interface())
inputStructTypeName = oai.golangTypeToSchemaName(inputObject.Type())
outputStructTypeName = oai.golangTypeToSchemaName(outputObject.Type())
operation = Operation{
mime string
path = Path{XExtensions: make(XExtensions)}
inputMetaMap = gmeta.Data(inputObject.Interface())
outputMetaMap = gmeta.Data(outputObject.Interface())
isInputStructEmpty = oai.doesStructHasNoFields(inputObject.Interface())
inputStructTypeName = oai.golangTypeToSchemaName(inputObject.Type())
operation = Operation{
Responses: map[string]ResponseRef{},
XExtensions: make(XExtensions),
}
Expand Down Expand Up @@ -245,9 +244,12 @@ func (oai *OpenApiV3) addPath(in addPathInput) error {
}
status = statusValue
}
err := operation.Responses.addStatus(oai, status, outputObject.Interface(), outputStructTypeName, true)
if err != nil {
return err
if _, ok := operation.Responses[status]; !ok {
response, err := oai.getResponseFromObject(outputObject.Interface())
if err != nil {
return err
}
operation.Responses[status] = ResponseRef{Value: response}
}

// =================================================================================================================
Expand All @@ -261,11 +263,13 @@ func (oai *OpenApiV3) addPath(in addPathInput) error {
if data == nil {
continue
}
dataType := oai.golangTypeToSchemaName(reflect.TypeOf(data))
status := gconv.String(statusCode)
err := operation.Responses.addStatus(oai, status, data, dataType, false)
if err != nil {
return err
if _, ok := operation.Responses[status]; !ok {
response, err := oai.getResponseFromObject(data, false)
if err != nil {
return err
}
operation.Responses[status] = ResponseRef{Value: response}
}
}
}
Expand Down
36 changes: 16 additions & 20 deletions net/goai/goai_response_ref.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,39 +24,36 @@ type ResponseRef struct {
// Responses is specified by OpenAPI/Swagger 3.0 standard.
type Responses map[string]ResponseRef

// AddStatus adds object to Responses with given status code.
// It ignores adding status code that already exists.
// businessStructName is the name of the business struct, could use the same way in oai.golangTypeToSchemaName.
// isDefault is a flag indicating whether it is the default response.
// If isDefault is set to false, it would override response schema when the mime type is set or the object has fields.
// Otherwise it would remain the same logic as original response adding logic.
func (r *Responses) addStatus(oai *OpenApiV3, status string, object interface{}, businessStructName string, isDefault bool) error {
// Ignore added status
if _, ok := (*r)[status]; ok {
return nil
// object could be someObject.Interface()
// There may be some difference between someObject.Type() and reflect.TypeOf(object).
func (oai *OpenApiV3) getResponseFromObject(object interface{}, isDefault ...bool) (*Response, error) {
// Add default status by default.
var isDefaultStatus = true
if len(isDefault) > 0 {
isDefaultStatus = isDefault[0]
}
// Add object schema to oai
if err := oai.addSchema(object); err != nil {
return err
return nil, err
}
var (
metaMap = gmeta.Data(object)
response = Response{
response = &Response{
Content: map[string]MediaType{},
XExtensions: make(XExtensions),
}
)
if len(metaMap) > 0 {
if err := oai.tagMapToResponse(metaMap, &response); err != nil {
return err
if err := oai.tagMapToResponse(metaMap, response); err != nil {
return nil, err
}
}
// Supported mime types of response.
var (
contentTypes = oai.Config.ReadContentTypes
tagMimeValue = gmeta.Get(object, gtag.Mime).String()
refInput = getResponseSchemaRefInput{
BusinessStructName: businessStructName,
BusinessStructName: oai.golangTypeToSchemaName(reflect.TypeOf(object)),
CommonResponseObject: oai.Config.CommonResponse,
CommonResponseDataField: oai.Config.CommonResponseDataField,
}
Expand All @@ -70,7 +67,7 @@ func (r *Responses) addStatus(oai *OpenApiV3, status string, object interface{},
refInput.CommonResponseObject = nil
refInput.CommonResponseDataField = ""
}
if !isDefault {
if !isDefaultStatus {
fields, _ := gstructs.Fields(gstructs.FieldsInput{
Pointer: object,
RecursiveOption: gstructs.RecursiveOptionEmbeddedNoTag,
Expand All @@ -88,20 +85,19 @@ func (r *Responses) addStatus(oai *OpenApiV3, status string, object interface{},
examples := make(Examples)
if responseExamplePath != "" {
if err := examples.applyExamplesFile(responseExamplePath); err != nil {
return err
return nil, err
}
}
schemaRef, err := oai.getResponseSchemaRef(refInput)
if err != nil {
return err
return nil, err
}
response.Content[v] = MediaType{
Schema: schemaRef,
Examples: examples,
}
}
(*r)[status] = ResponseRef{Value: &response}
return nil
return response, nil
}

func (r ResponseRef) MarshalJSON() ([]byte, error) {
Expand Down

0 comments on commit 23e6a98

Please sign in to comment.