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

Not able to find relationship id when order of objects is changed in the POST and PATCH requests #1276

Closed
prateekagrwl05 opened this issue Apr 23, 2020 · 19 comments

Comments

@prateekagrwl05
Copy link

prateekagrwl05 commented Apr 23, 2020

Expected Behavior

Hi Team,
I am getting a very weird issue while POST and PATCH (BULK) request. When the relationship order is changed in the JSON request, it is not able to find the id of the object (can be any of the three) in relationship with. Is it the correct behavior? Does the order of relationship matters in the request?

{
	"data": {
		"type": "metadata",
		"relationships": {
			"ast" : {
					"data": {
						"id": "9223372036854775793",
						"type": "asset"
					}
				},
				"txnItm": {
					"data": {
						"id": "dispute1",
						"type": "taxonomyItem"
					}
				},
				"txnGrp": {
					"data": {
						"id": "locale1",
						"type": "taxonomy"
					}
				}
		}
	}
}
@Include(rootLevel = true, type="metadata")
@ReadPermission(expression = "Prefab.Role.All")
@UpdatePermission(expression = "Prefab.Role.All")
@CreatePermission(expression = "Prefab.Role.All")
@SharePermission
class Metadata {

@Id
@GeneratedValue(....)
private Long id;

@ManyToOne
@JoinColumn(name= "ast_id")
private Asset ast;

@ManyToOne
@JoinColumn(name= "grp_cd")
private Taxonomy txnGrp;

@ManyToOne
@JoinColumn(name= "item_cd")
private TaxonomyItem txnItem;

}
@Include(rootLevel = true, type="asset")
@ReadPermission(expression = "Prefab.Role.All")
@UpdatePermission(expression = "Prefab.Role.All")
@CreatePermission(expression = "Prefab.Role.All")
@SharePermission
class Asset {

@Id
@GeneratedValue(....)
private Long id;

@Column(name= "name")
private String name;

@OneToMany
@JoinColumn(name= "ast_id")
private Set<Metadata> mtda;

}
@Include(rootLevel = true, type="taxonomy")
@ReadPermission(expression = "Prefab.Role.All")
@UpdatePermission(expression = "Prefab.Role.All")
@CreatePermission(expression = "Prefab.Role.All")
@SharePermission
class Taxonomy {

@Id
@GeneratedValue(....)
private Long id;

@Column(name= "name")
private String name;

@OneToMany
@JoinColumn(name= "grp_cd")
private Set<Metadata> mtda;

}

Steps to Reproduce (for bugs)

  1. If txnGrp is provided first, it is not able to find the id of one or two of the relationships. And this is a random behavior. If the request is sent in the above order, then it works fine.

Your Environment

  • Elide version used: elide-spring-boot-starter:4.6.0
  • Environment name and version (Java 1.8.0_152): 1.8.0_151
  • Operating System and version: MAC OSX
@prateekagrwl05
Copy link
Author

Just to add, I am seeing a pattern here. Put all relationships in order of what columns are defined in the database. And then, put the last relationship (as per column order) at second last position. For eg. If my DB column order is ast, txnGrp, txnItm, then relationship order should be ast, txnItm, txnGrp

@aklish
Copy link
Member

aklish commented Apr 24, 2020

Can you post the error you are seeing along with the full JSON body of the request as well as the HTTP URL?

@prateekagrwl05
Copy link
Author

prateekagrwl05 commented Apr 24, 2020

The JSON body pasted above is the complete request. I don't have any attributes in the class. It has only 3 relations as present in the request.
Request:

{
	"data": {
		"type": "metadata",
		"relationships": {
			"ast" : {
					"data": {
						"id": "9223372036854775793",
						"type": "asset"
					}
				},
                                 "txnGrp": {
					"data": {
						"id": "locale1",
						"type": "taxonomy"
					}
				},
				"txnItm": {
					"data": {
						"id": "dispute1",
						"type": "taxonomyItem"
					}
				}
		}
	}
}

The response that I get is

{
      "errors": [
          "TransactionException: could not execute statement"
      ]
}

When I see in my logs, it says that it inserting a null value for a relationship, and since I have NOT NULL defined at the DB level, the insert statement fails.

Caused by: org.postgresql.util.PSQLException: ERROR: null value in column "item_cd" violates not-null constraint
  Detail: Failing row contains (9223372036854775775, 9223372036854775793, locale2, null).

Request URL: http://localhost:8080/content/metadata
Request Headers: Content-type: application/vnd.api+json Accept: application/vnd.api+json

@aklish
Copy link
Member

aklish commented Apr 24, 2020

Are you doing a POST or PATCH?

@prateekagrwl05
Copy link
Author

prateekagrwl05 commented Apr 24, 2020

this is a POST request

PATCH has another problem.

Request URL: http://localhost:8080/content/metadata
METHOD: PATCH
HEADERS: Content-type: application/vnd.api+json; ext=jsonpatch Accept: application/vnd.api+json; ext=jsonpatch

Request:

[
	{
		"op": "add",
		"path": "/",
		"value": {
			"type": "metadata",
			"id": "1",
			"relationships": {
				"ast": {
					"data": {
						"id": "9223372036854775788",
						"type": "asset"
					}
				},
				"txnItm": {
					"data": {
						"id": "dispute2",
						"type": "taxonomyItem"
					}
				},
				"txnGrp": {
					"data": {
						"id": "locale2",
						"type": "taxonomy"
					}
				}
			}
		}
	}
]

Response:

{
  "errors": [
    "TransactionException: Cannot deserialize instance of `com.yahoo.elide.jsonapi.models.JsonApiDocument` out of START_ARRAY token\n at [Source: UNKNOWN; line: -1, column: -1]"
  ]
}

@aklish
Copy link
Member

aklish commented Apr 24, 2020

The order of the relationships should not matter.

@aklish
Copy link
Member

aklish commented Apr 24, 2020

For PATCH extension, every object must be assigned an ID (even if temporary) if you want to fixup relationships.

@aklish
Copy link
Member

aklish commented Apr 24, 2020

PATCH extension docs don't include information on this - and probably should return a more meaningful error. I am going to file an issue to resolve that.

I haven't seen the POST problem before.

@prateekagrwl05
Copy link
Author

For the patch request, I have assigned an id - 1. Is there anything to do more?

@aklish
Copy link
Member

aklish commented Apr 24, 2020

Do all of the other objects already exist that you are referencing in?

@aklish
Copy link
Member

aklish commented Apr 24, 2020

Relationships must point to a pre-existing object - or one that you created in the same PATCH extension request.

@prateekagrwl05
Copy link
Author

Yes, so when I change the order to ast, txnItm, txnGrp, it works absolutely fine

@aklish
Copy link
Member

aklish commented Apr 24, 2020

What is the model for TaxonomyItem?

@prateekagrwl05
Copy link
Author

prateekagrwl05 commented Apr 24, 2020

Here is the model for taxonomyItem

@Include(rootLevel = true, type="taxonomyItem")
@ReadPermission(expression = "Prefab.Role.All")
@UpdatePermission(expression = "Prefab.Role.All")
@CreatePermission(expression = "Prefab.Role.All")
@SharePermission
class TaxonomyItem {

@Id
@Column(name="item_cd")
private String id;

@Column(name= "item_nm")
private String name;

@OneToMany
@JoinColumn(name= "item_cd")
private Set<Metadata> mtda;

}

@aklish
Copy link
Member

aklish commented Apr 24, 2020

Also - are you using an ORM like Hibernate to generate the schema?

@prateekagrwl05
Copy link
Author

No, we are not using anything for schema generation.

spring.jpa.properties.hibernate.ddl-auto = update

@aklish
Copy link
Member

aklish commented Apr 24, 2020

Can you post your schemas - or alternatively - make a small project so I can debug?

@aklish
Copy link
Member

aklish commented Apr 24, 2020

I misspoke on one point:

{
  "errors": [
    "TransactionException: Cannot deserialize instance of `com.yahoo.elide.jsonapi.models.JsonApiDocument` out of START_ARRAY token\n at [Source: UNKNOWN; line: -1, column: -1]"
  ]
}

means you are using the wrong content type for patch extension. JSON-API and the patch extension use different content types.

Use application/vnd.api+json; ext=jsonpatch

@prateekagrwl05
Copy link
Author

I have started using 4.6.1 and the bulk operation is succeeding fine now with application/vnd.api+json; ext=jsonpatch.

Also, I have written all the relationships again. Not sure what has changed in 4.6.1 but the other issue is also working fine now.

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