| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent e126f5a commit 8822002
19 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -24,9 +24,11 @@ import ( | |||
| 24 | 24 | "go/scanner" | |
| 25 | 25 | "io" | |
| 26 | 26 | "io/fs" | |
| 27 | + "maps" | ||
| 27 | 28 | "net/http" | |
| 28 | 29 | "os" | |
| 29 | 30 | "runtime/debug" | |
| 31 | + "slices" | ||
| 30 | 32 | "sort" | |
| 31 | 33 | "strings" | |
| 32 | 34 | "text/template" | |
@@ -103,11 +105,8 @@ func constructImportMapping(importMapping map[string]string) importMap { | |||
| 103 | 105 | ) | |
| 104 | 106 | ||
| 105 | 107 | { | |
| 106 | - var packagePaths []string | ||
| 107 | - for _, packageName := range importMapping { | ||
| 108 | - packagePaths = append(packagePaths, packageName) | ||
| 109 | - } | ||
| 110 | - sort.Strings(packagePaths) | ||
| 108 | + packagePaths := slices.Collect(maps.Values(importMapping)) | ||
| 109 | + slices.Sort(packagePaths) | ||
| 111 | 110 | ||
| 112 | 111 | for _, packagePath := range packagePaths { | |
| 113 | 112 | if _, ok := pathToImport[packagePath]; !ok && packagePath != importMappingCurrentPackage { | |
@@ -255,7 +254,7 @@ func Generate(spec *openapi3.T, opts Configuration) (string, error) { | |||
| 255 | 254 | if err != nil { | |
| 256 | 255 | return "", fmt.Errorf("error getting type definition imports: %w", err) | |
| 257 | 256 | } | |
| 258 | - MergeImports(xGoTypeImports, imprts) | ||
| 257 | + maps.Copy(xGoTypeImports, imprts) | ||
| 259 | 258 | } | |
| 260 | 259 | ||
| 261 | 260 | var serverURLsDefinitions string | |
@@ -626,11 +625,7 @@ func GenerateConstants(t *template.Template, ops []OperationDefinition) (string, | |||
| 626 | 625 | } | |
| 627 | 626 | } | |
| 628 | 627 | ||
| 629 | - var providerNames []string | ||
| 630 | - for providerName := range providerNameMap { | ||
| 631 | - providerNames = append(providerNames, providerName) | ||
| 632 | - } | ||
| 633 | - | ||
| 628 | + providerNames := slices.Collect(maps.Keys(providerNameMap)) | ||
| 634 | 629 | sort.Strings(providerNames) | |
| 635 | 630 | ||
| 636 | 631 | constants.SecuritySchemeProviderNames = append(constants.SecuritySchemeProviderNames, providerNames...) | |
@@ -944,7 +939,7 @@ func resolvedNameForComponent(section, name string, contentType ...string) strin | |||
| 944 | 939 | } | |
| 945 | 940 | if len(matches) > 0 { | |
| 946 | 941 | if len(matches) > 1 { | |
| 947 | - sort.Strings(matches) | ||
| 942 | + slices.Sort(matches) | ||
| 948 | 943 | } | |
| 949 | 944 | return globalState.resolvedNames[matches[0]] | |
| 950 | 945 | } | |
@@ -1243,14 +1238,14 @@ func OperationSchemaImports(s *Schema) (map[string]goImport, error) { | |||
| 1243 | 1238 | if err != nil { | |
| 1244 | 1239 | return nil, err | |
| 1245 | 1240 | } | |
| 1246 | - MergeImports(res, imprts) | ||
| 1241 | + maps.Copy(res, imprts) | ||
| 1247 | 1242 | } | |
| 1248 | 1243 | ||
| 1249 | 1244 | imprts, err := GoSchemaImports(&openapi3.SchemaRef{Value: s.OAPISchema}) | |
| 1250 | 1245 | if err != nil { | |
| 1251 | 1246 | return nil, err | |
| 1252 | 1247 | } | |
| 1253 | - MergeImports(res, imprts) | ||
| 1248 | + maps.Copy(res, imprts) | ||
| 1254 | 1249 | return res, nil | |
| 1255 | 1250 | } | |
| 1256 | 1251 | ||
@@ -1263,7 +1258,7 @@ func OperationImports(ops []OperationDefinition) (map[string]goImport, error) { | |||
| 1263 | 1258 | if err != nil { | |
| 1264 | 1259 | return nil, err | |
| 1265 | 1260 | } | |
| 1266 | - MergeImports(res, imprts) | ||
| 1261 | + maps.Copy(res, imprts) | ||
| 1267 | 1262 | } | |
| 1268 | 1263 | } | |
| 1269 | 1264 | ||
@@ -1272,7 +1267,7 @@ func OperationImports(ops []OperationDefinition) (map[string]goImport, error) { | |||
| 1272 | 1267 | if err != nil { | |
| 1273 | 1268 | return nil, err | |
| 1274 | 1269 | } | |
| 1275 | - MergeImports(res, imprts) | ||
| 1270 | + maps.Copy(res, imprts) | ||
| 1276 | 1271 | } | |
| 1277 | 1272 | ||
| 1278 | 1273 | for _, b := range op.Responses { | |
@@ -1281,7 +1276,7 @@ func OperationImports(ops []OperationDefinition) (map[string]goImport, error) { | |||
| 1281 | 1276 | if err != nil { | |
| 1282 | 1277 | return nil, err | |
| 1283 | 1278 | } | |
| 1284 | - MergeImports(res, imprts) | ||
| 1279 | + maps.Copy(res, imprts) | ||
| 1285 | 1280 | } | |
| 1286 | 1281 | } | |
| 1287 | 1282 | ||
@@ -1316,7 +1311,7 @@ func GetTypeDefinitionsImports(swagger *openapi3.T, excludeSchemas []string) (ma | |||
| 1316 | 1311 | } | |
| 1317 | 1312 | ||
| 1318 | 1313 | for _, imprts := range []map[string]goImport{schemaImports, reqBodiesImports, responsesImports, parametersImports} { | |
| 1319 | - MergeImports(res, imprts) | ||
| 1314 | + maps.Copy(res, imprts) | ||
| 1320 | 1315 | } | |
| 1321 | 1316 | return res, nil | |
| 1322 | 1317 | } | |
@@ -1343,14 +1338,14 @@ func GoSchemaImports(schemas ...*openapi3.SchemaRef) (map[string]goImport, error | |||
| 1343 | 1338 | if err != nil { | |
| 1344 | 1339 | return nil, err | |
| 1345 | 1340 | } | |
| 1346 | - MergeImports(res, imprts) | ||
| 1341 | + maps.Copy(res, imprts) | ||
| 1347 | 1342 | } | |
| 1348 | 1343 | } else if t.Is("array") { | |
| 1349 | 1344 | imprts, err := GoSchemaImports(schemaVal.Items) | |
| 1350 | 1345 | if err != nil { | |
| 1351 | 1346 | return nil, err | |
| 1352 | 1347 | } | |
| 1353 | - MergeImports(res, imprts) | ||
| 1348 | + maps.Copy(res, imprts) | ||
| 1354 | 1349 | } | |
| 1355 | 1350 | } | |
| 1356 | 1351 | return res, nil | |
@@ -1371,7 +1366,7 @@ func GetSchemaImports(schemas map[string]*openapi3.SchemaRef, excludeSchemas []s | |||
| 1371 | 1366 | if err != nil { | |
| 1372 | 1367 | return nil, err | |
| 1373 | 1368 | } | |
| 1374 | - MergeImports(res, imprts) | ||
| 1369 | + maps.Copy(res, imprts) | ||
| 1375 | 1370 | } | |
| 1376 | 1371 | return res, nil | |
| 1377 | 1372 | } | |
@@ -1389,7 +1384,7 @@ func GetRequestBodiesImports(bodies map[string]*openapi3.RequestBodyRef) (map[st | |||
| 1389 | 1384 | if err != nil { | |
| 1390 | 1385 | return nil, err | |
| 1391 | 1386 | } | |
| 1392 | - MergeImports(res, imprts) | ||
| 1387 | + maps.Copy(res, imprts) | ||
| 1393 | 1388 | } | |
| 1394 | 1389 | } | |
| 1395 | 1390 | return res, nil | |
@@ -1408,7 +1403,7 @@ func GetResponsesImports(responses map[string]*openapi3.ResponseRef) (map[string | |||
| 1408 | 1403 | if err != nil { | |
| 1409 | 1404 | return nil, err | |
| 1410 | 1405 | } | |
| 1411 | - MergeImports(res, imprts) | ||
| 1406 | + maps.Copy(res, imprts) | ||
| 1412 | 1407 | } | |
| 1413 | 1408 | } | |
| 1414 | 1409 | return res, nil | |
@@ -1424,7 +1419,7 @@ func GetParametersImports(params map[string]*openapi3.ParameterRef) (map[string] | |||
| 1424 | 1419 | if err != nil { | |
| 1425 | 1420 | return nil, err | |
| 1426 | 1421 | } | |
| 1427 | - MergeImports(res, imprts) | ||
| 1422 | + maps.Copy(res, imprts) | ||
| 1428 | 1423 | } | |
| 1429 | 1424 | return res, nil | |
| 1430 | 1425 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,14 +1,18 @@ | |||
| 1 | 1 | package codegen | |
| 2 | 2 | ||
| 3 | 3 | import ( | |
| 4 | + "cmp" | ||
| 4 | 5 | "fmt" | |
| 5 | - "sort" | ||
| 6 | + "slices" | ||
| 6 | 7 | "strings" | |
| 7 | 8 | ||
| 8 | 9 | "github.com/getkin/kin-openapi/openapi3" | |
| 10 | + | ||
| 9 | 11 | "github.com/oapi-codegen/oapi-codegen/v2/pkg/util" | |
| 10 | 12 | ) | |
| 11 | 13 | ||
| 14 | + var _ fmt.Stringer = (*SchemaPath)(nil) | ||
| 15 | + | ||
| 12 | 16 | // SchemaPath represents the document location of a schema, e.g. | |
| 13 | 17 | // ["components", "schemas", "Pet", "properties", "name"]. | |
| 14 | 18 | type SchemaPath []string | |
@@ -18,6 +22,8 @@ func (sp SchemaPath) String() string { | |||
| 18 | 22 | return strings.Join(sp, "/") | |
| 19 | 23 | } | |
| 20 | 24 | ||
| 25 | + var _ fmt.Stringer = (*SchemaContext)(nil) | ||
| 26 | + | ||
| 21 | 27 | // SchemaContext identifies where in the OpenAPI document a schema was found. | |
| 22 | 28 | type SchemaContext int | |
| 23 | 29 | ||
@@ -82,13 +88,13 @@ func (sc SchemaContext) Suffix() string { | |||
| 82 | 88 | // GatheredSchema represents a schema discovered during the gather pass, | |
| 83 | 89 | // along with its document location and context metadata. | |
| 84 | 90 | type GatheredSchema struct { | |
| 85 | - Path SchemaPath | ||
| 86 | - Context SchemaContext | ||
| 87 | - Ref string // $ref string if this is a reference | ||
| 88 | - Schema *openapi3.Schema // The resolved schema value | ||
| 89 | - OperationID string // Enclosing operation's ID, if any | ||
| 90 | - ContentType string // Media type, if from request/response body | ||
| 91 | - StatusCode string // HTTP status code, if from a response | ||
| 91 | + Path SchemaPath | ||
| 92 | + Context SchemaContext | ||
| 93 | + Ref string // $ref string if this is a reference | ||
| 94 | + Schema *openapi3.Schema // The resolved schema value | ||
| 95 | + OperationID string // Enclosing operation's ID, if any | ||
| 96 | + ContentType string // Media type, if from request/response body | ||
| 97 | + StatusCode string // HTTP status code, if from a response | ||
| 92 | 98 | ParamIndex int // Parameter index within an operation | |
| 93 | 99 | ComponentName string // The component name (e.g., "Bar" for components/schemas/Bar) | |
| 94 | 100 | GoNameOverride string // x-go-name override from the component or its parent container | |
@@ -105,11 +111,13 @@ func GatherSchemas(spec *openapi3.T, opts Configuration) []*GatheredSchema { | |||
| 105 | 111 | var schemas []*GatheredSchema | |
| 106 | 112 | ||
| 107 | 113 | if spec.Components != nil { | |
| 108 | - schemas = append(schemas, gatherComponentSchemas(spec.Components)...) | ||
| 109 | - schemas = append(schemas, gatherComponentParameters(spec.Components)...) | ||
| 110 | - schemas = append(schemas, gatherComponentResponses(spec.Components)...) | ||
| 111 | - schemas = append(schemas, gatherComponentRequestBodies(spec.Components)...) | ||
| 112 | - schemas = append(schemas, gatherComponentHeaders(spec.Components)...) | ||
| 114 | + schemas = slices.Concat( | ||
| 115 | + gatherComponentSchemas(spec.Components), | ||
| 116 | + gatherComponentParameters(spec.Components), | ||
| 117 | + gatherComponentResponses(spec.Components), | ||
| 118 | + gatherComponentRequestBodies(spec.Components), | ||
| 119 | + gatherComponentHeaders(spec.Components), | ||
| 120 | + ) | ||
| 113 | 121 | } | |
| 114 | 122 | ||
| 115 | 123 | // Gather client response wrapper types for operations that will generate | |
@@ -268,10 +276,8 @@ func gatherComponentHeaders(components *openapi3.Components) []*GatheredSchema { | |||
| 268 | 276 | // `<OperationId>Response`. These don't correspond to a real schema in the | |
| 269 | 277 | // spec but they need names that don't collide with real types. | |
| 270 | 278 | func gatherClientResponseWrappers(spec *openapi3.T) []*GatheredSchema { | |
| 271 | - var result []*GatheredSchema | ||
| 272 | - | ||
| 273 | 279 | if spec.Paths == nil { | |
| 274 | - return result | ||
| 280 | + return nil | ||
| 275 | 281 | } | |
| 276 | 282 | ||
| 277 | 283 | // Collect all operations sorted for determinism | |
@@ -296,10 +302,11 @@ func gatherClientResponseWrappers(spec *openapi3.T) []*GatheredSchema { | |||
| 296 | 302 | } | |
| 297 | 303 | ||
| 298 | 304 | // Sort by operationID for determinism | |
| 299 | - sort.Slice(ops, func(i, j int) bool { | ||
| 300 | - return ops[i].op.OperationID < ops[j].op.OperationID | ||
| 305 | + slices.SortFunc(ops, func(a, b opEntry) int { | ||
| 306 | + return cmp.Compare(a.op.OperationID, b.op.OperationID) | ||
| 301 | 307 | }) | |
| 302 | 308 | ||
| 309 | + result := make([]*GatheredSchema, 0, len(ops)) | ||
| 303 | 310 | for _, entry := range ops { | |
| 304 | 311 | result = append(result, &GatheredSchema{ | |
| 305 | 312 | Path: SchemaPath{"paths", entry.path, entry.method, "x-client-response-wrapper"}, | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -3,6 +3,7 @@ package codegen | |||
| 3 | 3 | import ( | |
| 4 | 4 | "errors" | |
| 5 | 5 | "fmt" | |
| 6 | + "maps" | ||
| 6 | 7 | "strings" | |
| 7 | 8 | ||
| 8 | 9 | "github.com/getkin/kin-openapi/openapi3" | |
@@ -87,14 +88,10 @@ func mergeAllOf(allOf []*openapi3.SchemaRef) (openapi3.Schema, error) { | |||
| 87 | 88 | func mergeOpenapiSchemas(s1, s2 openapi3.Schema, allOf bool) (openapi3.Schema, error) { | |
| 88 | 89 | var result openapi3.Schema | |
| 89 | 90 | ||
| 90 | - result.Extensions = make(map[string]any) | ||
| 91 | - for k, v := range s1.Extensions { | ||
| 92 | - result.Extensions[k] = v | ||
| 93 | - } | ||
| 94 | - for k, v := range s2.Extensions { | ||
| 95 | - // TODO: Check for collisions | ||
| 96 | - result.Extensions[k] = v | ||
| 97 | - } | ||
| 91 | + result.Extensions = make(map[string]any, len(s1.Extensions)+len(s2.Extensions)) | ||
| 92 | + maps.Copy(result.Extensions, s1.Extensions) | ||
| 93 | + // TODO: Check for collisions | ||
| 94 | + maps.Copy(result.Extensions, s2.Extensions) | ||
| 98 | 95 | ||
| 99 | 96 | result.OneOf = append(s1.OneOf, s2.OneOf...) | |
| 100 | 97 | ||
@@ -195,14 +192,10 @@ func mergeOpenapiSchemas(s1, s2 openapi3.Schema, allOf bool) (openapi3.Schema, e | |||
| 195 | 192 | result.Required = append(s1.Required, s2.Required...) | |
| 196 | 193 | ||
| 197 | 194 | // We merge all properties | |
| 198 | - result.Properties = make(map[string]*openapi3.SchemaRef) | ||
| 199 | - for k, v := range s1.Properties { | ||
| 200 | - result.Properties[k] = v | ||
| 201 | - } | ||
| 202 | - for k, v := range s2.Properties { | ||
| 203 | - // TODO: detect conflicts | ||
| 204 | - result.Properties[k] = v | ||
| 205 | - } | ||
| 195 | + result.Properties = make(map[string]*openapi3.SchemaRef, len(s1.Properties)+len(s2.Properties)) | ||
| 196 | + maps.Copy(result.Properties, s1.Properties) | ||
| 197 | + // TODO: detect conflicts | ||
| 198 | + maps.Copy(result.Properties, s2.Properties) | ||
| 206 | 199 | ||
| 207 | 200 | if isAdditionalPropertiesExplicitFalse(&s1) || isAdditionalPropertiesExplicitFalse(&s2) { | |
| 208 | 201 | result.WithoutAdditionalProperties() | |
| Back | FazBrowse Home | New Git URL |
0 commit comments