swagger-api / swagger-codegen

swagger-codegen contains a template-driven engine to generate documentation, API clients and server stubs in different languages by parsing your OpenAPI / Swagger definition.
http://swagger.io
Apache License 2.0
16.9k stars 6.03k forks source link

Model with name Map/List is ignored #3223

Closed lsarni closed 8 years ago

lsarni commented 8 years ago
Description

I have this json that defines a model called "Map". I noticed that both in java and csharp this gets ignored when generating the code, I'm building a generator for a personalized language and it also gets ignored. What I did was changing the name to "Mapa", and the problem is solved. But if I name it "List" I get the same problem, so it seems to be related to names that exist as types (like tony tam guessed here)

Swagger declaration file content or url
```json
"Map": {
  "description": "Typically, the map format would be consumed by Tradeoff Analytics JavaScript widget.",
  "required": [
    "anchors",
    "nodes"
  ],
  "properties": {
    "anchors": {
      "description": "A representation of the vertices for the objectives and their positions on the map visualization.",
      "type": "array",
      "items": {
        "$ref": "#/definitions/Anchor"
      }
    },
    "nodes": {
      "description": "A cell on the map visualization. Each cell in the array includes coordinates that describe the position on the map of the glyphs for one or more listed options, which are identified by their keys. The structure of an array element is `{\"coordinates\": {\"x\": 0, \"y\": 0}, \"solution_refs\": [\"key1\", \"key3\"]}`, where `coordinates` describe the position on the map visualization of options identified by the keys in `solution_refs`.",
      "type": "array",
      "items": {
        "$ref": "#/definitions/MapNode"
      }
    }
  }
},

##### Command line used for generation

java -jar modules/swagger-codegen-cli/target/swagger-codegen-cli.jar generate -i https://watson-api-explorer.mybluemix.net/listings/tradeoff-analytics-v1.json -l csharp -o outputC

java -jar modules/swagger-codegen-cli/target/swagger-codegen-cli.jar generate -i https://watson-api-explorer.mybluemix.net/listings/tradeoff-analytics-v1.json -l java -o outputJ
wing328 commented 8 years ago

@daisymoon thanks for reporting the issue. I think it's due to the following:

https://github.com/swagger-api/swagger-codegen/blob/master/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/AbstractCSharpCodegen.java#L499

May I know if you've cycle to look into the issue and ideally submit a PR with the fix?

lsarni commented 8 years ago

I decided to try all of the languages with this yaml and got the following results:

Those where the results, the ones that don´t have a comment generated the model for ObjectForTest but not for List or Map. Here is the code that each one generated

@wing328 do you still think the problem might be related to AbstractCSharpCodegen?

lsarni commented 8 years ago

I´ve been looking at the code and the problem seems to be here Running the yaml I added this lines to see if it was here where the models where getting lost:

if (config.importMapping().containsKey(name)) {
   LOGGER.warn(name);
   continue;
}
LOGGER.info(name);

And got this as a result:

[main] WARN io.swagger.codegen.DefaultGenerator - Map
[main] WARN io.swagger.codegen.DefaultGenerator - List
[main] INFO io.swagger.codegen.DefaultGenerator - ObjectForTest

So that´s where the problem seems to be.

The values for importMapping are stated here in DefaultCodegen.

And I checked why this isn´t a problem in some of the languages. They all seem to be either: removing List and Map from the typeMapping values, creating a new typeMapping object or clearing the typeMapping object.

I think there is more than one way in which this could be solved:

Any of this options will have effects on how the code is generated that might not be what you are looking for so i´m not sure which one would be the best fix.

wing328 commented 8 years ago

@daisymoon thanks for the detailed findings. I do not have an answer on which one is the best way to fix the issue.

I'll take a deeper look after the 2.2.0 stable release.

jimschubert commented 8 years ago

@wing328 @daisymoon I think we can re-initialize the importMappings hash for AbstractCSharpCodegen. Those java-y mappings aren't relevant for .NET types.

The swagger-codegen-cli module allows for an import-mappings switch, which is applied to the CodegenConfigurator instance. The CodedgenConfigurator instantiates the relevant C# generator based on other parameters passed to the tool, and uses the DefaultGenerator type for generation of all generators (this is where the import mappings are consumed). We don't really do imports in the C# code the way Java does.

So, my proposal for this issue is to clear the importMappings for C# generators in AbstractCSharpCodegen then open another issue to add support for the import-mappings switch to C# generators (at least csharp, nancyfx, and aspnet5)

wing328 commented 8 years ago

@jimschubert thanks for the suggestion.

@daisymoon I'll take a look tomorrow (Sunday) or Monday when I've time. Hopefully I'll have a PR to fix the issue.

wing328 commented 8 years ago

@daisymoon I've filed #3454 to fix the issue using Jim's proposal.

For Java-related generators, we'll file another PR to address the issue later.