go-openapi / spec

openapi specification object model

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Why does spec.IsCircular's behavior depend on basePath?

elakito opened this issue · comments

I am wondering why IsCirclar method in the following line skips the check when basePath is not set?
https://github.com/go-openapi/spec/blob/master/expander.go#L628

func isCircular(ref *Ref, basePath string, parentRefs ...string) bool {
	return basePath != "" && swag.ContainsStringsCI(parentRefs, ref.String())
}

That means, if I load a swagger file with circular definitions from a file using loads.Spec, as in

doc, _ := loads.Spec(filepath)
doc.Expanded()

That will work because the filepath will be used as the above basePath in the circular check.

But if I have a swagger file in memory and load it using loads.Analyzed, as in

doc, _  := loads.Analyzed(rawspec, "")
doc.Expanded()

This will not work as the empty basePath is passed to the above check and the circle will not be detected.

I could avoid this problem by adding the expansion option explicitly as in

doc, _  := loads.Analyzed(rawspec, "")
doc.Expanded(&spec.ExpandOptions{RelativeBase: "/inmemory"})

But if IsCircular did not omit the check even if basePath is empty, I wouldn't need to pass the expansion option.

Could someone tell me the reason for this basePatch nil check?

regards, aki

I don't understand either and there is no test to demonstrate the usefulness of this.
I'll patch for circular resolution but leave this one unchanged.
@pytlesk4 might shed some light on this.

I think we should remove that check, doesn't really make sense that a basePath has to be set for there to be a circular ref.

Thanks Thomas. I am going to patch this tonight. I'll remove the check then.

You're welcome to review the forthcoming PR on this circular $ref check (see other related issues that this PR is supposed to fix).

The general idea is that the current circular stuff does not work when a ref is involved in several cycles. Basically, my fix consists in remembering previous circular refs and shortcut.

This works, but I am going to wrap it up more cleanly tonight that with a global map that was just here for debug. It also fixes go-swagger/go-swagger#957.

Sounds good, I would def like to check it out. Dealing with refs is a pain to say the least.