Skip to content

Commit 1738186

Browse files
authored
Merge pull request #3 from livesession/feat/exclude-package-go-type
feat(exclude-package-go-type): impl
2 parents 13a6da5 + 4f328f9 commit 1738186

6 files changed

Lines changed: 61 additions & 3 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# yaml-language-server: $schema=../../configuration-schema.json
2+
package: exclude_package_go_type
3+
output: exclude_package_go_type.gen.go
4+
generate:
5+
models: true
6+
output-options:
7+
exclude-package-go-type: true
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package exclude_package_go_type
2+
3+
//go:generate go run github.com/livesession/oapi-codegen/v2/cmd/oapi-codegen -config cfg.yaml openapi.yaml

internal/test/exclude_package_go_type/exclude_package_go_type.gen.go

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
openapi: "3.0.1"
2+
info:
3+
version: 1.0.0
4+
title: Tests exclude package go type option
5+
paths:
6+
/placeholder:
7+
get:
8+
operationId: placeholder
9+
responses:
10+
default:
11+
description: placeholder
12+
content:
13+
application/json:
14+
schema:
15+
$ref: "#/components/schemas/response.Placeholder"
16+
components:
17+
schemas:
18+
response.Placeholder:
19+
x-go-embedding: true
20+
x-go-type: Ignored
21+
type: object
22+
required: [name]
23+
properties:
24+
name:
25+
type: string

pkg/codegen/configuration.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,8 @@ type OutputOptions struct {
250250
IncludeTags []string `yaml:"include-tags,omitempty"`
251251
// Exclude operations that have one of these tags. Ignored when empty.
252252
ExcludeTags []string `yaml:"exclude-tags,omitempty"`
253+
// Exclude package the `x-go-type` extension when generating types from schemas
254+
ExcludePackageGoType bool `yaml:"exclude-package-go-type,omitempty"`
253255
// Only include operations that have one of these operation-ids. Ignored when empty.
254256
IncludeOperationIDs []string `yaml:"include-operation-ids,omitempty"`
255257
// Exclude operations that have one of these operation-ids. Ignored when empty.

pkg/codegen/schema.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -316,10 +316,22 @@ func GenerateGoSchema(sref *openapi3.SchemaRef, path []string) (Schema, error) {
316316
if err != nil {
317317
return outSchema, fmt.Errorf("invalid value for %q: %w", extPropGoType, err)
318318
}
319-
outSchema.GoType = typeName
320-
outSchema.DefineViaAlias = true
321319

322-
return outSchema, nil
320+
exludeCurrentPackage := false
321+
currentPackage := globalState.options.PackageName
322+
if globalState.options.OutputOptions.ExcludePackageGoType {
323+
parts := strings.Split(typeName, ".")
324+
if parts[0] == currentPackage || len(parts) == 1 {
325+
exludeCurrentPackage = true
326+
}
327+
}
328+
329+
if !exludeCurrentPackage {
330+
outSchema.GoType = typeName
331+
outSchema.DefineViaAlias = true
332+
333+
return outSchema, nil
334+
}
323335
}
324336

325337
// Schema type and format, eg. string / binary

0 commit comments

Comments
 (0)