SAP / olingo-jpa-processor-v4

The JPA Processor fills the gap between Olingo V4 and the database, by providing a mapping between JPA metadata and OData metadata, generating queries and supporting the entity manipulations.
Apache License 2.0
122 stars 76 forks source link

Adjusting Endpoints #149

Closed kishanp519 closed 2 years ago

kishanp519 commented 2 years ago

Is there a way to remove the automatic addition of an 's' at the end of an Entity name? For example, I'm trying to do https://localhost:8080/OData/V1.0/Employees(1) but have to do https://localhost:8080/OData/V1.0/Employeess(1) when the table name is 'Employees'. Is there a way I can customize the endpoints to only have a singular s.

wog48 commented 2 years ago

Actually there is. Unfortunately it is not documented yet. So let me describe it here:

The name of the artifacts in the API (called externalName in the code) are generated by a class implementing the JPAEdmNameBuilder interface. By default JPADefaultEdmNameBuilder is taken.

Here an example, which shall replace for one specific entity set the name:

class APINameBuilder implements JPAEdmNameBuilder {
  private final JPAEdmNameBuilder defaultNameBuilder;

  APINameBuilder(final String punit) {
    defaultNameBuilder = new JPADefaultEdmNameBuilder(punit);
  }

  @Override
  public String buildComplexTypeName(final EmbeddableType<?> jpaEmbeddedType) {
    return defaultNameBuilder.buildComplexTypeName(jpaEmbeddedType);
  }

  @Override
  public String buildContainerName() {
    return defaultNameBuilder.buildContainerName();
  }

  @Override
  public String buildEntitySetName(final String entityTypeName) {
    return "Person".equals(entityTypeName) ? "People" : defaultNameBuilder.buildEntitySetName(entityTypeName);
  }

  @Override
  public String buildEntityTypeName(final EntityType<?> jpaEntityType) {
    return defaultNameBuilder.buildEntityTypeName(jpaEntityType);
  }

  @Override
  public String buildEnumerationTypeName(final Class<? extends Enum<?>> javaEnum) {
    return defaultNameBuilder.buildEnumerationTypeName(javaEnum);
  }

  @Override
  public String buildNaviPropertyName(final Attribute<?, ?> jpaAttribute) {
    return defaultNameBuilder.buildNaviPropertyName(jpaAttribute);
  }

  @Override
  public String buildOperationName(final String internalOperationName) {
    return defaultNameBuilder.buildOperationName(internalOperationName);
  }

  @Override
  public String buildPropertyName(final String jpaAttributeName) {
    return defaultNameBuilder.buildPropertyName(jpaAttributeName);
  }

  @Override
  public String getNamespace() {
    return defaultNameBuilder.getNamespace();
  }

}

The name builder needs to be provided with the session context. E.g.:

  @Bean
  public JPAODataSessionContextAccess sessionContext(@Autowired final EntityManagerFactory emf) throws ODataException {

    return JPAODataServiceContext.with()
        .setPUnit(punit)
        .setEntityManagerFactory(emf)
        .setTypePackage(rootPackages)
        .setErrorProcessor(new ErrorPostProcessor())
        .setRequestMappingPath("Trippin/v1")
        .setEdmNameBuilder(new APINameBuilder(punit))
        .build();
  }
kishanp519 commented 2 years ago

Thank you - I made it so it just takes the exact table name rather than adjusting the endpoints with 's'. Makes it easier for people to use. Do you know when documentation will be updated?

wog48 commented 2 years ago

I have created a new documentation for this: Name Building