Minimal API Results.Ok Not Returning Full Object - c#

While working on a larger Minimal API project, Results.Ok() was not returning the full object. However, switching to Results.Text() returns the full object.
I've included the full listing of a simple application that I created to see if I can reproduce the behavior. The application includes two endpoints that are supposed to return the same object.
using Newtonsoft.Json;
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
School school = new School();
school.SchoolName = "My Elementary School";
school.Students.Add(new Student() { FirstName = "Bill", LastName = "Smith", Grade = 4, StudentID = "123456" });
school.Students.Add(new Student() { FirstName = "Jane", LastName = "Doe", Grade = 5, StudentID = "54321" });
app.MapGet("/SchoolsV1", () =>
{
return Results.Ok(school);
});
app.MapGet("/SchoolsV2", () =>
{
string strValue = JsonConvert.SerializeObject(school, Formatting.Indented);
return Results.Text(strValue, "application/json", null);
});
app.Run();
public class School
{
public string SchoolName { get; set; }
public List<Student> Students = new List<Student>();
}
public class Student
{
public string StudentID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public int Grade { get; set; }
}
Using Postman I get the following results.
https://localhost:7000/SchoolsV1
{
"schoolName": "My Elementary School"
}
Then
https://localhost:7000/SchoolsV2
{
"Students": [
{
"StudentID": "123456",
"FirstName": "Bill",
"LastName": "Smith",
"Grade": 4
},
{
"StudentID": "54321",
"FirstName": "Jane",
"LastName": "Doe",
"Grade": 5
}
],
"SchoolName": "My Elementary School"
}
Is this expected behavior? Is there something I'm missing when using Results.Ok()?

