Skip to content

Unexpected initial token 'String' when populating object #2065

Description

@chfn

I get the following error when trying to generate .net core code from swagger specifications .yml

Newtonsoft.Json.JsonSerializationException: Unexpected initial token 'String' when populating object. Expected JSON object or array. Path 'paths['/api/acceptatie/scenario/foobar/v1'].__referencePath', line 1, position 592.

Runtime: NetCore21
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Populate(JsonReader reader, Object target)
at NJsonSchema.ExtensionDataDeserializationConverter.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 NSwag.SwaggerPathItem.SwaggerPathItemConverter.ReadJson(JsonReader reader, Type objectType, Object existingValue, JsonSerializer serializer) in C:\projects\nswag\src\NSwag.Core\SwaggerPathItem.cs:line 89
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 containerProperty, String id)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(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, JsonProperty 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.Populate(JsonReader reader, Object target)
at NJsonSchema.ExtensionDataDeserializationConverter.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 NJsonSchema.Infrastructure.JsonSchemaSerialization.FromJsonAsync[T](String json, SchemaType schemaType, String documentPath, Func2 referenceResolverFactory, IContractResolver contractResolver) at NSwag.SwaggerDocument.FromJsonAsync(String data, String documentPath, SchemaType expectedSchemaType, Func2 referenceResolverFactory) in C:\projects\nswag\src\NSwag.Core\SwaggerDocument.cs:line 187
at NSwag.SwaggerYamlDocument.FromYamlAsync(String data, String documentPath, SchemaType expectedSchemaType, Func`2 referenceResolverFactory) in C:\projects\nswag\src\NSwag.Core.Yaml\SwaggerYamlDocument.cs:line 70
at NSwag.Commands.OutputCommandBase.ReadSwaggerDocumentAsync(String input) in C:\projects\nswag\src\NSwag.Commands\Commands\OutputCommandBase.cs:line 52
at NSwag.Commands.SwaggerGeneration.FromSwaggerCommand.RunAsync() in C:\projects\nswag\src\NSwag.Commands\Commands\SwaggerGeneration\FromSwaggerCommand.cs:line 62
at NSwag.Commands.SwaggerGeneration.FromSwaggerCommand.RunAsync(CommandLineProcessor processor, IConsoleHost host) in C:\projects\nswag\src\NSwag.Commands\Commands\SwaggerGeneration\FromSwaggerCommand.cs:line 53
at NSwag.Commands.NSwagDocumentBase.GenerateSwaggerDocumentAsync() in C:\projects\nswag\src\NSwag.Commands\NSwagDocumentBase.cs:line 268
at NSwag.Commands.NSwagDocument.ExecuteAsync() in C:\projects\nswag\src\NSwag.Commands\NSwagDocument.cs:line 81
at NSwag.Commands.Document.ExecuteDocumentCommand.ExecuteDocumentAsync(IConsoleHost host, String filePath) in C:\projects\nswag\src\NSwag.Commands\Commands\Document\ExecuteDocumentCommand.cs:line 77
at NSwag.Commands.Document.ExecuteDocumentCommand.RunAsync(CommandLineProcessor processor, IConsoleHost host) in C:\projects\nswag\src\NSwag.Commands\Commands\Document\ExecuteDocumentCommand.cs:line 31
at NConsole.CommandLineProcessor.ProcessSingleAsync(String[] args, Object input)
at NConsole.CommandLineProcessor.ProcessAsync(String[] args, Object input)
at NConsole.CommandLineProcessor.Process(String[] args, Object input)
at NSwag.Commands.NSwagCommandProcessor.Process(String[] args) in C:\projects\nswag\src\NSwag.Commands\NSwagCommandProcessor.cs:line 54

Activity

  1. RicoSuter commented on Apr 1, 2019

    @RicoSuter
    Owner

    Can you provide a sample yaml spec?

  2. chfn commented on Apr 1, 2019

    @chfn
    Author
    swagger: '2.0'
    info:
      description: This is a description
      version: '0.2'
      title: xxx-service rest API
    host: 'localhost:6080'
    basePath: /
    paths:
    
      /api/acceptatie/scenario/foobar/v1:
        $ref: local/v1/scenario/foobar_v1.yml
    
    definitions:
      $ref: inheritance_definitions.yml
    
  3. RicoSuter commented on Apr 2, 2019

    @RicoSuter
    Owner

    I think this is because currently externel files must be swagger specs too (here you have a file with a path item in the root)... if you’d reference into a swagger spec’s path item with spec.yaml#paths/... it should work

    But we should support this

  4. RicoSuter commented on Apr 2, 2019

    @RicoSuter
    Owner

    Probably a duplicate of
    #1880

    Pr: #1953

  5. chfn commented on Apr 2, 2019

    @chfn
    Author

    refs doens't work indeed. I came across a tooling called json-refs it can resolve the yamls with refs into one big yaml. With that it works, but unfortunately it comes with a lot anonymous classes.

  6. Zoxive commented on Apr 18, 2019

    @Zoxive

    I'm getting this error when i have no external files. My swagger document is also generated by NSwag, and I'm trying to generate a typescript client for it.

    It does have "schema": { "$ref": "#/definitions/FlattenedUIData" } properties but all they are all defined inline.

  7. Zoxive commented on Apr 23, 2019

    @Zoxive

    Turns out my issued was caused by a bug with dotnet core preview 3..
    dotnet/aspnetcore#8837

    When i save my swagger.json file to a file first and use that it generates fine..

  8. flidr commented on Aug 19, 2019

    @flidr

    I'm getting the same error on this trivial input.

    info:
      title: Foo
      version: 1.0.0
    paths:
      /something:
        description: operation description
        get:
          responses:
            200:
              description: get description
    

    Once the description of something is removed then it works

  9. flidr commented on Aug 19, 2019

    @flidr

    Another sample (anonymized real case). When x-swagger-router-controller removed then works.
    Tried on version 12.3.1 and 13.0.5

    swagger: '2.0'
    info:
      title: foo
      version: '1.0'
    paths:
      /bar:
        x-swagger-router-controller: bar
        get:
          responses:
            '200':
              description: baz
    
  10. alexmartinezm commented on Oct 8, 2019

    @alexmartinezm

    This package becomes unusable due this bug...

  11. added a commit that references this issue on Apr 2, 2024
    998beb1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions