Null Reference Exception generating client
See original GitHub issueAutoRest fails to generate a client for the following swagger (refs point to documents on an internal server):
{
"swagger": "2.0",
"info": {
"title": "Bug Example",
"description": "A summary of a a bug",
"version": "01.00.00"
},
"schemes": [
"https"
],
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"paths": {
"/a/path": {
"get": {
"operationId": "getAPath",
"description": "a path",
"responses": {
"200": {
"description": "A positive response",
"schema": {
"$ref": "#/definitions/aModel"
}
},
"401": {
"$ref": "http://internalserver/api-governance/shared/responses/401.json"
},
"404": {
"$ref": "http://internalserver/api-governance/shared/responses/404.json"
}
}
}
}
},
"definitions": {
"aModel": {
"type": "object",
"title": "someModel",
"description": "Representation for a model",
"properties": {
"data": {
"type": "object",
"properties": {
"aModel": {
"type": "array",
"items": {
"type": "object",
"allOf": [
{
"$ref": "#/definitions/aModelAttributes"
}
]
}
}
}
},
"meta": {
"type": "object",
"description": "The metadata for the resource",
"allOf": [
{
"$ref": "http://internalserver/api-governance/shared/definitions/pagination-meta.json"
}
]
}
}
},
"aModelAttributes": {
"properties": {
"prop1": {
"type": "string"
}
}
}
}
}
The stack trace is:
Code generation failed.
AutoRest.exe -namespace MyNamespace -CodeGenerator CSharp -Input .\example.json -verbose
error: [FATAL] Error generating client model: Object reference not set to an instance of an object.
System.NullReferenceException: Object reference not set to an instance of an object.
at Microsoft.Rest.Modeler.Swagger.JsonConverters.ResponseRefConverter.ReadJson(JsonReader reader, Type objectType, Object existingValue, JsonSerializer serializer)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.DeserializeConvertable(JsonConverter converter, JsonReader reader, Type objectType, Object existingValue)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateDictionary(IDictionary dictionary, JsonReader reader, JsonDictionaryContract contract, JsonProperty containerPrope
rty, String id)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract contain
erContract, JsonProperty containerMember, Object existingValue)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract
containerContract, JsonProperty containerMember, Object existingValue)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.SetPropertyValue(JsonProperty property, JsonConverter propertyConverter, JsonContainerContract containerContract, JsonProp
erty containerProperty, JsonReader reader, Object target)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject(Object newObject, JsonReader reader, JsonObjectContract contract, JsonProperty member, String id)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract contain
erContract, JsonProperty containerMember, Object existingValue)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract
containerContract, JsonProperty containerMember, Object existingValue)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateDictionary(IDictionary dictionary, JsonReader reader, JsonDictionaryContract contract, JsonProperty containerPrope
rty, String id)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract contain
erContract, JsonProperty containerMember, Object existingValue)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract
containerContract, JsonProperty containerMember, Object existingValue)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)
at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)
at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings)
at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value, JsonSerializerSettings settings)
at Microsoft.Rest.Modeler.Swagger.JsonConverters.PathLevelParameterConverter.ReadJson(JsonReader reader, Type objectType, Object existingValue, JsonSerializer serializer)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.DeserializeConvertable(JsonConverter converter, JsonReader reader, Type objectType, Object existingValue)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)
at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)
at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings)
at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value, JsonSerializerSettings settings)
at Microsoft.Rest.Modeler.Swagger.JsonConverters.PathItemRefConverter.ReadJson(JsonReader reader, Type objectType, Object existingValue, JsonSerializer serializer)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.DeserializeConvertable(JsonConverter converter, JsonReader reader, Type objectType, Object existingValue)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateDictionary(IDictionary dictionary, JsonReader reader, JsonDictionaryContract contract, JsonProperty containerPrope
rty, String id)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract contain
erContract, JsonProperty containerMember, Object existingValue)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract
containerContract, JsonProperty containerMember, Object existingValue)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.SetPropertyValue(JsonProperty property, JsonConverter propertyConverter, JsonContainerContract containerContract, JsonProp
erty containerProperty, JsonReader reader, Object target)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject(Object newObject, JsonReader reader, JsonObjectContract contract, JsonProperty member, String id)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract contain
erContract, JsonProperty containerMember, Object existingValue)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract
containerContract, JsonProperty containerMember, Object existingValue)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)
at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)
at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings)
at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value, JsonSerializerSettings settings)
at Microsoft.Rest.Modeler.Swagger.SwaggerParser.Parse(String swaggerDocument)
at Microsoft.Rest.Modeler.Swagger.SwaggerParser.Load(String path, IFileSystem fileSystem)
at Microsoft.Rest.Modeler.Swagger.SwaggerModeler.Build()
at Microsoft.Rest.Generator.AutoRest.Generate(Settings settings)
INFO: AutoRest Core 0.16.0.0
INFO: Initializing modeler.
INFO: Initializing modeler.
INFO: Parsing swagger json file.
If the error response definitions are removed (like so):
{
"swagger": "2.0",
"info": {
"title": "Bug Example",
"description": "A summary of a a bug",
"version": "01.00.00"
},
"schemes": [
"https"
],
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"paths": {
"/a/path": {
"get": {
"operationId": "getAPath",
"description": "a path",
"responses": {
"200": {
"description": "A positive response",
"schema": {
"$ref": "#/definitions/aModel"
}
}
}
}
}
},
"definitions": {
"aModel": {
"type": "object",
"title": "someModel",
"description": "Representation for a model",
"properties": {
"data": {
"type": "object",
"properties": {
"aModel": {
"type": "array",
"items": {
"type": "object",
"allOf": [
{
"$ref": "#/definitions/aModelAttributes"
}
]
}
}
}
},
"meta": {
"type": "object",
"description": "The metadata for the resource",
"allOf": [
{
"$ref": "http://internalserver/api-governance/shared/definitions/pagination-meta.json"
}
]
}
}
},
"aModelAttributes": {
"properties": {
"prop1": {
"type": "string"
}
}
}
}
}
Then I get:
Code generation failed.
AutoRest.exe -namespace MyNamespace -CodeGenerator CSharp -Input .\example.json -verbose
error: [FATAL] Error generating client model: The given key was not present in the dictionary.
System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary.
at System.Collections.Generic.Dictionary`2.get_Item(TKey key)
at Microsoft.Rest.Modeler.Swagger.SwaggerModeler.Build()
at Microsoft.Rest.Generator.AutoRest.Generate(Settings settings)
INFO: AutoRest Core 0.16.0.0
INFO: Initializing modeler.
INFO: Initializing modeler.
INFO: Parsing swagger json file.
INFO: Generating client model from swagger model.
If I remove any refs that point to external documents, then the client object model is generated successfully.
Issue Analytics
- State:
- Created 7 years ago
- Comments:12 (7 by maintainers)
Top Results From Across the Web
NullReferenceException Class (System) - Microsoft Learn
You can recover from the exception. For example, a call to a web service that returns a reference type might return null if...
Read more >c# - What is a NullReferenceException, and how do I fix it?
Object variables that are uninitialized and hence point to nothing. In this case, if you access members of such objects, it causes a...
Read more >NullReferenceException in C# - TutorialsTeacher
The NullReferenceException is an exception that will be thrown while accessing a null object. The following example shows the code that ...
Read more >Debugging System.NullReferenceException - elmah.io Blog
NET exceptions. Today's exception is, without a doubt, the error most people have experienced: System.NullReferenceException.
Read more >Object Reference Not Set to an Instance of an Object - Stackify
This infamous and dreaded error message happens when you get a NullReferenceException. This exception is thrown when you try to access a ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Absolutely correct. Looking at your examples again, it seems I was drawing the wrong conclusions from the discussion… your Swagger is perfectly fine 👍 but using the current AutoRest, both of your examples work for me (after pointing the external URLs to some files/URLs that are accessible)!
Do you have a counterexample (with URLs that work for me - or post the referenced swagger)? 😃
@olydis Looks like
$refs
can be used other than to reference schemas although I don’t know if that includes my example. The spec seems a little confusing on where$ref
may be used - from what I can work out:$ref
to point to a response definition, which was my problem.The spec also says:
Which suggests a relatively arbitrary use of JSON refs is supported, and the official swagger editor allows it.
I guess the problem would be that it’s difficult to work out what the error is when AutoRest rejects it since other swagger tooling doesn’t highlight it. Would a better exception message be possible?