{"action":"created","issue":{"url":"https://api.github.com/repos/swagger-api/swagger-codegen/issues/2114","repository_url":"https://api.github.com/repos/swagger-api/swagger-codegen","labels_url":"https://api.github.com/repos/swagger-api/swagger-codegen/issues/2114/labels{/name}","comments_url":"https://api.github.com/repos/swagger-api/swagger-codegen/issues/2114/comments","events_url":"https://api.github.com/repos/swagger-api/swagger-codegen/issues/2114/events","html_url":"https://github.com/swagger-api/swagger-codegen/pull/2114","id":133024961,"number":2114,"title":"Use static model factory methods","user":{"login":"delenius","id":7936653,"avatar_url":"https://avatars.githubusercontent.com/u/7936653?v=3","gravatar_id":"","url":"https://api.github.com/users/delenius","html_url":"https://github.com/delenius","followers_url":"https://api.github.com/users/delenius/followers","following_url":"https://api.github.com/users/delenius/following{/other_user}","gists_url":"https://api.github.com/users/delenius/gists{/gist_id}","starred_url":"https://api.github.com/users/delenius/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/delenius/subscriptions","organizations_url":"https://api.github.com/users/delenius/orgs","repos_url":"https://api.github.com/users/delenius/repos","events_url":"https://api.github.com/users/delenius/events{/privacy}","received_events_url":"https://api.github.com/users/delenius/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/swagger-api/swagger-codegen/labels/Client:%20Javascript","name":"Client: Javascript","color":"fad8c7"},{"url":"https://api.github.com/repos/swagger-api/swagger-codegen/labels/Enhancement","name":"Enhancement","color":"e5ffe5"}],"state":"closed","locked":false,"assignee":null,"milestone":{"url":"https://api.github.com/repos/swagger-api/swagger-codegen/milestones/10","html_url":"https://github.com/swagger-api/swagger-codegen/milestones/v2.1.6","labels_url":"https://api.github.com/repos/swagger-api/swagger-codegen/milestones/10/labels","id":1488305,"number":10,"title":"v2.1.6","description":"","creator":{"login":"fehguy","id":249413,"avatar_url":"https://avatars.githubusercontent.com/u/249413?v=3","gravatar_id":"","url":"https://api.github.com/users/fehguy","html_url":"https://github.com/fehguy","followers_url":"https://api.github.com/users/fehguy/followers","following_url":"https://api.github.com/users/fehguy/following{/other_user}","gists_url":"https://api.github.com/users/fehguy/gists{/gist_id}","starred_url":"https://api.github.com/users/fehguy/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/fehguy/subscriptions","organizations_url":"https://api.github.com/users/fehguy/orgs","repos_url":"https://api.github.com/users/fehguy/repos","events_url":"https://api.github.com/users/fehguy/events{/privacy}","received_events_url":"https://api.github.com/users/fehguy/received_events","type":"User","site_admin":false},"open_issues":74,"closed_issues":234,"state":"open","created_at":"2016-01-06T20:41:39Z","updated_at":"2016-02-19T07:22:12Z","due_on":null,"closed_at":null},"comments":11,"created_at":"2016-02-11T16:45:20Z","updated_at":"2016-02-19T09:56:35Z","closed_at":"2016-02-15T02:13:10Z","pull_request":{"url":"https://api.github.com/repos/swagger-api/swagger-codegen/pulls/2114","html_url":"https://github.com/swagger-api/swagger-codegen/pull/2114","diff_url":"https://github.com/swagger-api/swagger-codegen/pull/2114.diff","patch_url":"https://github.com/swagger-api/swagger-codegen/pull/2114.patch"},"body":"Fixes #2113 \r\n\r\nThe `constructFromObject` factory methods should be class methods\r\n(or \"static\" methods), not instance methods.\r\n\r\nWith this commit, ApiClient no longer calls the model constructors\r\ndirectly. Instead, it calls the new static factory method to get the\r\nnew instance. If there is no data on the top level, null is returned.\r\n\r\nIt is still possible for users to call the model constructors\r\ndirectly, of course."},"comment":{"url":"https://api.github.com/repos/swagger-api/swagger-codegen/issues/comments/186144818","html_url":"https://github.com/swagger-api/swagger-codegen/pull/2114#issuecomment-186144818","issue_url":"https://api.github.com/repos/swagger-api/swagger-codegen/issues/2114","id":186144818,"user":{"login":"demonfiddler","id":14012347,"avatar_url":"https://avatars.githubusercontent.com/u/14012347?v=3","gravatar_id":"","url":"https://api.github.com/users/demonfiddler","html_url":"https://github.com/demonfiddler","followers_url":"https://api.github.com/users/demonfiddler/followers","following_url":"https://api.github.com/users/demonfiddler/following{/other_user}","gists_url":"https://api.github.com/users/demonfiddler/gists{/gist_id}","starred_url":"https://api.github.com/users/demonfiddler/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/demonfiddler/subscriptions","organizations_url":"https://api.github.com/users/demonfiddler/orgs","repos_url":"https://api.github.com/users/demonfiddler/repos","events_url":"https://api.github.com/users/demonfiddler/events{/privacy}","received_events_url":"https://api.github.com/users/demonfiddler/received_events","type":"User","site_admin":false},"created_at":"2016-02-19T09:56:35Z","updated_at":"2016-02-19T09:56:35Z","body":"Regarding default values: since these are modelled in the YAML, the expectation is that an instance will have the specified default property value from the outset. There are two ways to achieve this:\r\n1. Set the default value explicitly in the constructor, thus creating an owned property. The disadvantage of this approach is that owned properties with the default value will be unnecessarily serialized to JSON when crossing the wire, making the payload larger than it needs to be.\r\n2. Add a prototype property with the default value, in which case the property won't be an owned property of the instance until/unless it is set elsewhere (in client code, for example).\r\n\r\nIn either case, the constructFromObject implementation would need to be smart enough to avoid explicitly setting instance properties to the value on the data object if the data object does not actually have an own property of that name. For example:\r\n\r\n```javascript\r\nC.protoype.constructFromObject = function(data) {\r\n  // Copy inherited properties.\r\n  D.prototype.constructFromObject.call(this, data);\r\n  E.prototype.constructFromObject.call(this, data);\r\n\r\n  // Copy own properties.\r\n  for (prop in C.protoype) {\r\n    if (data.hasOwnProperty(prop))\r\n      this[prop] = data[prop];\r\n  }\r\n}\r\n```\r\n\r\nAlso in either case, a mechanism to unset a named property back to its default value is required. In approach 1 above this could be implemented as a static method of the class but the implementation would be complex as it would have to account for inheritance/composition. In approach 2 this could be achieved by a provided global static helper method somewhere that accepts the instance and property name(s) as parameters - it would simply delete the instance property(ies) and thus further references would be resolved via the prototype chain. Overall I think this is the most satisfactory approach.\r\n\r\nIncidentally, in some languages (e.g., Java) there's 1.) no way to delete a property and 2.) no way for primitive values to be 'unset' since JVM defaults always apply (e.g. boolean: false, numeric: 0). JavaScript has no such limitations.\r\n\r\nRegarding composition semantics, I see two ways to resolve conflicts. The first approach is to have the validator reject any specification that contains conflicting composed properties (i.e., abort codegen). The second approach would simply allow successive definitions to override prior definitions, by post-order traversal of the allOf inheritance tree. Or one could make the behaviour a generator option (strict vs. lenient). There may be other validations that one could control via this option."}}