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

Fix issue with generating docs for embedded structures with alias instead of package name #668

Merged
merged 9 commits into from May 8, 2020
Merged

Conversation

ShoshinNikita
Copy link
Contributor

Issue

For example, there's a package github.com/user/repo/common which contains type Common:

package common

type Common struct {
    ID int `json:"id"
}

If we embed this type in another struct but use an alias instead of the real package name, swag will generate wrong documentation:

package main

import (
    c "github.com/user/repo/common"
)

type Response struct {
    c.Common
    Name string `json:"name"
} 

Generated documentation:

{
    "definitions": {
        "main.Response": {
            "type": "object",
            "properties": {
                "name": {
                    "type": "string"
                }
            }
        }
    }
}

Solution

We can suppose that pkgName is a package alias, if typeSpec is nil. So, we can just iterate through import aliases and check can we get non-nil typeSpec

@codecov-io
Copy link

codecov-io commented Apr 10, 2020

Codecov Report

Merging #668 into master will increase coverage by 0.04%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #668      +/-   ##
==========================================
+ Coverage   84.40%   84.45%   +0.04%     
==========================================
  Files           7        7              
  Lines        1866     1872       +6     
==========================================
+ Hits         1575     1581       +6     
  Misses        184      184              
  Partials      107      107              
Impacted Files Coverage Δ
parser.go 80.38% <100.00%> (+0.11%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 593555a...ca3ddd8. Read the comment docs.

@sdghchj
Copy link
Member

sdghchj commented Apr 21, 2020

It is also need to be considered that the embeded field here might be a alias type, for example:

type Common []string

Since a lot of work to do, I refactored a branch refactored.
@easonlin404 What's your idea?

@sdghchj sdghchj merged commit 3ebb56e into swaggo:master May 8, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants