Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions internal/test/exclude_package_go_type/cfg.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# yaml-language-server: $schema=../../configuration-schema.json
package: exclude_package_go_type
output: exclude_package_go_type.gen.go
generate:
models: true
output-options:
exclude-package-go-type: true
3 changes: 3 additions & 0 deletions internal/test/exclude_package_go_type/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package exclude_package_go_type

//go:generate go run github.com/livesession/oapi-codegen/v2/cmd/oapi-codegen -config cfg.yaml openapi.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions internal/test/exclude_package_go_type/openapi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
openapi: "3.0.1"
info:
version: 1.0.0
title: Tests exclude package go type option
paths:
/placeholder:
get:
operationId: placeholder
responses:
default:
description: placeholder
content:
application/json:
schema:
$ref: "#/components/schemas/response.Placeholder"
components:
schemas:
response.Placeholder:
x-go-embedding: true
x-go-type: Ignored
type: object
required: [name]
properties:
name:
type: string
2 changes: 2 additions & 0 deletions pkg/codegen/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,8 @@ type OutputOptions struct {
IncludeTags []string `yaml:"include-tags,omitempty"`
// Exclude operations that have one of these tags. Ignored when empty.
ExcludeTags []string `yaml:"exclude-tags,omitempty"`
// Exclude package the `x-go-type` extension when generating types from schemas
ExcludePackageGoType bool `yaml:"exclude-package-go-type,omitempty"`
// Only include operations that have one of these operation-ids. Ignored when empty.
IncludeOperationIDs []string `yaml:"include-operation-ids,omitempty"`
// Exclude operations that have one of these operation-ids. Ignored when empty.
Expand Down
18 changes: 15 additions & 3 deletions pkg/codegen/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,10 +316,22 @@ func GenerateGoSchema(sref *openapi3.SchemaRef, path []string) (Schema, error) {
if err != nil {
return outSchema, fmt.Errorf("invalid value for %q: %w", extPropGoType, err)
}
outSchema.GoType = typeName
outSchema.DefineViaAlias = true

return outSchema, nil
exludeCurrentPackage := false
currentPackage := globalState.options.PackageName
if globalState.options.OutputOptions.ExcludePackageGoType {
parts := strings.Split(typeName, ".")
if parts[0] == currentPackage {
exludeCurrentPackage = true
}
}

if !exludeCurrentPackage {
outSchema.GoType = typeName
outSchema.DefineViaAlias = true

return outSchema, nil
}
}

// Schema type and format, eg. string / binary
Expand Down