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

Fix ExampleGenerator

Open vfpfafrf opened this issue 9 years ago • 4 comments

Fixes several issues in ExampleGenerator:

  • Incorrect float & double generation (values taken from DecimalProperty)

Definition:

 "Test" : {
      "type" : "object",
      "properties" : {
        "pFloat" : {
          "type" : "number",
          "format" : "float"
        },
        "pDouble" : {
          "type" : "number",
          "format" : "double"
        }
      }
    }

Result:

examples['application/json'] = {
  "pFloat" : 1.3579000000000001069366817318950779736042022705078125,
  "pDouble" : 1.3579000000000001069366817318950779736042022705078125
};
  • Incorrect object example generation as string, not an object:

Definition:

 "Test" : {
      "type" : "object",
      "properties" : {
        "obj" : {
          "type" : "object"
        }
      }
    }

Result:

examples['application/json'] = {
  "obj" : "{}"
};
  • ComposedModel generates empty example result

Definition:

    "Test" : {
      "allOf" : [
        {
          "$ref" : "#/definitions/TypeOne"
        },
        {
          "$ref" : "#/definitions/TypeTwo"
        }
      ]
    },
    "TypeOne" : {
      "type" : "object",
      ...
    },
    "TypeTwo" : {
      "type" : "object",
      ...
    }

Result:

  examples['application/json'] = "";

vfpfafrf avatar Jul 22 '16 07:07 vfpfafrf

@vfpfafrf thanks for the PR. I noticed that you've changed some methods from public/protected to private. Can we keep those as public/protected as there are use cases in which the developers customize the generator?

wing328 avatar Jul 22 '16 08:07 wing328

@wing328 I rollback method modifies to public/protected. May be it should be interface with abstract and default generator implementations ?

vfpfafrf avatar Jul 22 '16 09:07 vfpfafrf

May be it should be interface with abstract and default generator implementations ?

Yes, I think that's better as the same Example Generator cannot meet the syntax requirement of all languages (e.g. C# float value needs to end with "f", e.g. 1.37f)

wing328 avatar Jul 25 '16 15:07 wing328

C# float value needs to end with "f", e.g. 1.37f

I dont think this is a problem - example objects use deserialization from JSON-string - from swagger-codegen/modules/swagger-codegen/src/main/resources/aspnet5/mapReturn.mustache (or from listReturn.mustache)

var example = exampleJson != null
            ? JsonConvert.DeserializeObject<Dictionary<{{#returnType}}{{{returnType}}}{{/returnType}}>>(exampleJson)
            : new Dictionary<{{#returnType}}{{{returnType}}}{{/returnType}}>();

But the problem is in swagger-codegen/modules/swagger-codegen/src/main/resources/aspnet5/controller.mustache where exampleJson declared as null-object, so result is always empty:

string exampleJson = null;

I don't have experience in C#, so i can't fix it in right way

vfpfafrf avatar Jul 26 '16 07:07 vfpfafrf