JMS serializer - deserializing doctrine entity with reference by id not working - symfony

is it possible to deserialize this json object and set the "place" reference only by id in one request? :
{
"title": "some title",
"description" : "some description",
"place" : {
"id" : "5367ffcd3271d87f5c7b23cf" // mongoid to one-to-many reference object
}
}
in this case jms serializer giving me: Cannot create a DBRef without an identifier. UnitOfWork::getDocumentIdentifier() did not return an identifier for class Path\Document\Place (500 Internal Server Error)
Ok, i understand it. when i posting json object with properties like latitude, longitude and other than id everything working perfectly -> reference "child" Place object is created.
My question is -> what if i want to create reference with existing object only by indication via "id" in one single request using JMS with deserialize method?
Thanks for your help and sorry for my english.
i solved this by setting place object in #PostDeserialize action if isset place ID.

Related

Passing a dynamic where clause through Web API

I am currently in the process of creating a.NET Core Web API that is going to interact with an Angular front end. There is a requirement for the web API to be able to return data based on a dynamically built where statement. For example, I have an object with the following model
{
"accountCode": "ABC123",
"addressLine1": "AddressLine1",
"addressLine2": "AddressLine2",
"addressLine3": "AddressLine3",
"addressLine4": null,
"addressLine5": null,
"businessUnitName": "",
"companyRegistrationNumber": null,
"name": "My supplier",
"postcode": "BA112RP",
"supplierId": 3,
"vatRegistrationNumber": null
}
Now rather than having my controller method setup with 12 different parameters and needing to add an extra parameter whenever I add a new property is there a way I can have a dynamic parameter (KeyValuePair type arrangement)?
Your asp.net-core web api is able to implicitly convert input into any given Object, for example:
[Route("api/[controller]")]
[ApiController]
public class HomeController : ControllerBase
{
public IActionResult PostObject([FromBody] MyObject obj)
{
// Your Code goes here
}
}
By default this expects a JSON format but you can use others like FormData as well.
Also it mustn't be an object with the defined properties. You can use a simple KeyValuePair as well. Have a look at Parameter Binding in ASP.NET Web API where you can find additional advice on how to best solve your Problem.

Using Web API to read from SQL Server database that has related tables

I am trying to set up a Web API to be used for capturing the Clocking In and Clocking Out times of Employees after scanning a QR code.
I have set up this SQL Server database to test out the logic for Web API and IONIC 3 project.
My SQL Server code are as follows:
-- Creating table 1 'FarmWorker'
CREATE TABLE [dbo].[FarmWorker]
(
[FarmWorkerNum] int primary key identity(1,1),
[FarmWorkerFName] varchar (15) not null,
[FarmWorkerLName] varchar (15) not null,
[FarmWorkerIDNum] char (13) not null
);
GO
-- Creating table 2 'AttendenceSheet'
CREATE TABLE [dbo].[AttendenceSheet]
(
[AttendenceSheetID] int primary key identity(1,1),
[ClockInTime] datetime not null,
[ClockOutTime] datetime not null,
[FarmWorkerNum] int not null
FOREIGN KEY REFERENCES [FarmWorker](FarmWorkerNum)
);
GO
My Web API has been set up using Visual Studio 2015 and ASP.Net Web API 2.
My controller code for the Web API are as follows:
namespace AttendanceService.Controllers
{
public class FarmWorkerController : ApiController
{
public IEnumerable<FarmWorker> Get()
{
using (AttDBEntities entities = new AttDBEntities())
{
return entities.FarmWorkers.ToList();
}
}
public FarmWorker Get(int id)
{
using (AttDBEntities entities = new AttDBEntities())
{
return entities.FarmWorkers.FirstOrDefault(e =>Convert.ToInt32(e.FarmWorkerIDNum) == id);
}
}
}
}
When I run my Web API in the browser and attempt to run the first Get (to return a list of all FarmWorkers), I get the following error:
An error has occurred
System.InvalidOperationException: the 'ObjectContent`1' type failed to serialize the response body for content type 'application/xml; charset=utf-8'.
Type 'System.Data.Entity.DynamicProxies.FarmWorker_DA4B06B144222E86714953DE48C1952FFB 59C5CE216BAE8D8D506D8AEE9CEEBB' with data contract name 'FarmWorker_DA4B06B144222E86714953DE48C1952FFB59C5CE216BAE8D8D506D8AEE9CEEBB:htt p://schemas.datacontract.org/2004/07/System.Data.Entity.DynamicProxies' is not expected. Consider using a DataContractResolver if you are using DataContractSerializer or add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to the serializer.
Does anyone know how to resolve this error? Any help will greatly be appreciated.
Thank you in advance.
[EDIT / UPDATE 1] - 2 hour after posting
I have managed to resolve the error message by adding in this code into the WebApiConfig.cs file:
config.Formatters.Remove(config.Formatters.XmlFormatter);
config.Formatters.Add(config.Formatters.JsonFormatter);
config.Formatters.JsonFormatter.SerializerSettings.Formatting = Newtonsoft.Json.Formatting.Indented;
QUESTION: Why do I receive an error when it is using XML format over JSON format? If possible, I would like to use the XML format, is there any way to make this possible ?
Thanks in advance again.
My guess is that whatever you're using to call the endpoint (e.g. your browser, Fiddler, Postman), is specifying a Content-Type of application/json in the request. Try inspecting the request to see if that is the value of the header being sent.
If you really do wish to use XML, add the XML formatter again and make a request using a Content-Type of application/xml, which should return the content in XML format for you.
Solving the issue of 'ObjectContent1', I had to insert code into the WebApiConfig.cs
config.Formatters.Remove(config.Formatters.XmlFormatter);
config.Formatters.Add(config.Formatters.JsonFormatter);
config.Formatters.JsonFormatter.SerializerSettings.Formatting = Newtonsoft.Json.Formatting.Indented;

