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

Hang when expanding circular $ref #79

Merged
merged 1 commit into from
Jul 8, 2018

Conversation

fredbi
Copy link
Member

@fredbi fredbi commented Jul 7, 2018

This PR is not fully satisfying in the sense that it does not fix once and for all the
circular ref story. It improves spec expansion which no more hangs on some complex
circular ref patterns. However, it is not able to produce stable expanded schemas when it
used to hang.

All produced expansions are correct, but may vary in the way they expand cycles,
when several cycles do collide. Since we walk the spec at random and this spec is mutating
during the expansion process, we stand no chance to come out with a clean resolution
(i.e. resolve shortest cycles first)

What it does:

Even though you chose not to merge this, and maybe rightly so, there are some advances with
this problem, like a minimal reproducible test case.

Cheers,

Fred

@@ -224,19 +235,23 @@ func init() {
func defaultSchemaLoader(
root interface{},
expandOptions *ExpandOptions,
cache ResolutionCache) (*schemaLoader, error) {
cache ResolutionCache,
context *resolverContext) (*schemaLoader, error) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

defaultSchemaLoader - result 1 (error) is always nil

@codecov
Copy link

codecov bot commented Jul 7, 2018

Codecov Report

Merging #79 into master will increase coverage by 0.69%.
The diff coverage is 78.72%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #79      +/-   ##
==========================================
+ Coverage   40.27%   40.96%   +0.69%     
==========================================
  Files          18       19       +1     
  Lines        1959     1982      +23     
==========================================
+ Hits          789      812      +23     
  Misses        995      995              
  Partials      175      175
Impacted Files Coverage Δ
debug.go 100% <100%> (ø)
expander.go 55.59% <74.35%> (+1.14%) ⬆️

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 16284f9...af4c70f. Read the comment docs.

* Fixes go-openapi#76 (absolute path left behind in resolved circular $ref)
* Fixes go-openapi#74 (isCircular no more checks on basePath!="")
* Contributes go-openapi#75 (hang is solved, but the (always correct) expanded result may differ from one run to another)
* Contributes go-swagger/go-swagger#957 (hang on circular spec)

No other way to pass golangci than disabling some linters
@pytlesk4
Copy link
Member

pytlesk4 commented Jul 8, 2018

This looks good to me, I am going to check out your changes and test this against some problematic specs we run into at Stoplight.

Copy link
Member

@pytlesk4 pytlesk4 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fredbi you are the man! This is awesome, can't wait to get this fix into Prism.

@fredbi
Copy link
Member Author

fredbi commented Jul 8, 2018

How about the unstable aspect of this? I mean, if you expand to validate, this is okay (all cycles are expanded in a somewhat correct way), but if you want, say to get documentation, that's no good.

Is that okay with you?

@pytlesk4
Copy link
Member

pytlesk4 commented Jul 8, 2018

For our use case at Stoplight and Prism, that is fine. We are expanding the spec to do contract testing of request/responses. It would take a bigger refactor to make it stable imo, and it is just nice that it doesn't hang anymore.

@casualjim casualjim merged commit b79fccf into go-openapi:master Jul 8, 2018
@fredbi fredbi deleted the fix-circular branch July 8, 2018 19:49
@fredbi
Copy link
Member Author

fredbi commented Jul 8, 2018

We have a problem with that one.

When I attempted to integrate with go-swagger, I could see that this breaks validate.

Validate expands JSON-schema meta schema and this is broken.

@casualjim
Copy link
Member

I reverted this one

return ref
}
// strip relativeBase from URI
r, _ := NewRef(strings.TrimPrefix(ref.String(), relativeBase))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line is causing the issue in the validation test. It is creating an invalid JSON Ref, if I change it to this:

  // strip relativeBase from URI
	relativeBaseURL, _ := url.Parse(relativeBase)
	relativeBaseURL.Fragment = ""
	r, _ := NewRef(strings.TrimPrefix(ref.String(), relativeBaseURL.String()))

Then all of the test in the validate package pass except one. It should be really easy to fix, I have to run, but will try to get to it later if someone else hasn't.

@pytlesk4 pytlesk4 mentioned this pull request Jul 9, 2018
@fredbi
Copy link
Member Author

fredbi commented Jul 9, 2018

I am fixing this

@fredbi
Copy link
Member Author

fredbi commented Jul 9, 2018

@pytlesk4 . Oh just realized you push a fix yourself. I am trying to test it with complete go-swagger integration then

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
4 participants