alexjomin / openapi-parser

Simple and still naive openapi documentation generator from comments of your Go code.
18 stars 8 forks source link

Public API can't be used concurrently #46

Closed maxcleme closed 3 years ago

maxcleme commented 3 years ago

Hi,

We are experiencing issue when using the exposed API, with NewOpenAPI().Parse().

We are currently calling this statement in a loop (issue will also be raised if call concurrently), and the Parse behavior relies on a global variable call registeredSchemas, which is not cleared when NewOpenAPI() is call.

In our loop case, schemas of the first Parse are still present in the second Parse. In a concurrent scenario, schema will be mixed (in a non-deterministic way) and results will also be invalid.

I'll submit a dummy PR in order to encapsulate this type registry inside the openapi struct in order to make the whole thing thread-safe.

You can easily reproduce the behavior by running this small snippet :

package main

import (
    "io/ioutil"
    "os"

    "github.com/alexjomin/openapi-parser/docparser"
    "gopkg.in/yaml.v2"
)

type Repo struct {
    Name    string
    Path    string
    DocFile string
}

func main() {
    repos := []*Repo{
        {Name: "repo-a", DocFile: "/tmp/repo-a-doc.yaml", Path: "workspace/repo-a"},
        {Name: "repo-b", DocFile: "/tmp/repo-b-doc.yaml", Path: "workspace/repo-b"},
    }
    for _, repo := range repos {
        spec := docparser.NewOpenAPI()
        spec.Parse(repo.Path, []string{}, "vendor", false)

        specYML, err := yaml.Marshal(&spec)
        if err != nil {
            panic(err)
        }
        if err := ioutil.WriteFile(repo.DocFile, specYML, os.ModePerm); err != nil {
            panic(err)
        }

    }
}

Schema from repo-a will be written in both /tmp/repo-a-doc.yaml and /tmp/repo-b-doc.yaml.

Cheers.

maxcleme commented 3 years ago

LGTM, dummy snippet is working as intended when using latest revision.

Any idea when new tag will be available (in order to avoid using commit hash in go.mod ?)

Thanks everyone (@Sadzeih @alexjomin) for merging this so fast, cheers!

alexjomin commented 3 years ago

Tag 1.5.0 pushed!