From 41e32e36b33ff45462e4b1e5251508ef3ea29424 Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Thu, 3 Mar 2022 19:11:57 +0300 Subject: [PATCH] fix for empty default response Signed-off-by: Vasiliy Tolstov --- openapiv3.go | 83 ++++++++++++++++++++++++++++------------------------ variables.go | 4 +-- 2 files changed, 46 insertions(+), 41 deletions(-) diff --git a/openapiv3.go b/openapiv3.go index 00a3ed8..7723d5a 100644 --- a/openapiv3.go +++ b/openapiv3.go @@ -653,49 +653,54 @@ func (g *openapiv3Generator) buildOperationV3( opt := eopt.(*v3.Operation) if r := opt.Responses; r != nil { responses = r - } - if ref := responses.Default.GetReference(); ref != nil && ref.GetXRef() != "" { - xref := strings.TrimPrefix(ref.GetXRef(), ".") - description := "Default" - if strings.Contains(xref, "micro.errors.Error") { - description += " Error" - } - desc, err := protofiles.FindDescriptorByName(protoreflect.FullName(xref)) - if err != nil { - log.Printf("unknown ref type %s err %v", xref, err) - } else { - responses.Default.Oneof = &v3.ResponseOrReference_Response{ - Response: &v3.Response{ - Description: description, - Content: g.responseContentForMessage(&protogen.Message{ - Desc: desc.(protoreflect.MessageDescriptor), - }), - }, - } - } - } - for _, rref := range responses.GetResponseOrReference() { - if ref := rref.Value.GetReference(); ref != nil && ref.GetXRef() != "" { - xref := strings.TrimPrefix(ref.GetXRef(), ".") - description := "Default" - if strings.Contains(xref, "micro.errors.Error") { - description += " Error" - } - desc, err := protofiles.FindDescriptorByName(protoreflect.FullName(xref)) - if err != nil { - log.Printf("unknown ref type %s err %v", xref, err) - } else { - responses.Default.Oneof = &v3.ResponseOrReference_Response{ - Response: &v3.Response{ - Description: description, - Content: g.responseContentForMessage(&protogen.Message{ - Desc: desc.(protoreflect.MessageDescriptor), - }), - }, + rd := responses.Default + if rd != nil { + if ref := rd.GetReference(); ref != nil && ref.GetXRef() != "" { + xref := strings.TrimPrefix(ref.GetXRef(), ".") + description := "Default" + if strings.Contains(xref, "micro.errors.Error") { + description += " Error" + } + desc, err := protofiles.FindDescriptorByName(protoreflect.FullName(xref)) + if err != nil { + log.Printf("unknown ref type %s err %v", xref, err) + } else { + responses.Default.Oneof = &v3.ResponseOrReference_Response{ + Response: &v3.Response{ + Description: description, + Content: g.responseContentForMessage(&protogen.Message{ + Desc: desc.(protoreflect.MessageDescriptor), + }), + }, + } } } } + for _, rref := range responses.GetResponseOrReference() { + if ref := rref.Value.GetReference(); ref != nil && ref.GetXRef() != "" { + xref := strings.TrimPrefix(ref.GetXRef(), ".") + description := "Default" + if strings.Contains(xref, "micro.errors.Error") { + description += " Error" + } + desc, err := protofiles.FindDescriptorByName(protoreflect.FullName(xref)) + if err != nil { + log.Printf("unknown ref type %s err %v", xref, err) + } else { + responses.Default.Oneof = &v3.ResponseOrReference_Response{ + Response: &v3.Response{ + Description: description, + Content: g.responseContentForMessage(&protogen.Message{ + Desc: desc.(protoreflect.MessageDescriptor), + }), + }, + } + } + } + } + } else { + responses = &v3.Responses{} } } else { responses = &v3.Responses{} diff --git a/variables.go b/variables.go index 05d4a01..0cf425b 100644 --- a/variables.go +++ b/variables.go @@ -16,8 +16,8 @@ var ( microServerPackage = protogen.GoImportPath("go.unistack.org/micro/v3/server") microClientHttpPackage = protogen.GoImportPath("go.unistack.org/micro-client-http/v3") microServerHttpPackage = protogen.GoImportPath("go.unistack.org/micro-server-http/v3") - microCodecPackage = protogen.GoImportPath("go.unistack.org/micro-proto/v3/codec") - microErrorsPackage = protogen.GoImportPath("go.unistack.org/micro-proto/v3/errors") + microCodecPackage = protogen.GoImportPath("go.unistack.org/micro/v3/codec") + microErrorsPackage = protogen.GoImportPath("go.unistack.org/micro/v3/errors") timePackage = protogen.GoImportPath("time") deprecationComment = "// Deprecated: Do not use." versionComment = "v3.5.3"