Skip to content

External ref resolve fails to resolve to same schema and creates duplicate classes #1518

Closed

Description

@jimshowalter

As reported in #1081 and
#1422, something gets confused and creates Foo.java and Foo1.java, even though there is just one foo.json model.

Those issues report that the problem has been fixed, but it has not.

In https://github.com/jimshowalter/swagger-parser-external-ref-bug there is a schema that reproduces the problem.

Run the JavaJAXRSSpecServerCodegen.java code generator on it, and you'll see that it generates a Stunts.java and Stunts1.java, but there is only stunts.json.

Also in that repo is a fix, or at least a fix that works for us. Look for "// BEGIN REFERENCE FORK:".

Activity

  1. webron commented on Jan 22, 2021

    @webron
    Contributor

    @jimshowalter which version of the parser do you use?

  2. jimshowalter commented on Jan 22, 2021

    @jimshowalter
    Author

    implementation "io.swagger.codegen.v3:swagger-codegen-generators:${swaggerCodegenGeneratorsVersion}"
    implementation "io.swagger.codegen.v3:swagger-codegen:${swaggerCodegenVersion}"
    implementation "io.swagger.parser.v3:swagger-parser:${swaggerParserVersion}"
    implementation "io.swagger.parser.v3:swagger-parser-v3:${swaggerParserVersion}"
    implementation "io.swagger.parser.v3:swagger-parser-v2-converter:${swaggerParserVersion}"
    implementation "io.swagger:swagger-parser:${swaggerParserVersion2}"
    implementation "io.swagger.parser.v3:swagger-parser-core:${swaggerParserVersion}"
    implementation "io.swagger.core.v3:swagger-models:${swaggerCoreVersion}"
    implementation "io.swagger.core.v3:swagger-annotations:${swaggerCoreVersion}"
    implementation "io.swagger.core.v3:swagger-core:${swaggerCoreVersion}"

    swaggerCoreVersion=2.1.6
    swaggerParserVersion=2.0.24
    swaggerParserVersion2=1.0.54
    swaggerCodegenVersion=3.0.24
    swaggerCodegenGeneratorsVersion=1.0.24

  3. jimshowalter commented on Feb 2, 2021

    @jimshowalter
    Author

    Were you able to reproduce the problem with those versions (which I think are current, or nearly so), and the repo I linked to?

  4. gracekarina commented on Feb 5, 2021

    @gracekarina
    Contributor

    Hi, may I ask why are you using two different kind of ref for the schemas?
    in ones you use components schemas assuming it ir will be resolve and in the other you use the relative path to the file.

    I also notice that in the main json file you don't use stunts, how ever is listed in the components->schemas section

          "oneOf": [
            {
              "$ref": "#/components/schemas/Rebato"
            },
            {
              "$ref": "#/components/schemas/Drabbet"
            },
            {
              "$ref": "#/components/schemas/Highvelds"
            },
            {
              "$ref": "#/components/schemas/Polly"
            }
          ],
          "description": "blah blah blah"
        },
        "tashotSipe": {
          "$ref": "../api.json#/components/schemas/TashotSipe"
        },
        "massage": {
          "type": "string",
          "description": "blah blah blah"
        },
        "stunts": {
          "$ref": "../api.json#/components/schemas/Stunts"
        }
    
  5. jimshowalter commented on Feb 5, 2021

    @jimshowalter
    Author

    There's nothing in the OAS spec that makes this an invalid schema.

    There's something in the implementation of the OAS parser that makes it unable to conclude that Stunts == Stunts.

    If there's a way to rewrite the schema that will produce the same output (other than fixing the duplicates), please explain how to do that, and we can use that as a workaround.

  6. gracekarina commented on Feb 5, 2021

    @gracekarina
    Contributor

    Hi @jimshowalter , I will start working on a fix for this duplication issue. Haven't found a workaround so far.

  7. self-assigned this
    on Feb 5, 2021
  8. jimshowalter commented on Feb 7, 2021

    @jimshowalter
    Author

    Thank you--the problem is in some of the most-complicated code in all of OAS!

  9. gracekarina commented on Feb 7, 2021

    @gracekarina
    Contributor

    Hi, @jimshowalter the only issue is that stunts_1 and TashotSipe_1 are being added? or is there an issue with the other schemas also being duplicated, as analemmata and reachless.

    • Is it possible for you to please create a simpler sample for me to work on, it would help the debugging, and also narrow down the real issue. thanks.
  10. jimshowalter commented on Feb 8, 2021

    @jimshowalter
    Author

    I'll try to create a simpler version, but having already reduced the original real schema to what's in the example, another pair of duplicates vanished. I'm afraid whittling it down will unconfuse the parser and the last duplicates will disappear.

  11. jimshowalter commented on Feb 13, 2021

    @jimshowalter
    Author

    Try now: https://github.com/jimshowalter/swagger-parser-external-ref-bug

    I noticed something interesting after boiling it down to essentials. If you remove the enums from stunts.json and tashotSipe.json, then it generates Stunts1.java and TashotSipe1.java, but doesn't generate Stunts.java or TashotSipe.java.

    The names are still wrong, because they shouldn't end in "1", but that's a clue.

  12. jimshowalter commented on Apr 19, 2021

    @jimshowalter
    Author

    Did the simplified example help?

  13. gracekarina commented on Apr 19, 2021

    @gracekarina
    Contributor

    Hi, @jimshowalter I had to work on another issues, but now I'm back on this one. Yes, the simplified example helped and the enum comment gave me an idea. Thanks. I hope to have a solution soon.

  14. added a commit that references this issue on May 6, 2021
  15. gracekarina commented on May 6, 2021

    @gracekarina
    Contributor

    Hi @jimshowalter #1559 here I made some changes, and seems like there are not duplication, can you please check? thanks

  16. 14 remaining items

  17. jimshowalter commented on May 13, 2021

    @jimshowalter
    Author

    Yes, that's fine. Working on the spec.

    Looking at your branch, it appears that after ignoring tests, the schema, and changes to code that only affect formatting, there is only one file with actual changes: ExternalRefProcessor.java. Can you confirm that's correct?

  18. jimshowalter commented on May 13, 2021

    @jimshowalter
    Author

    This shows the infinite recurse:

    example0.zip

  19. jimshowalter commented on May 13, 2021

    @jimshowalter
    Author

    I'll work on examples of the other problems tomorrow.

  20. jimshowalter commented on May 13, 2021

    @jimshowalter
    Author

    This one outputs:

    public Error setBad(List<CommonmodelsbadJson> bad) {

    but it should output:

    public Error setBad(List<Bad> bad) {

    Note that config.json specifies a modelName.

    example1.zip

  21. jimshowalter commented on May 13, 2021

    @jimshowalter
    Author

    This one doesn't generate anything for porchOperation.json despite it being referred to in the API schema.

    In addition, it generates:

    public CollectionItemCreationProps setFoos(CollectionsApiJsoncomponentsschemasLink foos) {

    instead of:

    public CollectionItemCreationProps setFoos(Link foos) {

    If you change the names of collectionCreationProps.json and collectionItemCreationProps.json at all, it makes it stop doing that.

    example2.zip

  22. jimshowalter commented on May 14, 2021

    @jimshowalter
    Author

    Have you been able to reproduce the problems?

  23. gracekarina commented on Jun 3, 2021

    @gracekarina
    Contributor

    Hi, @jimshowalter we have merged a solution for the duplication schema issue, and we also released parser, please let us know if this solves the issue.

  24. jimshowalter commented on Jun 15, 2021

    @jimshowalter
    Author

    I tested it by deleting the forked ExternalRefProcessor, and it fails with a bunch of errors.

  25. gracekarina commented on Jun 15, 2021

    @gracekarina
    Contributor

    That was not the only class that changed, so please do a proper test. If you find other issues I'll apreciate if you create new issues and link it to this one.

  26. jimshowalter commented on Jun 18, 2021

    @jimshowalter
    Author

    I did a comprehensive test!

    We test our code generation by starting with 40+ schemas for our services, running the generator at time T0, and checking in the resulting generated code. Then whenever we make a change to our schemas, or to our templates, or to the versions of Swagger we use, or any other change, our unit tests rerun, regenerating everything from the schemas, and fail the build if there are any diffs (including spacing and formatting changes).

    If the build fails, we review the diffs. If the diffs are expected, or unexpected but acceptable, we check in the latest generated code, which becomes our new baselines as of time T1. This cycle repeats forever whenever there is a change.

    Whenever we launch a new service, we add its schemas and baseline to the test framework.

    To test your change, I upgraded all of the Swagger versions, deleted ExternalRefProcessor, and got a bunch of diffs. The diffs are coming entirely from Swagger, because with ExternalRefProcessor no longer forked, we are using the very latest Swagger everything, which includes your parser changes.

    The zips I attached show various ways in which the latest Swagger generates code that doesn't match our baselines, with ExternalRefProcessor no longer forked.

    You should be able to easily reproduce the problems by unzipping the schemas and running the latest parser on them.

  27. regniblod commented on May 25, 2023

    @regniblod

    Hello, any updates on this?

  28. AmateurECE commented on Aug 1, 2023

    @AmateurECE
    Contributor

    I believe I'm experiencing this issue at 08a7630 with my OpenAPI 3.0.1 specification. Is there a chance I'm experiencing a regression, or has this not been fully resolved yet?

  29. codepred commented on Mar 14, 2024

    @codepred

    Any updates?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions