Skip to content

segmentation fault when attempting to generate from spec that contains allOf groups #295

@rogerluan

Description

@rogerluan

Hey 👋

I've recently spent some time fighting with SwagGen because it would enter a recursion when attempting to parse a given json spec.

After much debugging, I narrowed down the problem to the presence of allOf groups in my spec.

I read a few issues and PRs in this repo, did some shallow code review in key parts of the code base to try to understand what problem the presence allOf was causing, that was triggering the recursion. Some of the issues/PRs: #221, #286, #267, #269, #217, #278. I couldn't figure out why it's crashing (entering a recursion) on my specific spec file 😬 Specially because the sample specs present in this repo do feature allOf groups, so I don't know what specific configuration in my spec files is causing this particular problem 😞

I tested with v4.3.1, v4.4.0, and v4.6.0, all of which caused the same problem (segmentation fault - a recursion somewhere).

If it helps at all, here's the stack trace (click to expand):

Line 0 Line 1 Line 2 Line 3 Line 4

Workaround

As a workaround for my specific case, since all of the allOf occurrences had a single element in the array, I replaced extracted its $ref out of the allOf key, directly in the JSON file, before parsing it using SwagGen. In Ruby, here's the script that worked for me:

# Taken from https://stackoverflow.com/a/4174125/4075379
def regex_replace_file(filename, regexp, replacement)
  require 'tempfile'
  Tempfile.open(".#{File.basename(filename)}", File.dirname(filename)) do |tempfile|
    File.open(filename).each do |line|
      tempfile.puts(line.gsub(regexp, replacement))
    end
    tempfile.close
    FileUtils.mv(tempfile.path, filename)
  end
end

swagger_spec_path = "/path/to/your/spec.json"
regexp = /"allOf":\[\{("\$ref":"#\/components\/schemas\/[a-zA-Z]+")\}\]/m
regex_replace_file(swagger_spec_path, regexp, '\1')

# swagger_spec_path now contains the processed JSON file, ready to be consumed by SwagGen

This workaround is admittedly terrible, but in my specific use case it didn't break anything. Use with caution.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions