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

Problems with references from external file to external or main file #304

Closed
Furion137 opened this issue Oct 21, 2016 · 16 comments
Closed
Milestone

Comments

@Furion137
Copy link

We have several POJO's we'd like to generate from yml/json using swagger-codegen. POJO's has modular structure and modules has dependencies between them. So we'd like to keep them in several json files and have "Parent" module absolutely independent, "Child1" module depending on "Parent", "Child2" depending on "Parent", "Grandchild1.1" depending on "Child1", etc. So we need inheritance and composition.
So for codegen it looks like swagger.yml with definition block filled with links to parent.json, child1.json, etc. The problem is that currently all the refs e.g. from "Child1" "./swagger.yml/#/definitions/BaseEntityDto" I've tried to use for inheritance are just ignored. And for composition it creates something like "SwaggerYmldefinitionsAddressTypeDto". Could you please take a look?

@fehguy
Copy link
Contributor

fehguy commented Oct 21, 2016

More details, please. It's really not possible to help without a sample spec

@Furion137
Copy link
Author

Furion137 commented Oct 21, 2016

All files lie in the same folder. Composition works fine. Inheritance and InheritanceAndComposition don't. ReferenceToTheSameFile doesn't work either.
I believe inheritance somehow breaks the parsing. I would assume that there is something like a variable referencing the current file and it works not that fine, especially with inheritance .
Hope it helps :-)

swagger.json

{
  "swagger": "2.0",
  "info": {
    "title": "test",
    "version": "1.0.0"
  },
  "paths": {
    "empty": null
  },
  "definitions": {
    "BaseEntityDto": {
      "$ref": "./parent.json/#/definitions/BaseEntity"
    },
    "InheritanceDto": {
      "$ref": "./child.json/#/definitions/Inheritance"
    },
    "CompositionDto": {
      "$ref": "./child.json/#/definitions/Composition"
    },
    "InheritanceAndCompositionDto": {
      "$ref": "./child.json/#/definitions/InheritanceAndComposition"
    },
    "ReferenceToTheSameFile": {
      "$ref": "./swagger.json/#/definitions/InheritanceAndCompositionDto"
    }
  }
}

parent.json

{
  "definitions": {
    "BaseEntity": {
      "type": "object",
      "discriminator": "baseEntity",
      "properties": {
        "entityId": {
          "type": "integer",
          "format": "int64"
        }
      }
    }
  }
}

child.json

{
  "definitions": {
    "Inheritance": {
      "allOf": [
        {
          "$ref": "./swagger.json#/definitions/BaseEntityDto"
        },
        {
          "type": "object",
          "properties": {
            "addressId": {
              "type": "string"
            }
          }
        }
      ]
    },
    "Composition": {
      "type": "object",
      "properties": {
        "addressId": {
          "$ref": "./swagger.json#/definitions/BaseEntityDto"
        }
      }
    },
    "InheritanceAndComposition": {
      "allOf": [
        {
          "$ref": "./swagger.json#/definitions/BaseEntityDto"
        },
        {
          "type": "object",
          "properties": {
            "addressId": {
              "$ref": "./swagger.json#/definitions/BaseEntityDto"
            }
          }
        }
      ]
    }
  }
}

build.gradle

task generateDtos(type: JavaExec) {
    description 'Generate DTO source files'
    classpath configurations.swagger_codegen
    jvmArgs '-Dmodels'
    main 'io.swagger.codegen.SwaggerCodegen'
    args 'generate',
            '-i', file("schemas/public/swagger.json").canonicalPath,
            '-l', 'java',
            '-o', file("${buildDir}/generated-sources").canonicalPath,
            '--model-package', 'com.taskdata.supplier.business.supplier.dto',
            '-DserializableModel=true',
            '-DdateLibrary=java8',
            '--library', 'feign'
}

@fehguy
Copy link
Contributor

fehguy commented Oct 27, 2016

This should be fixed in the parser now. I'll ensure the codegen gets a reference to the snapshot here.

@Furion137
Copy link
Author

Thanks a lot. Hope to try it as soon as I finish with my current task.

@fehguy fehguy modified the milestone: v1.0.23 Oct 28, 2016
@Furion137
Copy link
Author

Furion137 commented Nov 8, 2016

Now it's not parsing the example above. I get "Exception in thread "main" java.lang.RuntimeException: Unable to load RELATIVE ref: ./child.json/swagger.json". Looks like it's trying to find one file into another and I haven't found a way to workaround it.
So in swagger.json we have a link to child.json where we have a link back to swagger.json and it's not workings. Same if I try to link swagger.json->child.json->parent.json ("Exception in thread "main" java.lang.RuntimeException: Unable to load RELATIVE ref: ./child.json/parent.json"). Could you please take a look?

@webron
Copy link
Contributor

webron commented Nov 10, 2016

URIs/URLs can be a pain. Try using ../swagger.json and ../child.json and so on. When referencing the same doc, prefer # over actually using the filename.

@Furion137
Copy link
Author

Have tried. Exception in thread "main" java.lang.RuntimeException: Unable to load RELATIVE ref: ../parent.json. Gonna check further tomorrow.

@Furion137
Copy link
Author

No, ../parent.json is not working for me.
Regarding using just # for referencing inside the file I fully agree. I used the part below just to show another small issue.
"ReferenceToTheSameFile": {
"$ref": "./swagger.json/#/definitions/InheritanceAndCompositionDto"
}"

@fehguy
Copy link
Contributor

fehguy commented Nov 11, 2016

@Furion137 please ensure you're testing master. There are numerous tests to verify this working now

@Furion137
Copy link
Author

@fehguy Have rechecked, I've been using the latest version from master. Does the example above work for you?

@fehguy fehguy reopened this Nov 11, 2016
@fehguy
Copy link
Contributor

fehguy commented Nov 11, 2016

@Furion137 I will add a test right now and report back

@Furion137
Copy link
Author

@fehguy thanks a lot!

@fehguy
Copy link
Contributor

fehguy commented Nov 11, 2016

Yes, looks like a bug--working on it, but you do have some circular references that may not be possible to use. I'll know more in a few.

@Furion137
Copy link
Author

@fehguy thanks a lot!

fehguy added a commit that referenced this issue Nov 11, 2016
@fehguy
Copy link
Contributor

fehguy commented Nov 11, 2016

OK in your swagger.json note that your reference patterns look like this:

"$ref": "./parent.json/#/definitions/BaseEntity"

You have an extra slash after the parent.json in the above example (same is true for all of them). Please change to this:

"$ref": "./parent.json#/definitions/BaseEntity"

and it works fine. Can you please retest and confirm?

@Furion137
Copy link
Author

Thanks a lot! Looks like this way it works. Trying to rewrite our code this way.

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

No branches or pull requests

3 participants