Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
  • Jun 26 2017 20:07
    @remmeier banned @hibaymj
Remo
@remmeier
@swyrazik sorry, a late reply. you can always intercept and add custom logic to map the transient field to actual JPA fields (also with a decorator or custom implementation of the repository). But for the sake of simplicity I usually try to avoid this things when it comes to JPA. In many cases a DB View may suffice to let the DB take care of those things.
Stepenik
@stepenik

Hi everbody,
I am new in JsonApi and I see this great library crnk.

My question is where to put custom logic written in services.
In standard Spring Boot Rest Api we have controllers -> services -> repositories
but in JsonApi (crnk) we have resources and repositories.
Resources is controllers in standard Rest Api ?
Where I can put custom logic and how.

Remo
@remmeier
Custom logic is all in the repositories. The "lower-level plumbing" is being taken care of by JSON:API as standard and crnk as implementation. That should be fine for 98% of the use cases, and there are hooks for the other 2%.
3 replies
wulfcolin
@wulfcolin
Hi @remmeier can I have a JSONObject (i.e org.json.JSONObject) as attribute type?
Currently I can only use this if the API caller passes the attribute as string and then jackson will parse the json object from string. Is there any other option? Thanks!
2 replies
aroraKom
@aroraKom
Is there any way to generate swagger documentation for Spring boot maven project which is using crnk library
2 replies
kushal69
@kushal69
@remmeier thank you for the the reply. One more question - I am using crnk-gen-openapi lib to generate open api document in that i need to add headers (like Authorization) which we will be consuming at crnk. Can you please suggest on this ?
1 reply
Lasse Immonen
@turtleman

hello! wanting to ask about a PR and possible new releases coming out. the PR is this one: crnk-project/crnk-framework#793. how does it look, is it okay?

how about plans for a new release? anything projected at the moment?

rtrinker
@rtrinker

Hello there, I'm trying to make use of the @JsonApiMetaInformation, I followed the documentation but I don't see it in my response when I try it with the postman, here is the code of my resource:


/**
 * The persistent class for the EF_V_API_TW_RUECKLIEF_KOPF_DET database table.
 * 
 * 
 */
@Data
@Entity
@NoArgsConstructor
@Table(name = "EF_V_API_TW_RUECKLIEF_KOPF_DET")
@JsonApiResource(type = "return", resourcePath = "returns")
public class Return implements Serializable {
  private static final long serialVersionUID = 1L;

...

 @JsonApiMetaInformation
  private ReturnMeta meta;

  public static class ReturnMeta implements MetaInformation, Serializable {

    private static final long serialVersionUID = 8129518187692599777L;

    private Boolean patchable = true;

    public Boolean getPatchable() {
      return patchable;
    }

