Skip to content

Commit d92daca

Browse files
authored
Prevent duplicate import (#71)
1 parent 7146b8c commit d92daca

2 files changed

Lines changed: 89 additions & 0 deletions

File tree

pkg/codegen/schema_imports.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,43 @@ type importMap map[string]goImport
4040
// We use `-` to indicate that this is a bit of a special case
4141
const importMappingCurrentPackage = "-"
4242

43+
// templateHardcodedImports lists import paths that are already present in header.tmpl.
44+
// GoImports() filters these out to avoid duplicate imports in generated code.
45+
var templateHardcodedImports = map[string]bool{
46+
"bytes": true,
47+
"compress/gzip": true,
48+
"context": true,
49+
"encoding/base64": true,
50+
"encoding/json": true,
51+
"encoding/xml": true,
52+
"errors": true,
53+
"fmt": true,
54+
"io": true,
55+
"os": true,
56+
"mime": true,
57+
"mime/multipart": true,
58+
"net/http": true,
59+
"net/url": true,
60+
"path": true,
61+
"strings": true,
62+
"time": true,
63+
"log/slog": true,
64+
"github.com/doordash-oss/oapi-codegen-dd/v3/pkg/runtime": true,
65+
"github.com/google/go-querystring/query": true,
66+
"github.com/google/uuid": true,
67+
"github.com/go-playground/validator/v10": true,
68+
}
69+
4370
// GoImports returns a slice of go import statements
4471
func (im importMap) GoImports() []string {
4572
goImports := make([]string, 0, len(im))
4673
for _, v := range im {
4774
if v.Path == importMappingCurrentPackage {
4875
continue
4976
}
77+
if v.Name == "" && templateHardcodedImports[v.Path] {
78+
continue
79+
}
5080
goImports = append(goImports, v.String())
5181
}
5282
return goImports

pkg/codegen/schema_imports_test.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Copyright 2025 DoorDash, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// 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.
10+
11+
package codegen
12+
13+
import (
14+
"testing"
15+
16+
"github.com/stretchr/testify/assert"
17+
)
18+
19+
func TestGoImports_FiltersHardcodedTemplateImports(t *testing.T) {
20+
im := importMap{
21+
"encoding/json": goImport{Path: "encoding/json"},
22+
"net/http": goImport{Path: "net/http"},
23+
"custom/pkg": goImport{Path: "custom/pkg"},
24+
"another/pkg": goImport{Name: "alias", Path: "another/pkg"},
25+
"aliased-json": goImport{Name: "myjson", Path: "encoding/json"},
26+
}
27+
28+
result := im.GoImports()
29+
30+
// encoding/json and net/http without aliases are hardcoded in header.tmpl, so they should be filtered out
31+
assert.NotContains(t, result, `"encoding/json"`)
32+
assert.NotContains(t, result, `"net/http"`)
33+
34+
// aliased hardcoded imports should still be present (user explicitly wants the alias)
35+
assert.Contains(t, result, `myjson "encoding/json"`)
36+
37+
// custom imports should still be present
38+
assert.Contains(t, result, `"custom/pkg"`)
39+
assert.Contains(t, result, `alias "another/pkg"`)
40+
assert.Len(t, result, 3)
41+
}
42+
43+
func TestGoImports_FiltersCurrentPackage(t *testing.T) {
44+
im := importMap{
45+
"-": goImport{Path: importMappingCurrentPackage},
46+
"custom/pkg": goImport{Path: "custom/pkg"},
47+
}
48+
49+
result := im.GoImports()
50+
51+
assert.Len(t, result, 1)
52+
assert.Contains(t, result, `"custom/pkg"`)
53+
}
54+
55+
func TestGoImports_EmptyMap(t *testing.T) {
56+
im := importMap{}
57+
result := im.GoImports()
58+
assert.Empty(t, result)
59+
}

0 commit comments

Comments
 (0)