Results.Ok (which is actually not needed here, you can use just app.MapGet("/SchoolsV1", () => school) uses the default System.Text.Json serializer (more info about it can be found in the docs) which does not serialize fields by default. There are several options.
Easiest one is to change School.Students to be a property:
public class School
{
public string SchoolName { get; set; }
public List<Student> Students { get; set; } = new List<Student>();
}
Or configure Microsoft.AspNetCore.Http.Json.JsonOptions:
builder.Services.Configure<JsonOptions>(opts => opts.SerializerOptions.IncludeFields = true);
var app = builder.Build();
// ...

Related

Fluent Assertions between two collection class

I have a class with collection class inside
public class SearchResult {
public int Id { get; set; }
public int Total { get; set; }
public IEnumerable<Book> Books { get; set; }
}
public class Book {
public int BookId { get; set; }
public string BookName { get; set; }
public string Publisher { get; set; }
public string ISBNCode { get; set; }
public IList<catagory> Catagories { get; set; }
}
I have a question , if I create the other object , with same structure of SearchResult and I want to copy SearchResult to SearchResultClone, which inside Books only copy BookId and BookName remain is empty.
Just like below
{
"Id": 0,
"Total": 3,
"Books": [
{
"BookId": 1,
"BookName": "Book A",
"Publisher": "",
"ISBNCode": "",
"Catagories": []
},
{
"BookId": 2,
"BookName": "Book B",
"Publisher": "",
"ISBNCode": "",
"Catagories": []
},
{
"BookId": 3,
"BookName": "Book C",
"Publisher": "",
"ISBNCode": "",
"Catagories": []
}
]
}
Event the original result have value of Publisher, ISBNCode ..etc
How to do it in LINQ ?
My second question is , if I want to make a fluent assertions as above object
var result = await sut.search(query);
result.Should().BeEquivalentTo ({the SearchResultClone })
How to write this fluent assertion ?
You need to create new instances of the classes based on the old instances:
var ans = result.Select(sr => new SearchResult {
Id = sr.Id,
Total = sr.Total,
Books = sr.Books.Select(b => new Book { BookId = b.BookId, BookName = b.BookName }).ToList()
}).ToList();
result.Should().BeEquivalentTo ({the SearchResultClone })
How to write this fluent assertion ?
If your expectation (the object you pass into BeEquivalentTo) is of the type SearchResult, then FA will try to compare the empty values of ISBN to the same property on the sut. You can solve that by doing something like:
sut.Should().BeEquivalentTo(new
{
Id = "some value",
Total = 123,
Books = new[]
{
new
{
BookId = 123,
BookName = "some book"
}
}
});

Sort List of C# object by STRING parameter [duplicate]

This question already has answers here:
C# - code to order by a property using the property name as a string [duplicate]
(10 answers)
Closed 4 years ago.
I have a list of ListUser class objects. I need to be able to pass in a String value and order by that column in ascending or descending order using text expression. Everything I have seen that uses Lambda expressions, has the object property as a strongly typed value, How can I achieve this by adding in "firstname descending" as a parameter ?
The code is as follows
namespace SortLists
{
class ListUser
{
public int id { get; set; }
public string firstname { get; set; }
public string lastname { get; set; }
public string company { get; set; }
public string phonenumber { get; set; }
}
class Program
{
static void Main(string[] args)
{
var user1 = new ListUser() { id = 1, firstname = "James", lastname = "Smith", company = "Code Logic", phonenumber = "01235 566 456" };
var user2 = new ListUser() { id = 1, firstname = "Chris", lastname = "Andrews", company = "Adobe", phonenumber = "01235 566 456" };
var user3 = new ListUser() { id = 1, firstname = "Paul", lastname = "Jones", company = "Microsoft", phonenumber = "01235 566 456" };
var user4 = new ListUser() { id = 1, firstname = "Peter", lastname = "Williams", company = "Apple", phonenumber = "01235 566 456" };
List<ListUser> users = new List<ListUser>()
{
user1, user2, user3, user4
};
}
}
Add reference to nuget package:
https://www.nuget.org/packages/System.Linq.Dynamic/
Add using System.Linq.Dynamic; at the top.
Use var usersSorted = users.AsQueryable().OrderBy("firstname ASC").ToList();
It's easy with a dictionary. Just start with this:
var sortBy = new Dictionary<string, Func<IEnumerable<ListUser>, IEnumerable<ListUser>>>()
{
{ "firstname", lus => lus.OrderBy(lu => lu.firstname) },
{ "lastname", lus => lus.OrderBy(lu => lu.lastname) },
{ "company", lus => lus.OrderBy(lu => lu.company) },
{ "phonenumber", lus => lus.OrderBy(lu => lu.phonenumber) },
};
Then you can easily sort like this:
List<ListUser> sorted = sortBy["firstname"](users).ToList();
If you want it descending just do this:
List<ListUser> sorted = sortBy["firstname"](users).Reverse().ToList();
Just structure your sort method like so:
if(stringPassed == "firstname")
{
List<ListUser> sortedListUser = listUser.OrderBy(p=>p.firstName).ToList();
}
else if(...) // and so on
if you want to order them by desc order just use LINQ's .OrderByDescending method.
The other cool approach may be that you set your properties to be objects with
string value;
string name;
and loop your input string with reflection towards the properties in your class and get the one you want and order it. It's a fancy way to impress your teacher xaxa.

json format output from JavaScriptSerializer

var singleItems = new List<Products>();
singleItems.Add(new Products() { product_id = 1, title = "Bryon Hetrick", price = 50 });
singleItems.Add(new Products() { product_id = 2, title = "Nicole Wilcox", price = 20 });
var serializer = new JavaScriptSerializer();
var serializedResult = serializer.Serialize(serializer);
From above example code i am getting Json output like bellow.
[{"product_id":1,"title":"Bryon Hetrick","price":50},
{"product_id":2,"title":"Nicole Wilcox","price":20}]
But my Json need one more value called- "config" also i need whole data formatted exactly like bellow. How to edit my c# code to achieve that value?
{ "products":[{"product_id":"B071H6TBM5","title":"New Iphone 5S","price":"23.45"},{"product_id":"B071DM968J","title":"Iphone 4 old","price":"23.45"}],"config":{"token":"","Site":"Us","Mode":"ListMyItem"}}
You could make a Config class with the properties you require and then a composite class with Prodcuts and Config, i.e. ProductConfig:
public class Products
{
public string product_id { get; set; }
public string title { get; set; }
public string price { get; set; }
}
public class Config
{
public string token { get; set; }
public string site { get; set; }
public string mode { get; set; }
}
public class ProductConfig
{
public List<Products> Products { get; set; }
public Config Config { get; set; }
}
You can then create/populate the ProductConfig class with the new properties.
public string SerializeProductConfig()
{
ProductConfig pc = new ProductConfig();
pc.Config = new Config { token = "DDTest", site = "US", mode = "Test Mode" };
pc.Products = new List<Products>();
pc.Products.Add(new Products() { product_id = "1", title = "Bryon Hetrick", price = "50" });
pc.Products.Add(new Products() { product_id = "2", title = "Nicole Wilcox", price = "20" });
var serializer = new JavaScriptSerializer();
return serializer.Serialize(pc);
}
and serialize the ProductConfig object using the JavaScript serializer or NewtonSoft which will give you the following JSON
{ // ProductConfig
"Products": [
{
"product_id": "1",
"title": "Bryon Hetrick",
"price": "50"
},
{
"product_id": "2",
"title": "Nicole Wilcox",
"price": "20"
}
],
"config": {
"token": "DDTest",
"site": "US",
"mode": "Test Mode"
}
}

How to Display the Members of a Class

I'm trying to create a wrapper for selecting multiple items from a single array. I get the result at the end of the code below. Not sure what I'm doing wrong.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Tester.cs
{
class Program
{
static void Main(string[] args)
{
var customers = new[]
{
new { CustomerID = 1, FirstName = "Orlando", LastName = "Gee",
CompanyName = "A Bike Store" },
new { CustomerID = 2, FirstName = "Keith", LastName = "Harris",
CompanyName = "Bike World" },
new { CustomerID = 3, FirstName = "Donna", LastName = "Carreras",
CompanyName = "A Bike Store" },
new { CustomerID = 4, FirstName = "Janet", LastName = "Gates",
CompanyName = "Fitness Hotel" },
new { CustomerID = 5, FirstName = "Lucy", LastName = "Harrington",
CompanyName = "Grand Industries" },
new { CustomerID = 6, FirstName = "David", LastName = "Liu",
CompanyName = "Bike World" },
new { CustomerID = 7, FirstName = "Donald", LastName = "Blanton",
CompanyName = "Grand Industries" },
new { CustomerID = 8, FirstName = "Jackie", LastName = "Blackwell",
CompanyName = "Fitness Hotel" },
new { CustomerID = 9, FirstName = "Elsa", LastName = "Leavitt",
CompanyName = "Grand Industries" },
new { CustomerID = 10, FirstName = "Eric", LastName = "Lang",
CompanyName = "Distant Inn" }
};
var addresses = new[] {
new { CompanyName = "A Bike Store", City = "New York", Country = "United States"},
new { CompanyName = "Bike World", City = "Chicago", Country = "United States"},
new { CompanyName = "Fitness Hotel", City = "Ottawa", Country = "Canada"},
new { CompanyName = "Grand Industries", City = "London", Country = "United Kingdom"},
new { CompanyName = "Distant Inn", City = "Tetbury", Country = "United Kingdom"}
};
IEnumerable<Names> customerfullName = customers.Select(data => new Names {
FirstName = data.FirstName,
LastName = data.LastName});
foreach (Names entry in customerfullName)
{
Console.WriteLine(entry);
}
}
}
class Names
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
}
Tester.cs.Names is what i get repeated when I run the program.
Console.WriteLine uses the ToString method of the object class. By default, that displays the name of the class.
This method is overridden by classes derived from object to display whatever they want. You have not overridden it, so you get the default.
You can reproduce your problem, without LINQ, as follows:
class Names
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
var name = new Names {FirstName = "John", LastName = "Saunders"};
Console.WriteLine(name); // Will display "Tester.cs.Names"
default the ToString will be used, use:
class Names
{
public string FirstName { get; set; }
public string LastName { get; set; }
public override string ToString()
{
return string.Format("{0} {1}", FirstName, LastName);
}
}
It's also possible to create an extra property for the fullname
class Names
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string FullName
{
get
{
return string.Format("{0} {1}", FirstName, LastName);
}
}
}
usage:
foreach (Names entry in customerfullName)
{
Console.WriteLine(entry.FullName);
}
Your Names class has not overridden the ToString method, so it is using the default implementation from object and printing out it's type name. You either need to override ToString in Names to print out the underlying strings, or you need to print out the individual string properties in your foreach loop.

Adding data to ObservableCollection in WPF

I have some problem here. Here it is:
I have this class
public class NewsFeedResources
{
public string Name { get; set; }
public string Id { get; set; }
public string Message { get; set; }
public static ObservableCollection<NewsFeedResources> _newsfeed = new ObservableCollection<NewsFeedResources>
{
new NewsFeedResources { Name = "Joe", Id = "1", Message="Foo" },
new NewsFeedResources { Name = "Wandy", Id = "2", Message="Bar" },
new NewsFeedResources { Name = "Yuliana", Id = "3", Message="Baz" },
new NewsFeedResources { Name = "Hardi", Id = "4", Message="Baz" },
};
public static ObservableCollection<NewsFeedResources> newsFeedResources
{ get { return _newsfeed; }
}
}
If I have another data such as
Name=John, Id=5, Message="Stack overflow"
Name=Jane, Id=6, Message="Hello world"
How can I add the data into the class, but not from the constructor? Thanks for the help
ObservableCollection exposes the Collection<T>.Add Method:
Adds an object to the end of the Collection.
So you'd have:
_newsfeed.Add(new NewsFeedResources {Name = "John",
Id = 5,
Message = "Stack overflow"});
_newsfeed.Add(new NewsFeedResources {Name = "Jane",
Id = 6,
Message = "Hello world"});
(typed from memory)
call a function from constructor or anywhere as u like and add items like below
NewsFeedResources NFR=new NewsFeedResources(){Name=John, Id=5, Message="Stack overflow"};
_newsfeed.add(NFR);
NewsFeedResources NFR1 =new NewsFeedResources(){Name=Jane, Id=6, Message="Hello world"};
_newsfeed.add(NFR);

Categories