Skip to content

Exception when generating Postman collection for request bodies containing a property of type IList<string> #38

@Aleksandr-Khvalov

Description

@Aleksandr-Khvalov

When generating postman collection, an exception occurs if a controller request body contains a property of type IList<string>. The issue arises because the code attempts to instantiate string elements using Activator.CreateInstance, which is not supported for the string type.

Error Message

Unhandled exception. System.MissingMethodException: Cannot dynamically create an instance of type 'System.String'. Reason: Uninitialized Strings cannot be created.
   at System.RuntimeType.ActivatorCache..ctor(RuntimeType rt)
   at System.RuntimeType.IGenericCacheEntry`1.CreateAndCache(RuntimeType type)
   at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean wrapExceptions)
   at Simplify.Web.Postman.Assembly.Collection.PartBuilders.RequestBuilder.InitializeListsSingleEmptyValues(Object model)
   at Simplify.Web.Postman.Assembly.Collection.PartBuilders.RequestBuilder.BuildRequestJsonData(Type modelType)
   at Simplify.Web.Postman.Assembly.Collection.PartBuilders.RequestBuilder.TryBuildBody(IControllerMetadata metaData)
   at Simplify.Web.Postman.Assembly.Collection.PartBuilders.RequestBuilder.Build(IControllerMetadata metaData, KeyValuePair`2 route)
   at Simplify.Web.Postman.Assembly.Collection.PartBuilders.CollectionItemsBuilder.BuildRequestCollectionItem(IControllerMetadata metaData, KeyValuePair`2 route)
   at Simplify.Web.Postman.Assembly.Collection.PartBuilders.CollectionItemsBuilder.Build(CollectionModel model)
   at Simplify.Web.Postman.Assembly.Collection.CollectionBuilder.Create()
   at Simplify.Web.Postman.Generators.FileBasedCollectionGenerator.Generate()
   at Simplify.Web.Postman.Generators.FileBasedPostmanGenerator.Generate()
   at Simplify.Web.Postman.Setup.SimplifyDIContextHandlerExtensions.GeneratePostmanData(IDIContextHandler contextHandler)

Steps to Reproduce

  1. Create a controller with a request model containing a IList<string> property.
  2. Attempt to generate a Postman collection.
  3. Observe the MissingMethodException during collection generation.

Expected Behavior

The package should handle IList<string> and string properties gracefully, initializing string elements as empty strings or with a suitable default value, so that Postman collections can be generated for all valid request models without exceptions.

Solution

The fix involves:

  • Checking for string types before attempting to use Activator.CreateInstance.
  • Returning string.Empty for string types instead of using the activator.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    Status

    Closed

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions