swagger-codegen icon indicating copy to clipboard operation
swagger-codegen copied to clipboard

[All] Still having a problem generating code w/ latest parser + external ref's

Open jaz-ah opened this issue 10 years ago • 18 comments

Description

When I use a json schema with external file references, ever since the swagger parser was moved up from 1.0.19, I get empty models throughout my generated code.

Swagger-codegen version

Trying latest code from @fehguy 's update to parser.

Swagger declaration file content or url

I tried simplifying this down to an easy reproducible problem -

FooAPI.json:

{
    "swagger": "2.0",
    "info": {
        "version": "1.0.0",
        "title": "FooAPI",
        "description": "API for all accesses to the Foo server system",
        "termsOfService": "TBD",
        "contact": {
            "name": "Foo API Team"
        },
        "license": {
            "name": "MIT"
        }
    },
    "host": "api.foo.com",
    "basePath": "",
    "schemes": [
        "https"
    ],
    "consumes": [
        "application/json"
    ],
    "produces": [
        "application/json"
    ],
    "paths": {
        "/bar": {
            "$ref":"./Bar.json#/bar"
        }
    },
    "definitions": {
        "BarSettingsRequest": {
	    "$ref":"./Bar.json#/definitions/BarSettingsRequest"
	},
        "BarData": {
	    "$ref":"./Bar.json#/definitions/BarData"
	}
    }
}

Bar.json:

{
    "bar": {
        "post": {
            "description": "posts stuff",
            "deprecated": true,
            "operationId": "enrollInBar",
            "produces": [
                         "application/json"
                         ],
            "parameters": [{
                           "name": "Authorization",
                           "in": "header",
                           "required": true,
                           "type": "string"
                           }, {
                           "name": "barRequest",
                           "in": "body",
                           "description": "The request body containing the amount",
                           "required": true,
                           "schema": {
                           "$ref": "#/definitions/BarSettingsRequest"
                           }
                           }],
            "responses": {
                "200": {
                    "schema": {
                        "$ref": "#/definitons/BarData"
                    }
                },
                "default": {
                }
            }
        }
    },
    "definitions": {
        "BarSettingsRequest": {
            "properties": {
                "stuff": {
                    "type": "integer"
                }
            },
            "required": [
                         "stuff"
                         ]
        },
        "BarData": {
            "properties": {
                "enabled": {
                    "type": "boolean"
                }
            }
        }
    }
}

when running this in swift3 for example, the output for BarData.swift looks like:

//
// BarData.swift
//
// Generated by swagger-codegen
// https://github.com/swagger-api/swagger-codegen
//

import Foundation


public typealias BarData = 

and the output of BarSettingsRequest.swift is:

//
// BarSettingsRequest.swift
//
// Generated by swagger-codegen
// https://github.com/swagger-api/swagger-codegen
//

import Foundation


public typealias BarSettingsRequest = 

whereas with the 1.0.19 parser, I get :

BarData.swift:

//
// BarData.swift
//
// Generated by swagger-codegen
// https://github.com/swagger-api/swagger-codegen
//

import Foundation


open class BarData: JSONEncodable {
    public var enabled: Bool?

    public init(enabled: Bool?=nil) {
        self.enabled = enabled
    }

    // MARK: JSONEncodable
    func encodeToJSON() -> Any {
        var nillableDictionary = [String:Any?]()
        nillableDictionary["enabled"] = self.enabled
        let dictionary: [String:Any] = APIHelper.rejectNil(nillableDictionary) ?? [:]
        return dictionary
    }
}

BarSettingsRequest.swift:

//
// BarSettingsRequest.swift
//
// Generated by swagger-codegen
// https://github.com/swagger-api/swagger-codegen
//

import Foundation


open class BarSettingsRequest: JSONEncodable {
    public var stuff: Int

    public init(stuff: Int) {
        self.stuff = stuff
    }

    // MARK: JSONEncodable
    func encodeToJSON() -> Any {
        var nillableDictionary = [String:Any?]()
        nillableDictionary["stuff"] = self.stuff.encodeToJSON()
        let dictionary: [String:Any] = APIHelper.rejectNil(nillableDictionary) ?? [:]
        return dictionary
    }
}

jaz-ah avatar Oct 31 '16 20:10 jaz-ah

@fehguy I was hoping your parser update to 1.0.23-SNAPSHOT would help but it still produces the issue above - 1.0.19 was the last version of the parser that worked for me. Is everything valid with my json definitions above?

jaz-ah avatar Oct 31 '16 20:10 jaz-ah

@jaz-ah it looks valid--are you sure you're testing from master on swagger-codegen to pick up the correct parser?

fehguy avatar Oct 31 '16 21:10 fehguy

