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

Compile Errors in project #19

Closed
bharathreddy-97 opened this issue May 15, 2017 · 22 comments
Closed

Compile Errors in project #19

bharathreddy-97 opened this issue May 15, 2017 · 22 comments

Comments

@bharathreddy-97
Copy link

I was using Swagger codegen until recently. I had encountered error. I tried to generate files from SwagGen. I downloaded the repo project and created xcodeproj using this command in terminal "swift package generate-xcodeproj".
I opened the project but i had some compile time errors in Sources/SwagGenKit/Utilities.swift

"/Users/next/Downloads/SwagGen-master/Sources/SwagGenKit/Utilities.swift:11:32: Same-type requirement makes generic parameter 'Key' non-generic"

Did i do something wrong in the installation process? I created a script file in folder and tried to run the script file but i got "Swag: command not found". Please guide me

@yonaskolb
Copy link
Owner

yonaskolb commented May 15, 2017

@bharathreddy-97 which version of Xcode are you using? This project requires Swift 3.1 (Xcode 8.3)

@bharathreddy-97
Copy link
Author

oh ok I will update Xcode(currently using 8.2.1) and check it. Thank you for the fast response

@yonaskolb
Copy link
Owner

No problem. Let me know if you have any other trouble, or if the generated API for your spec has any problems

@bharathreddy-97
Copy link
Author

bharathreddy-97 commented May 16, 2017

I updated the xcode and tried. I generated xcodeproj but where should i run the command. I tried it in terminal(where .xcodeproj is generated) but i am getting SwagGen: command not found. Am i missing something?

I tried running Swaggen project. I was getting "Must provide a valid spec" in console. Where can i pass these from? I am new so please bear with me

@yonaskolb
Copy link
Owner

yonaskolb commented May 16, 2017

Run these two commands in the root of the repo

swift build
.build/debug/SwagGen --template Templates/Swift

For the second command append your spec and destination arguments

Let me know if you have any more trouble

@bharathreddy-97
Copy link
Author

Thank you. I finally generated files but in other way to your suggestion. I opened .xcodeproj and in the main.swift i added paths in the command at the bottom. Thank you for replying.

One last thing how can i add prefix to model names like in Swagger we use "--model-name-prefix prefixName".

@bharathreddy-97
Copy link
Author

I added source folder to my project(added JSONUtilities and Alamofire to my project) but was getting some compile error in one of the classes Requests folder. Below is the error

Use of undeclared type 'UNKNOWN'

json corresponding to that api

"/v1/types": {
"get": {
"operationId": "getTypes",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"parameters": [
{
"name": "param1",
"in": "query",
"required": true,
"type": "integer",
"minimum": 1.0,
"format": "int64"
},
{
"name": "param2",
"in": "query",
"description": "Session Parameter - Branch Id",
"required": true,
"type": "integer",
"format": "int64"
},
{
"name": "param3",
"in": "query",
"description": "Session Parameter - Academic Session Id",
"required": true,
"type": "integer",
"format": "int64"
},
],
"responses": {
"200": {
"description": "successful operation",
"schema": {
"type": "array",
"items": {
"type": "string",
"enum": [
"Day",
"Session",
"Period"
]
}
},
"headers": {

        }
      }
    }
  }
}

@yonaskolb
Copy link
Owner

If you're using a the xcode project to run the command, it's best to specify the paths as scheme arguments by editing the scheme.

In which generated file is the UNKNOWN?

@yonaskolb
Copy link
Owner

There's no inbuilt model prefixing, but you can change the model.swift in the template and add the prefix there.
If you want to make the prefix dynamic you can put {{ options.modelPrefix }} in model.swift and then specify the model prefix in the options in the template.yml file or by passing it in as an argument: --option modelPrefix:MyPrefix

@yonaskolb
Copy link
Owner

Actually, thinking about it some more, just changing the model name as above wouldn't work, as all the references to it would be broken.
Another way if you want to edit the code would be to add the prefix in CodeFormatter.getSchemaType()

@bharathreddy-97
Copy link
Author

