<commit_before>package modify

import (
	"fmt"
	"regexp"
	"strings"

	oas3 "github.com/getkin/kin-openapi/openapi3"
	"github.com/grokify/swaggman/openapi3"
)

const oas3ComponentsSchemasBase = "#/components/schemas"

var rxOAS2RefDefinition = regexp.MustCompile(`#/definitions/(.*)`)

func SpecOperationsFixResponseReferences(spec *oas3.Swagger) []*openapi3.OperationMeta {
	errorOperations := []*openapi3.OperationMeta{}
	openapi3.VisitOperations(spec, func(path, method string, op *oas3.Operation) {
		if op == nil {
			return
		}
		for resCode, resRef := range op.Responses {
			if strings.Index(resRef.Ref, "#/definitions/") == 0 {
				m := rxOAS2RefDefinition.FindStringSubmatch(resRef.Ref)
				if len(m) > 0 {
					resRefOrig := resRef.Ref
					resRef.Ref = oas3ComponentsSchemasBase + "/" + m[1]
					om := openapi3.OperationToMeta(path, method, op)
					om.MetaNotes = append(om.MetaNotes,
						fmt.Sprintf("E_BAD_RESPONSE_REF_OAS2_DEF [%s] type[%s]", resCode, resRefOrig))
					errorOperations = append(errorOperations, &om)
				}<commit_msg>streamline: openapi3/modify: update SpecOperationsFixResponseReferences()<commit_after>package modify

import (
	"fmt"
	"regexp"
	"strings"

	oas3 "github.com/getkin/kin-openapi/openapi3"
	"github.com/grokify/swaggman/openapi3"
)

const (
	oas2BasePathDefinitions       = "#/definitions/"
	oas3BasePathComponentsSchemas = "#/components/schemas/"
)

var rxOAS2RefDefinition = regexp.MustCompile(`^#/definitions/(.*)`)

func SpecOperationsFixResponseReferences(spec *oas3.Swagger) []*openapi3.OperationMeta {
	errorOperations := []*openapi3.OperationMeta{}
	openapi3.VisitOperations(spec, func(path, method string, op *oas3.Operation) {
		if op == nil {
			return
		}
		for resCode, resRef := range op.Responses {
			if strings.Index(resRef.Ref, oas2BasePathDefinitions) == 0 {
				resRef.Ref = strings.TrimSpace(resRef.Ref)
				m := rxOAS2RefDefinition.FindStringSubmatch(resRef.Ref)
				if len(m) > 0 {
					resRefOrig := resRef.Ref
					resRef.Ref = oas3BasePathComponentsSchemas + m[1]
					om := openapi3.OperationToMeta(path, method, op)
					om.MetaNotes = append(om.MetaNotes,
						fmt.Sprintf("E_BAD_RESPONSE_REF_OAS2_DEF [%s] type[%s]", resCode, resRefOrig))
					errorOperations = append(errorOperations, &om)
				}
{'fraction_non_alphanumeric': 0.13350900307422048, 'fraction_numerical': 0.016249451032059728, 'mean_word_length': 4.610837438423645, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2756847', 'n_tokens_mistral': 928, 'n_tokens_neox': 830, 'n_words': 135}