@fehguy yup definitely - here is the bottom of pom.xml from latest master that i'm using:

    <properties>
        <swagger-parser-version>1.0.23-SNAPSHOT</swagger-parser-version>
        <scala-version>2.11.1</scala-version>
        <felix-version>2.3.4</felix-version>
        <swagger-core-version>1.5.10</swagger-core-version>
        <commons-io-version>2.4</commons-io-version>
        <commons-cli-version>1.2</commons-cli-version>
        <junit-version>4.8.1</junit-version>
        <maven-plugin-version>1.0.0</maven-plugin-version>
        <commons-lang-version>3.4</commons-lang-version>
        <slf4j-version>1.7.12</slf4j-version>
        <scala-maven-plugin-version>3.2.1</scala-maven-plugin-version>
        <jmustache-version>1.12</jmustache-version>
        <testng-version>6.9.6</testng-version>
        <surefire-version>2.19.1</surefire-version>
        <jmockit-version>1.25</jmockit-version>
        <reflections-version>0.9.10</reflections-version>
    </properties>

jaz-ah avatar Oct 31 '16 21:10 jaz-ah

same happens w/ swift and android languages...

jaz-ah avatar Oct 31 '16 21:10 jaz-ah

OK I have reproduced this. Looks like the issue is having relative definitions in a remote path reference, which isn't something that was tested in 1.0.23 of the parser. I'll open a new issue and hopefully it's another easy fix.

PS you have definitions spelled incorrectly in your Bar.json file.

fehguy avatar Nov 01 '16 15:11 fehguy

no problem thx @fehguy

jaz-ah avatar Nov 01 '16 16:11 jaz-ah

Hi @jaz-ah I think this is fixed. Please pull master, which grabs swagger-parser-1.0.24-SNAPSHOT. Your test files generate and compile fine now.

fehguy avatar Nov 02 '16 20:11 fehguy

ok i'll give it a try in a minute - thx!

jaz-ah avatar Nov 02 '16 20:11 jaz-ah

great, thanks

fehguy avatar Nov 02 '16 21:11 fehguy

hmm.. @fehguy for some reason i checked out master, verified pom.xml is using 1.0.24-SNAPSHOT, rebuilt and I still see the same problem... i cleaned/etc... then tried switching pom.xml to 1.0.19 and verified the problem went away... is snapshot definitely updated to the latest fix?

jaz-ah avatar Nov 02 '16 22:11 jaz-ah

I've redeployed the snapshot. Can you run mvn clean package -U on the top-level codegen project?

fehguy avatar Nov 02 '16 23:11 fehguy

@fehguy ok did that, tried again and same issue... // // BarData.swift // // Generated by swagger-codegen // https://github.com/swagger-api/swagger-codegen //

import Foundation

public typealias BarData =

are you getting the same for BarData.swift if you generate swift3?

jaz-ah avatar Nov 03 '16 00:11 jaz-ah

Here's my output:

//
// BarData.swift
//
// Generated by swagger-codegen
// https://github.com/swagger-api/swagger-codegen
//

import Foundation


open class BarData: JSONEncodable {
    public var enabled: Bool?

    public init() {}

    // MARK: JSONEncodable
    open func encodeToJSON() -> Any {
        var nillableDictionary = [String:Any?]()
        nillableDictionary["enabled"] = self.enabled
        let dictionary: [String:Any] = APIHelper.rejectNil(nillableDictionary) ?? [:]
        return dictionary
    }
}

I built it like this (serving your test files on localhost:8000):

java -jar ./modules/swagger-codegen-cli/target/swagger-codegen-cli.jar generate -i http://localhost:8080/swagger.json -o test -l swift3

fehguy avatar Nov 03 '16 00:11 fehguy

@fehguy the difference is I'm not serving test files on my localhost, I'm referencing them directly:

java -jar ./modules/swagger-codegen-cli/target/swagger-codegen-cli.jar generate -i ../activehours_apis-jaz/spec-server/public/specs/FooAPI.json -o test -l swift3

would this make a difference?

jaz-ah avatar Nov 03 '16 02:11 jaz-ah

@jaz-ah do you work at Active Hours? If so, our office is 1/2 block from you and we can probably sort this out in person :)

It works fine from file-system for me:

java -jar ~/dev/projets/swagger-api/swagger-codegen/modules/swagger-codegen-cli/target/swagger-codegen-cli.jar generate -i ./jaz/swagger.json -o foo -l swift3
$ cat foo/SwaggerClient/Classes/Swaggers/Models/BarData.swift 
//
// BarData.swift
//
// Generated by swagger-codegen
// https://github.com/swagger-api/swagger-codegen
//

import Foundation


open class BarData: JSONEncodable {
    public var enabled: Bool?

    public init() {}

    // MARK: JSONEncodable
    open func encodeToJSON() -> Any {
        var nillableDictionary = [String:Any?]()
        nillableDictionary["enabled"] = self.enabled
        let dictionary: [String:Any] = APIHelper.rejectNil(nillableDictionary) ?? [:]
        return dictionary
    }
}

fehguy avatar Nov 03 '16 15:11 fehguy

@fehguy yup I do - where are you @?

jaz-ah avatar Nov 03 '16 16:11 jaz-ah

437 kipling 2nd floor. If you want to come by, we can work this out in person

fehguy avatar Nov 03 '16 16:11 fehguy

I believe this has been addressed. Please reply to let us know if that's not the case.

wing328 avatar Feb 17 '17 06:02 wing328