I got Unknown type in one of the generated files in Request folder. The exact line is

extension API {

public class GetTypes: APIRequest<[UNKNOWN]> {    -----> Here I am getting compile time error

@bharathreddy-97
Copy link
Author

I actually had another major issue. For one of the Swagger json when i generated the files some of the filesnames are same which is throwing compile time error in xcode. How are you generating those files(naming) in the Requests folder?

@yonaskolb
Copy link
Owner

The request names are generated using the operationId of each operation. If non exists one is generated using the method type and path (can be ugly though).
Is it possible for you to share the swagger spec you are trying to generate?

The latest 0.5.2 release may also fix something

@bharathreddy-97
Copy link
Author

Then this is the problem. In my Swagger json there are more than one operation with same operationId. What can i do in that case? Can i make use of description, summary params? Sorry I cannot give you the Swagger spec(organisation policy)

@yonaskolb
Copy link
Owner

Oh ok, that's not really to spec then, and SwagGen doesn't support it. Quoting the Swagger Spec:

Unique string used to identify the operation. The id MUST be unique among all operations described in the API. Tools and libraries MAY use the operationId to uniquely identify an operation, therefore, it is recommended to follow common programming naming conventions

@yonaskolb
Copy link
Owner

If you can't change the operationId's for some reason, you can change the code here to always automatically generate an operationId https://github.com/yonaskolb/SwagGen/blob/master/Sources/SwagGenKit/CodeFormatter.swift#L84

@bharathreddy-97
Copy link
Author

bharathreddy-97 commented May 17, 2017

I have asked my server guys to use seperate operationIds for each operation. There was a request where i have to pass a string in body but it is a enhancement right? What about that first Issue about that Type?

Just curious what is that {{ }} syntax you used for code generating? Is that syntax available in swift or what is it? I searched in the net but could not find much on it. I could only find other code-generators using it. Does it have a name or where can i learn it?

@yonaskolb
Copy link
Owner

Issue #21 is outstanding. I may be able to get around to it soon. Feel free to also create any pull requests though. Are you able to post an obfuscated version of your request with a string body?

The UNKNOWN type issue seems to be a bug where arrays of anonymous enums are not supported as return types. In this case it can't find what the name of the enum should be, which even a human would have trouble figuring out. I guess I'll just called it Enum. That should be an easier fix.

In terms of the {{ }}, see the readme. It explains that that is the https://github.com/kylef/Stencil format

@bharathreddy-97
Copy link
Author

I tried sending String in the body but I got a error saying outer most body type should be JSON type or something like that.

@yonaskolb
Copy link
Owner

Can you please share a version of your full operation spec where you send a string body. You can change the names if you like

@bharathreddy-97
Copy link
Author

bharathreddy-97 commented May 18, 2017

Just check this one. I have trimmed the json. I actually had another error but was able to solve that error by changing Request template. There was empty Body struct creating which was not conforming JSONValueEncodable and because of it body?.encode() was throwing error. I fixed it by conforming it and implementing encode in that body struct.

{
  "swagger": "2.0",
  "info": {
    "version": "v1",
    "title": "Spring Boot + Jersey + Swagger + Docker Example",
    "contact": {
      "name": "Orlando L Otero"
    }
  },
  "host": "hostUrl",
  "basePath": "/basePathUrl",
  "schemes": [
    "http",
    "https"
  ],
  "paths": {

    "/v1/somePath/someuri": {
      "post": {
        "operationId": "opId",
        "consumes": [
          "text/plain"
        ],
        "produces": [
          "application/json"
        ],
        "parameters": [
          {
            "name": "opId",
            "in": "cookie",
            "required": false,
            "type": "string"
          },
          {
            "in": "body",
            "name": "body",
            "required": false,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "default": {
            "description": "successful operation"
          }
        }
      }
    }
  },
  "definitions": {
    
  }
}

@yonaskolb
Copy link
Owner

@bharathreddy-97, just so you know the UKNOWN type for responses containing enums has been fixed in master 👍

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

2 participants