    public void setPatchable(Boolean patchable) {
      this.patchable = patchable;
    }

  }

Am I missing something in the repository?
Thank you very much.

Remo
@remmeier
looks good to me. Meta data not visible at all? ResourceMapper would be the class responsible for writing meta data and other fields. So if there is an issue, it should be visible in there.
Stepenik
@stepenik

Hi @remmeier .
How works crnk-security ?
I put
config.permitRole("USER", ResourcePermission.ALL);

but on every request It gives me 401 unauthorized.
In spring-security I have 2 roles ROLE_USER, ROLE_ADMIN.

In spring security @PreAuthorize("hasRole('USER')") this works.

Remo
@remmeier
looks good to me. The DEBUG log should help track it down if there is an issue. On startup there (probably) is also a message what kind of SecurityProvider is installed, should be a spring or servlet one I think in the spring boot case.
whgibbo
@whgibbo
@remmeier Was wondering if you could point me in the right direction..
I have a use case where I need to generate JSONAPI output, but not via a REST API. What is the best entry point/service that I can use to take advantage of the Resources / Repos that I already have in place ?
fchill
@fchill
Is it expected that unknown fields are ignored within nested attributes?
Let's say our server (using crnk) accepts requests with attributes {name: {first: "Max", last: "Powers"}}, we would receive a HTTP400 when sending additional, unknown attributes on the same level as "name". But it is ignored inside the nested object:
{name: {first: "Max", last: "Powers"}, test: 1} -> BAD_REQUEST
{name: {first: "Max", last: "Powers", test: 1}} -> CREATED (but "test" is of course ignored and not persisted)
We modeled these nested objects as additional POJOs that are used by our main JsonApiResource
Remo
@remmeier
@whgibbo maybe have a look at the CrnkServlet and JsonApiRequestProcessor. Something in that area should help
2 replies
Remo
@remmeier
@fchill @JsonApiEmbeddable could work (it is fairly recent and may miss something), generally it will allow to track that nested attribute, rather then just relying on Jackson.
wulfcolin
@wulfcolin
Is there any reasoning behind nested resources? How deep can they be nested and is there an estimation when it is no longer experimental?
anilmullapudi
@anilmullapudi
Is Crnk support spring Autowired dependency injection?
I tried to inject bean using the following code. But no luck. @Component
public class ExampleRepository extends ResourceRepositoryBase<Example, String> {
@Autowired
private ApplicationContext appContext;
public ExampleRepository() {
super(Example.class)
appContext.getBean(CSVLoader.class)
}
}
@Configuration
public class AppLoader {
@Bean
CSVLoader getCSVData() {
return new CSVLoader();
}
}
public class CSVLoader {
public static List<String> equipmentData=new ArrayList<>();
CSVLoader() {
equipmentData.add("Example");
}
}
Jochen Reinhardt
@JochenReinhardt
I'm using crnk with DTO mapping and many-to-many relationships
When I define a many-to-many relationship in the entity and directly expose the entity as crnk resource, the relationship works as expected.
When I expose the same data with a DTO mapper enabled, the relationship does not work anymore.
I found, that in GetFromOwnerStrategy for the entity, the source actually is the JPA entity and the fieldAccessor call triggers JPA lazy loading.
Whereas when DTO mapping is in place, the source is the DTO and the fieldAccessor returns null
Is this a bug?
avbitla
@avbitla
Hi Team! What's the best way to pass the database info in the response headers? My initial thought is to use a response filter and call database for required info. But I don't like having database dependency from a filter class. Is there a way to pass the database info into response headers from the repository class itself?
duncanportelli
@duncanportelli

Hi all,

i am trying to do a filter to check if a date value exists within a certain 2 ranges and using the below filter:

{"OR":{"AND":{"GE":{"transmissionDate":"2020-10-01T00:00"},"LE":{"transmissionDate":"2020-10-15T00:00"}},"AND":{"GE":{"transmissionDate":"2020-11-01T00:00"},"LE":{"transmissionDate":"2020-11-15T00:00"}}}}

I believe it is correct but crnk is only taking into consideration the last AND. Is this an issue?

Jochen Reinhardt
@JochenReinhardt
I am trying to filter on a related entity being null, e.g. there's a relation from A to B, I want to find A where B is null. Crnk produces an SQL query that has a correct where clause (A.B_ID is null) but it does not find any entries because it uses an INNER JOIN to join A and B. This inner join actually finds all A where B is actually assigned. Any hints?
Jochen Reinhardt
@JochenReinhardt
Is there a way to configure a JOIN TYPE for certain resources / paths?
Jochen Reinhardt
@JochenReinhardt
I found configureQuery(JpaQuery<?> query)method in JpaEntityRepositoryBase and now set the join type on the relations to JoinType.LEFT. Seems to work well :)
wulfcolin
@wulfcolin

@remmeier what would be the best option if you just want to add a prefix to outgoing links but not for matching requests?
Apparently setWebPathPrefix is also used for matching and I only want this prefix to be present in response links.
Reimplementing JsonApiUrlBuilder just to add a path prefix seems overkill too.
As ResourceRegistryImpl is hardwired in CrnkBoot I cannot override the getBaseUrl either.

In the Doc it says: "setUrlMapper to provide a new url mapping implementation to customize how Crnk generates links." - But isn't that one use only for mapping incoming links? And UrlBuilder is used to generate links for resources?

