Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Support type aliases #45

Merged
merged 3 commits into from
May 14, 2024
Merged
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
5 changes: 4 additions & 1 deletion integration_test/test_data/handler/restaurants.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func GetRestaurants() {
// @param commodityGroup query string false "Use as filter.commodityGroup! Commodity Group"
// @param isDigitalScreen query string false "Use as filter.isDigitalScreen! IsDigitalScreen. Can be: true, false"
// @success 200 {object} GetPogsResponse
// @failure 400 {object} ValidationError
// @failure 400 {object} aliasValidationError
// @failure 404 {object} ErrResponse
// @failure 500 {object} ErrResponse
// @route assortment/planogram [get]
Expand All @@ -37,6 +37,9 @@ type GetPogsResponse struct {
Planograms []int `json:"planograms"`
}

// make a type alias of ValidationError
type aliasValidationError = ValidationError

type ValidationError struct {
StatusCode int `json:"statusCode" xml:"statusCode"`
Errors []error `json:"errors" xml:"errors"`
Expand Down
4 changes: 2 additions & 2 deletions integration_test/test_data/spec/expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/ValidationError"
"$ref": "#/components/schemas/aliasValidationError"
}
}
}
Expand Down Expand Up @@ -489,7 +489,7 @@
}
}
},
"ValidationError": {
"aliasValidationError": {
"type": "object",
"properties": {
"statusCode": {
Expand Down
4 changes: 2 additions & 2 deletions integration_test/test_data/spec/expected_with_pkg.json
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/github.com.parvez3019.go-swagger3.handler.ValidationError"
"$ref": "#/components/schemas/github.com.parvez3019.go-swagger3.handler.aliasValidationError"
}
}
}
Expand Down Expand Up @@ -382,7 +382,7 @@
}
}
},
"github.com.parvez3019.go-swagger3.handler.ValidationError": {
"github.com.parvez3019.go-swagger3.handler.aliasValidationError": {
"type": "object",
"properties": {
"statusCode": {
Expand Down
2 changes: 2 additions & 0 deletions parser/apis/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type parser struct {
model.Utils
schemaParser schema.Parser
operationParser operations.Parser
TypeAliases map[string]map[string]string // pkgName -> alias -> original
}

func NewParser(utils model.Utils, api *oas.OpenAPIObject, schemaParser schema.Parser) Parser {
Expand All @@ -26,6 +27,7 @@ func NewParser(utils model.Utils, api *oas.OpenAPIObject, schemaParser schema.Pa
OpenAPI: api,
schemaParser: schemaParser,
operationParser: operations.NewParser(utils, api, schemaParser),
TypeAliases: make(map[string]map[string]string),
}
}

Expand Down
19 changes: 18 additions & 1 deletion parser/apis/type_specs.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,27 @@ func (p *parser) parseTypeSpecs() error {
p.parseTypeSpecsFromPackage(astPackage, pkgName)
}
}
// After all type specifications have been parsed, resolve the type aliases
for pkgName, aliases := range p.TypeAliases {
for alias, original := range aliases {
if originalTypeSpec, ok := p.TypeSpecs[pkgName][original]; ok {
p.TypeSpecs[pkgName][alias] = originalTypeSpec
}
}
}

return nil
}

func (p *parser) parseTypeAlias(typeSpec *ast.TypeSpec, pkgName string) {
if ident, ok := typeSpec.Type.(*ast.Ident); ok {
if _, ok := p.TypeAliases[pkgName]; !ok {
p.TypeAliases[pkgName] = map[string]string{}
}
p.TypeAliases[pkgName][typeSpec.Name.String()] = ident.Name
}
}

func (p *parser) parseTypeSpecsFromPackage(astPackage *ast.Package, pkgName string) {
for _, astFile := range astPackage.Files {
p.parseTypeSpecsFromFile(astFile, pkgName)
Expand All @@ -54,11 +71,11 @@ func (p *parser) parseTypeSpecFromDeclaration(astDeclaration ast.Decl, pkgName s
}
}

// parseTypeSpecFromGenDeclaration find type declaration
func (p *parser) parseTypeSpecFromGenDeclaration(astGenDeclaration *ast.GenDecl, pkgName string) {
for _, astSpec := range astGenDeclaration.Specs {
if typeSpec, ok := astSpec.(*ast.TypeSpec); ok {
p.TypeSpecs[pkgName][typeSpec.Name.String()] = typeSpec
p.parseTypeAlias(typeSpec, pkgName)
}
}
}
Expand Down
Loading