Spring Data JPA not parsing response as proper JSON string

We are using Spring MVC 4.1.2 and Spring Data JPA 1.9.0. Everything works fine but when we have custom query with only selected field for a given entity then our json response does not include property name in the response, instead it just included property value.
If I correctly guess, your custom query looks like:
SELECT e.myProperty FROM Entity e [WHERE ...]
The effect of this is that you get a List<Object[]> containing only the array of property values instead of an object that has a field with the name myProperty and its value is the value in the database.
The solution is to create a custom data-transfer object, which has this one field and assign the value in the constructor
public class MyPropertyDTO { // find a better name, though :)
private int myProperty;
public MyPropertyDTO(int myProperty) {
this.myProperty = myProperty;
}
public int getMyProperty() {
return myProperty;
}
}
Then rewrite your query as:
SELECT NEW com.mycompany.MyPropertyDTO(e.myProperty) FROM Entity e [WHERE ...]
In theory you could even use your original Entity class, add a json view on myProperty and create the matching constructor instead of creating a brand new class.

Jackson and unmarshalling Spring HATEOAS references

I have a Spring Boot REST application which also include dependencies to spring-boot-starter-data-jpa and spring-hateoas.
My issue is want to convert a JSON payload being posted to my server.
The payload contains data to create a new Product with a relation to a existing VAT Rate.
This relation is modeled using a JPA #ManyToOne like:
#NotNull
#ManyToOne(fetch = FetchType.EAGER)
private VatRate vatRate;
When I post something like:
{
"description": "bla",
"price": 99.99,
"vat_rate": {
"id": 1
}
Jackson automatically converts I can persist a new Product with a relation to the given VAT Rate.
However as I'm using HATEOAS I think I should use something like:
{
"description": "bla",
"price": 99.99,
"vat_rate": {
"id": "http://somehost/vat-rates/1"
}
But this one fails as Jackson cannot convert it.
Is there a easy solution for this? As HATEOAS is not a solid requirement in my case, if this is not possible easily it will put a to high burden to use HATEOAS.
Next to that if in first example I provide a unknown vat_rate id Boot gives me back a 500.
Reason is that Jackson references/creates a VAT Rate which does not exists and when trying to store the Product a FK violation happens resulting in the 500 due to a org.hibernate.exception.ConstraintViolationException.
I think it would be better to return a 400 pointing out that vat_rate does not exists.
But probably I have to check the existence of the VAT Rate manually before saving the Product.
In this situation, I like to use a separate DTO that's passed into my controller methods, rather than trying to reuse the JPA entity. In this scenario I'd create a ProductInput class with three properties:
class ProductInput {
private String description;
private String price;
private URI vatRate;
}
The vatRate property is a URI as it's referencing an existing VAT rate resource and, when you're using hypermedia, a URI is a resource's id.
In your controller you then need to use this URI to look up a VatRate instance. You can use UriTemplate to extract the id from the URI and then use the id to look it up. Something like this:
String idString = new UriTemplate(/vat-rates/{id})
.match(productInput.getVateRate().toASCIIString()).get("id");
long id = Long.valueOf(idString);
VatRate vatRate = vatRateRepository.findById(id);
if (vatRate == null) {
// Return an appropriate error response, probably a 400
// …
} else {
// Create and save the product
// …
}
I've got a very basic app that uses Spring HATEOAS on Github that might provide some useful further reading.

How to convert linq entitySet AND CHILDREN to lists?

I ran into an error when trying to serialize a linq entitySet. To get around this i converted the entitySet to a list. The problem I have run into now is that it's child entity sets are not converting to a list and when I try to serialize the parent those are now throwing an error. Does anyone know of a way to convert a linq entitySet AND it's children to lists?
p.s. I'm new to linq so if any of this dosn't make sense let me know
Just project onto new types:
var q = from e in Entities
select new
{
Id = e.Id,
Name = e.Name,
Children = from c in e.Children
select new
{
Id = c.Id,
Name = c.Name,
// etc.
},
// etc.
};
var s = serializer.Serialize(q);
I am guessing you are trying to serialize to XML.
Either way, the problem stems from the circular references in LINQ entity objects. Lets say you have a main table Customers with a second table Orders. The Customers entity has a reference to all of the Orders this customer has (typically Customer.Orders). The Orders entity has a reference to the customer entity (typically Order.Customer). When the serializer tries to serialize this object, it recognizes the circular reference and throws an error.
In order to do what you want, you have to write your own serializer or convert the objects to something that can be directly serialized, like custom serialization objects which contain just the information you want to show up in the serialized version.

Resources