Jochen Reinhardt
@JochenReinhardt
@duncanportelli ANDs and ORs should be arrays, indicated by square brackets, e.g. "AND": [{...}, {...}]. I think you used curly braces in your example.
duncanportelli
@duncanportelli

@JochenReinhardt thanks very much for your input. achieved my intended result just as you said. leaving the result here just in case anyone encounters the same difficulty:

{"OR":[{"GE":{"transmissionDate":"2020-10-01T00:00"},"LE":{"transmissionDate":"2020-10-15T00:00"}},{"GE":{"transmissionDate":"2020-11-01T00:00"},"LE":{"transmissionDate":"2020-11-15T00:00"}}]}

Sebastian Wyrazik
@swyrazik
Hi all! Do you know of a way to use/escape % as a literal part of a LIKE filter expression? For example: GET /tasks?filter[name][LIKE]=% 50% done, where I'd like the first % to act as a wildcard as usual and the second % to act as an actual literal part of the expression, maybe somehow escaped. (In this case, Task 50% done should match, while Task 50# done shouldn't match)
Remo
@remmeier
@swyrazik in principle you can add the escaping logic to your repository findAll. But would be nice to have some built-in support as well. Or even better in the JSON:API spec.
@wulfcolin what is your use case for things to not be symmetric between request and response?
1 reply
@duncanportelli can you check whether the QuerySpec used in findAll matches that request? If there is an issue, it can be on the parsing side or the translation into the native query of the repository implementation (like JPA)
@avbitla HttpRequestContextAware gives low-level HTTP access
Remo
@remmeier
@wulfcolin I think in the meantime the nesting can be considered fairly stable. So that experimental could be removed IMHO. As for reasoning. As for reason, it is not uncommon for objects to have a parent and share the same lifecycle. So deleting the parent will delete them. The nesting helps to give "nice urls". Otherwise it behaves pretty much like any other repositories. You can even have it both ways, with nested urls but also direct non-nested access.
1 reply
Sebastian Wyrazik
@swyrazik
@remmeier Thanks for the info!
Rehab Reda
@rehab-reda0
Hi @remmeier
I have a question If I am having an id in the database it looks like #profile:{uuid}
I want it when it appears in json to be the {uuid} only How I can do that ?
Remo
@remmeier
simplest way might be to define an object representing that primary key. there is then a jackson annotation that specifies an object can be serialized to a string with a given serializer. dont remember its name just now, but crnk will pick this up. if not, let me know.
duncanportelli
@duncanportelli

Hi @remmeier. In a relationship where the source and target are both of the same type, while filtering/sorting/pagination how do you refer to just the target only since in a filter you are to specify the type.

E.g. a resource of a type person having a relationship "children" which is of type person as well. How can I get the persons together with the eldest child younger than 18?

/persons?include=children?filter[children][age][LT]=18&sort=-children.age&page[limit]=1

Anders Swanson
@anderskswanson
Hi, is there a public way to serialize/deserialize objects such that you get their JSON:API representation? I would like to do some processing on this representation without receiving it from an HTTP response (instead, directly from CRNK)
Essentially I would like to use the serializer that CRNK uses when it is creating an HTTP response.
Anders Swanson
@anderskswanson
Does CRNK use any filter to do this that I could intercept?
duncanportelli
@duncanportelli
@anderskswanson you can set your own serializer in CrnkBoot
Anders Swanson
@anderskswanson
@duncanportelli I've found AbstractDocumentFilter as an entry point here. This class contains a filter method which lets you access the JSON:API Response object.
I believe this fits my use case of intercepting the JSON:API response to do some processing on it :)
Remo
@remmeier
@duncanportelli if it has the same type, rhen as yet not possible. one could imagine swutching types with attribute paths. both approaches have there merits.
This message was deleted
Remo
@remmeier
@anderskswanson DocumentMapper gives you a Document object that can be serialized with Jackson or the DocumentFilter as mentioned if you want to intercept a live request