forked from doordash-oss/oapi-codegen-dd
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopenapi_provider.go
More file actions
146 lines (126 loc) · 4.39 KB
/
openapi_provider.go
File metadata and controls
146 lines (126 loc) · 4.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Copyright 2025 DoorDash, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
package codegen
import (
"fmt"
"path/filepath"
"strings"
"unicode"
"github.com/pb33f/libopenapi"
"github.com/pb33f/libopenapi/datamodel"
"go.yaml.in/yaml/v4"
)
func CreateDocument(docContents []byte, cfg Configuration) (libopenapi.Document, error) {
doc, err := LoadDocumentFromContents(docContents, cfg.BasePath)
if err != nil {
return nil, err
}
// Apply overlays before filtering and pruning
if cfg.Overlay != nil && len(cfg.Overlay.Sources) > 0 {
doc, err = applyOverlays(doc, cfg.Overlay.Sources)
if err != nil {
return nil, fmt.Errorf("error applying overlays: %w", err)
}
}
if _, err = doc.BuildV3Model(); err != nil {
return nil, fmt.Errorf("error building model: %w", err)
}
var filtered bool
model, filtered, err := filterOutDocument(doc, cfg.Filter)
if err != nil {
return nil, fmt.Errorf("error filtering document: %w", err)
}
// If we filtered anything, we must prune to remove dangling references
// Otherwise, only prune if SkipPrune is false
if filtered || !cfg.SkipPrune {
if err = pruneSchema(model); err != nil {
return nil, fmt.Errorf("error pruning schema: %w", err)
}
return doc, nil
}
return doc, nil
}
// LoadDocumentFromContents creates a libopenapi Document from raw bytes.
// An optional basePath can be provided to resolve relative $ref file references.
// If provided, basePath should be an absolute path to the directory containing the spec file.
func LoadDocumentFromContents(contents []byte, basePath ...string) (libopenapi.Document, error) {
docConfig := &datamodel.DocumentConfiguration{
SkipCircularReferenceCheck: true,
}
if len(basePath) > 0 && basePath[0] != "" {
bp := basePath[0]
if !filepath.IsAbs(bp) {
var err error
bp, err = filepath.Abs(bp)
if err != nil {
return nil, fmt.Errorf("error resolving base path %q: %w", basePath[0], err)
}
}
docConfig.BasePath = bp
docConfig.AllowFileReferences = true
}
doc, err := libopenapi.NewDocumentWithConfiguration(contents, docConfig)
if err != nil {
return fixDocument(contents, err, docConfig)
}
return doc, nil
}
func fixDocument(contents []byte, originalErr error, docConfig *datamodel.DocumentConfiguration) (libopenapi.Document, error) {
if !strings.Contains(originalErr.Error(), "unable to parse specification") {
return nil, originalErr
}
// Strip control characters if the error indicates they're present
processedContents := contents
// Check if it's a fixable error (control character error)
if strings.Contains(originalErr.Error(), "control characters are not allowed") {
text := string(contents)
cleaned := strings.Map(func(r rune) rune {
// Keep printable characters, tabs, newlines, and carriage returns
if unicode.IsPrint(r) || r == '\t' || r == '\n' || r == '\r' {
return r
}
// Remove control characters
return -1
}, text)
processedContents = []byte(cleaned)
}
// Unmarshal into a generic map
var doc map[string]any
if err := yaml.Unmarshal(processedContents, &doc); err != nil {
return nil, fmt.Errorf("failed to unmarshal document: %w", err)
}
// Extract existing title and version from info section if available
title := "API"
version := "1.0.0"
if info, ok := doc["info"].(map[string]any); ok {
if t, ok := info["title"].(string); ok && t != "" {
title = t
}
if v, ok := info["version"].(string); ok && v != "" {
version = v
}
}
// Replace info section with minimal required fields using existing values
doc["info"] = map[string]any{
"title": title,
"version": version,
}
// Marshal back to YAML
fixed, err := yaml.Marshal(doc)
if err != nil {
return nil, fmt.Errorf("failed to marshal document: %w", err)
}
// Retry with fixed content
result, err := libopenapi.NewDocumentWithConfiguration(fixed, docConfig)
if err != nil {
return nil, fmt.Errorf("error creating document after fix: %w", err)
}
return result, nil
}