OperationDefinitions returns all operations for a swagger definition.
(swagger *openapi3.T)
| 739 | |
| 740 | // OperationDefinitions returns all operations for a swagger definition. |
| 741 | func OperationDefinitions(swagger *openapi3.T) ([]OperationDefinition, error) { |
| 742 | var operations []OperationDefinition |
| 743 | |
| 744 | if swagger == nil || swagger.Paths == nil { |
| 745 | return operations, nil |
| 746 | } |
| 747 | |
| 748 | // Track alias counters for generating unique client method names |
| 749 | // when multiple paths $ref the same path item. |
| 750 | aliasCounters := map[string]int{} |
| 751 | |
| 752 | for _, requestPath := range SortedMapKeys(swagger.Paths.Map()) { |
| 753 | pathItem := swagger.Paths.Value(requestPath) |
| 754 | // These are parameters defined for all methods on a given path. They |
| 755 | // are shared by all methods. |
| 756 | globalParams, err := DescribeParameters(pathItem.Parameters, nil) |
| 757 | if err != nil { |
| 758 | return nil, fmt.Errorf("error describing global parameters for %s: %s", |
| 759 | requestPath, err) |
| 760 | } |
| 761 | |
| 762 | // Each path can have a number of operations, POST, GET, OPTIONS, etc. |
| 763 | pathOps := pathItem.Operations() |
| 764 | for _, opName := range SortedMapKeys(pathOps) { |
| 765 | // NOTE that this is a reference to the existing copy of the Operation, so any modifications will modify our shared copy of the spec |
| 766 | op := pathOps[opName] |
| 767 | |
| 768 | if pathItem.Servers != nil { |
| 769 | op.Servers = &pathItem.Servers |
| 770 | } |
| 771 | // take a copy of operationId, so we don't modify the underlying spec |
| 772 | operationId := op.OperationID |
| 773 | // Preserve the raw spec value (pre-normalization, pre-prefix, pre-alias-suffix) |
| 774 | // so templates that need to mirror the OpenAPI spec verbatim — e.g. echo's |
| 775 | // per-operation middleware map key — can do so without seeing the |
| 776 | // Go-identifier-friendly transformations applied below. |
| 777 | specOperationId := op.OperationID |
| 778 | // We rely on OperationID to generate function names, it's required |
| 779 | if operationId == "" { |
| 780 | operationId, err = generateDefaultOperationID(opName, requestPath) |
| 781 | if err != nil { |
| 782 | return nil, fmt.Errorf("error generating default OperationID for %s/%s: %s", |
| 783 | opName, requestPath, err) |
| 784 | } |
| 785 | } else { |
| 786 | operationId = nameNormalizer(operationId) |
| 787 | } |
| 788 | operationId = typeNamePrefix(operationId) + operationId |
| 789 | |
| 790 | // Detect path aliases: when a path item has an internal $ref |
| 791 | // pointing to another path in the same document (e.g. |
| 792 | // "#/paths/~1test"), it's a duplicate that would produce |
| 793 | // identical server methods. External $refs (pointing to other |
| 794 | // files) are not aliases — they're the sole definition of |
| 795 | // that path, just stored externally. |
| 796 | isAlias := strings.HasPrefix(pathItem.Ref, "#/paths/") |
| 797 | var aliasTarget string |
| 798 | if isAlias { |
no test coverage detected