Skip to content

Commit 71b9f36

Browse files
committed
feat(codegen): handle shared schemas
1 parent 26bee6d commit 71b9f36

File tree

4 files changed

+220
-218
lines changed

4 files changed

+220
-218
lines changed

codegen/Makefile

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,15 @@ help: ## Show help
66

77
.PHONY: fmt
88
fmt: ## Format go files
9-
golangci-lint fmt -v
9+
golangci-lint fmt --verbose
1010

1111
.PHONY: lint
1212
lint: ## Lint go files
13-
golangci-lint run -v
13+
golangci-lint run --verbose
14+
15+
.PHONY: lint-fix
16+
lint-fix: ## Lint go files and apply auto-fixes
17+
golangci-lint run --verbose --fix
1418

1519
.PHONY: test
1620
test: ## Run tests

codegen/pkg/builder/collect.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ func (b *Builder) collectSchemas() {
132132
c.collectSchemasInResponse(op)
133133
c.collectSchemasInParams(op)
134134
c.collectSchemasInRequest(op)
135-
135+
136136
for _, schema := range c {
137137
if schema.GetReference() == schemaRef {
138138
foundSchema = schema
@@ -147,7 +147,7 @@ func (b *Builder) collectSchemas() {
147147
break
148148
}
149149
}
150-
150+
151151
if foundSchema != nil && !slices.ContainsFunc(schemasByTag["_shared"], func(sp *base.SchemaProxy) bool {
152152
return sp.GetReference() == schemaRef
153153
}) {
@@ -159,6 +159,13 @@ func (b *Builder) collectSchemas() {
159159
}
160160
}
161161

162+
// Sort shared schemas to ensure deterministic order
163+
if sharedSchemas, ok := schemasByTag["_shared"]; ok {
164+
slices.SortFunc(sharedSchemas, func(a, b *base.SchemaProxy) int {
165+
return strings.Compare(a.GetReference(), b.GetReference())
166+
})
167+
}
168+
162169
b.schemasByTag = schemasByTag
163170
}
164171

codegen/pkg/builder/out.go

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -255,12 +255,7 @@ func (b *Builder) generateResource(tagName string, paths *v3.Paths) error {
255255
}
256256

257257
func usesSecretType(writables []Writable) bool {
258-
for _, w := range writables {
259-
if writableUsesSecret(w) {
260-
return true
261-
}
262-
}
263-
return false
258+
return slices.ContainsFunc(writables, writableUsesSecret)
264259
}
265260

266261
func writableUsesSecret(w Writable) bool {
@@ -288,19 +283,15 @@ func (b *Builder) usesSharedTypes(writables []Writable, methods []*Method) (bool
288283
}
289284

290285
// Check writables for _shared references
291-
for _, w := range writables {
292-
if containsSharedRef(w) {
293-
return true, "from .. import _shared"
294-
}
286+
if slices.ContainsFunc(writables, containsSharedRef) {
287+
return true, "from .. import _shared"
295288
}
296289

297290
// Check methods for _shared references (only if methods are provided)
298-
if methods != nil {
299-
for _, m := range methods {
300-
for _, r := range m.Responses {
301-
if r.Type != "" && strings.Contains(r.Type, "_shared.") {
302-
return true, "from .. import _shared"
303-
}
291+
for _, m := range methods {
292+
for _, r := range m.Responses {
293+
if r.Type != "" && strings.Contains(r.Type, "_shared.") {
294+
return true, "from .. import _shared"
304295
}
305296
}
306297
}

0 commit comments

Comments
 (0)