Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Please let me know how can I make models for mentioned json #281

Open
shantanugupta135 opened this issue Jan 21, 2021 · 5 comments
Open

Comments

@shantanugupta135
Copy link

shantanugupta135 commented Jan 21, 2021

{
"data": [
{
"data": {
"type": "tenants",
"id": "3c3a6031-eff9-4b8b-9d8c-2a1dfc86548e",
"attributes": {
"name": "thyssenkrupp AG",
"created_by_user_id": 1,
"updated_by_user_id": null,
"deleted_by_user_id": null,
"created_at": "2020-12-28T10:48:50.000000Z",
"updated_at": null
},
"links": {
"self": ""
}
}
}

@hpawe01
Copy link
Contributor

hpawe01 commented Jan 21, 2021

Have a look at the configuration instruction, especially in the section beginning with "Then set up your models".

For example, your model would start like this:

// some imports

// ...
@JsonApiModelConfig({
    type: 'tenants'
})
export class Tenant extends JsonApiModel {

    @Attribute()
    name: string;

    @Attribute({ serializedName: 'created_by_user_id' })
    createdByUserId: number;

    // ...
}

@shantanugupta135
Copy link
Author

This is what I have made but please pay attention toward data object
{
"data": [
{
"data": {
"type": "tenants",
This part is creating problem and my model is not getting ready as desired.
import { JsonApiModelConfig, JsonApiModel, Attribute, BelongsTo, HasMany } from 'angular2-jsonapi';

@JsonApiModelConfig({
type: 'tenants',

})
export class TenantModel extends JsonApiModel {

 @Attribute() name: string | undefined;
 @Attribute() deleted_by_user_id: string | undefined;
//  @Attribute() deleted_at: string | undefined;
 @Attribute() updated_at: string | undefined;
 @Attribute() created_by_user_id: string | undefined;
 @Attribute() created_at: string | undefined; 
 @Attribute() updated_by_user_id: string | undefined;

}
This is what I have written but it is not working.
Please help me out, Thanks in advance

@hpawe01
Copy link
Contributor

hpawe01 commented Jan 21, 2021

This will not work because the response of your server is not JSON:API conform: https://jsonapi.org/format/#document-top-level. So you only have two possibilities:

  1. Fix your server to return JSON:API conform responses
  2. Change the response before handing it over to this library to make it JSON:API conform.

I would go with the solution 1.

But if this is not feasible for you, you can use a HttpInterceptor and modify the response:

An interceptor may transform the response event stream as well, by applying additional RxJS operators on the stream returned by next.handle()

You should be aware, that this will intercept all your http requests. So make sure to handle that.

Some remark to your model config: It may be a problem that your attributes have underscores instead of camelCase (see #86). If this is the case try to use it this way:

@Attribute({ serializedName: 'created_by_user_id' })
    createdByUserId: number;

@shantanugupta135
Copy link
Author

Thank you so much for your help

@hpawe01
Copy link
Contributor

hpawe01 commented Feb 16, 2021

@shantanugupta135 , nice to hear :)

Would you mind closing this issue then? Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants