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

replace skipUserManagement logic with 'with builtInEntity' definition #21767

Merged
merged 1 commit into from
Apr 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions generators/info/generator.mts
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,11 @@ export default class InfoGenerator extends BaseApplicationGenerator {
});
jdlObject = JSONToJDLEntityConverter.convertEntitiesToJDL({
entities,
skippedUserManagement: this.jhipsterConfig.skipUserManagement,
});
JSONToJDLOptionConverter.convertServerOptionsToJDL({ 'generator-jhipster': this.config.getAll() }, jdlObject);
} catch (error) {
this.logger.warn((error as any).message || error);
throw new Error('\nError while parsing entities to JDL\n');
this.logger.error('Error while parsing entities to JDL', error);
throw new Error('\nError while parsing entities to JDL\n', { cause: error });
}
return jdlObject;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ entity Another {
const jdlApplicationWithRelationshipToUser = `
${jdlApplicationWithEntities}
relationship ManyToOne {
One{user(login)} to User
One{user(login)} to User with builtInEntity
}
`;

Expand Down
2 changes: 2 additions & 0 deletions jdl/__snapshots__/jdl-importer.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ exports[`jdl - JDLImporter import when not parsing applications should export en
"otherEntityName": "user",
"relationshipName": "user",
"relationshipType": "many-to-one",
"relationshipWithBuiltInEntity": true,
},
{
"otherEntityField": "lastName",
Expand Down Expand Up @@ -567,6 +568,7 @@ exports[`jdl - JDLImporter import when not parsing applications should return th
"otherEntityName": "user",
"relationshipName": "user",
"relationshipType": "many-to-one",
"relationshipWithBuiltInEntity": true,
},
{
"otherEntityField": "lastName",
Expand Down
2 changes: 1 addition & 1 deletion jdl/__test-files__/big_sample.jdl
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ relationship OneToMany {
}

relationship ManyToOne {
Employee{user(login)} to User,
Employee{user(login)} to User with builtInEntity,
Employee{manager(lastName)} to Employee
}

Expand Down
2 changes: 1 addition & 1 deletion jdl/__test-files__/complex_jdl.jdl
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ relationship ManyToMany {
}

relationship ManyToOne {
Employee{user(login)} to User
Employee{user(login)} to User with builtInEntity
}

relationship OneToOne {
Expand Down
1 change: 1 addition & 0 deletions jdl/__test-files__/jhipster_app/.jhipster/Country.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"relationshipType": "one-to-one",
"relationshipName": "user",
"otherEntityName": "user",
"relationshipWithBuiltInEntity": true,
"ownerSide": true,
"otherEntityRelationshipName": "country"
}
Expand Down
2 changes: 1 addition & 1 deletion jdl/__test-files__/multiple_jdl_comments.jdl
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ entity A { // Another line
}
// thing
relationship OneToOne {
A to User
A to User with builtInEntity
}
2 changes: 1 addition & 1 deletion jdl/__test-files__/realistic_sample.jdl
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ enum Gender {
}

relationship OneToOne {
Customer{user(login) required} to User
Customer{user(login) required} to User with builtInEntity
}

service Customer with serviceClass
Expand Down
4 changes: 2 additions & 2 deletions jdl/__test-files__/user_entity_to_relationship.jdl
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ entity A
entity B

relationship ManyToOne {
A{user} to User
A{user} to User with builtInEntity
}

relationship OneToOne {
B{user} to User
B{user} to User with builtInEntity
}
12 changes: 0 additions & 12 deletions jdl/converters/jdl-to-json/jdl-to-json-basic-entity-converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,6 @@ function createJSONEntities(jdlEntities: JDLEntity[]): Map<string, JSONEntity> {

jdlEntities.forEach(jdlEntity => {
const entityName = jdlEntity.name;
/*
* If the user adds a 'User' entity we consider it as the already
* created JHipster User entity and none of its fields and owner-side
* relationships will be considered.
*/
if (builtInEntities.has(entityName.toLowerCase())) {
logger.warn(
`An Entity name '${entityName}' was used: '${entityName}' is an entity created by default by JHipster.` +
' All relationships toward it will be kept but any attributes and relationships from it will be disregarded.'
);
return;
}
convertedEntities.set(
entityName,
new JSONEntity({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,8 @@ describe('jdl - JDLToJSONBasicEntityConverter', () => {
context('with some of them being built-in entities', () => {
let builtInEntitiesAreConverted;
let customEntitiesAreConverted;
let loggerSpy;

before(() => {
loggerSpy = sinon.spy(logger, 'warn');
const entityA = new JDLEntity({
name: 'A',
tableName: 'entity_a',
Expand All @@ -66,18 +64,8 @@ describe('jdl - JDLToJSONBasicEntityConverter', () => {
builtInEntitiesAreConverted = returnedMap.has('User') || returnedMap.has('Authority');
});

after(() => {
loggerSpy.restore();
});

it('should warn about them', () => {
expect(loggerSpy.getCall(0).args[0]).to.equal(
"An Entity name 'User' was used: 'User' is an entity created by default by JHipster. All relationships " +
'toward it will be kept but any attributes and relationships from it will be disregarded.'
);
});
it('should not convert built-in entities', () => {
expect(builtInEntitiesAreConverted).to.be.false;
it('should convert built-in entities', () => {
expect(builtInEntitiesAreConverted).to.be.true;
});
it('should convert custom entities', () => {
expect(customEntitiesAreConverted).to.be.true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import { convert } from './jdl-to-json-relationship-converter.js';
import { relationshipTypes, relationshipOptions } from '../../jhipster/index.mjs';

const { ONE_TO_ONE, MANY_TO_MANY, MANY_TO_ONE, ONE_TO_MANY } = relationshipTypes;
const { JPA_DERIVED_IDENTIFIER } = relationshipOptions;
const { BUILT_IN_ENTITY } = relationshipOptions;

describe('jdl - JDLToJSONRelationshipConverter', () => {
describe('convert', () => {
Expand Down Expand Up @@ -158,6 +158,8 @@ describe('jdl - JDLToJSONRelationshipConverter', () => {
injectedFieldInTo: 'a',
injectedFieldInFrom: 'b',
options: {
source: {},
destination: {},
global: {
custom: 42,
},
Expand Down Expand Up @@ -193,8 +195,10 @@ describe('jdl - JDLToJSONRelationshipConverter', () => {
injectedFieldInTo: 'a',
injectedFieldInFrom: 'b',
options: {
source: {},
destination: {},
global: {
[JPA_DERIVED_IDENTIFIER]: true,
[BUILT_IN_ENTITY]: true,
},
},
});
Expand All @@ -210,7 +214,7 @@ describe('jdl - JDLToJSONRelationshipConverter', () => {
"ownerSide": true,
"relationshipName": "b",
"relationshipType": "one-to-one",
"useJPADerivedIdentifier": true,
"relationshipWithBuiltInEntity": true,
}
`);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,9 @@ import JDLRelationship from '../../models/jdl-relationship.js';
const {
Validations: { REQUIRED },
} = validations;
const { JPA_DERIVED_IDENTIFIER } = relationshipOptions;
const { JPA_DERIVED_IDENTIFIER, BUILT_IN_ENTITY } = relationshipOptions;
const { MANY_TO_MANY, MANY_TO_ONE, ONE_TO_MANY, ONE_TO_ONE } = relationshipTypes;

const USER = 'user';
const AUTHORITY = 'authority';
const builtInEntities = new Set([USER, AUTHORITY]);

let convertedRelationships;

export default { convert };
Expand Down Expand Up @@ -156,6 +152,8 @@ function setOptionsForRelationshipSourceSide(relationshipToConvert, convertedRel
if (convertedRelationship.ownerSide) {
convertedRelationship.useJPADerivedIdentifier = optionValue;
}
} else if (optionName === BUILT_IN_ENTITY) {
convertedRelationship.relationshipWithBuiltInEntity = optionValue;
} else {
convertedRelationship.options[optionName] = optionValue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,12 +142,12 @@ describe('jdl - JDLWithApplicationsToJSONConverter', () => {
const returnedMap: any = convert({
jdlObject,
});
customEntitiesAreConverted = returnedMap.get('toto').every(entity => entity.name === 'A');
customEntitiesAreConverted = returnedMap.get('toto').some(entity => entity.name === 'A');
builtInEntitiesAreConverted = returnedMap.get('toto').some(entity => entity.name === 'User' || entity.name === 'Authority');
});

it('should not convert built-in entities', () => {
expect(builtInEntitiesAreConverted).to.be.false;
expect(builtInEntitiesAreConverted).to.be.true;
});
it('should convert custom entities', () => {
expect(customEntitiesAreConverted).to.be.true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,12 @@ describe('jdl - JDLWithoutApplicationToJSONConverter', () => {
applicationType: MONOLITH,
databaseType: SQL,
});
customEntitiesAreConverted = returnedMap.get('toto').every(entity => entity.name === 'A');
customEntitiesAreConverted = returnedMap.get('toto').some(entity => entity.name === 'A');
builtInEntitiesAreConverted = returnedMap.get('toto').some(entity => entity.name === 'User' || entity.name === 'Authority');
});

it('should not convert built-in entities', () => {
expect(builtInEntitiesAreConverted).to.be.false;
it('should convert built-in entities', () => {
expect(builtInEntitiesAreConverted).to.be.true;
});
it('should convert custom entities', () => {
expect(customEntitiesAreConverted).to.be.true;
Expand Down
3 changes: 1 addition & 2 deletions jdl/converters/json-to-jdl-converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,7 @@ function getJDLObjectFromSingleApplication(
existingJDLObject.addApplication(jdlApplication);
return existingJDLObject;
}
const skippedUserManagement = jdlApplication.getConfigurationOptionValue(OptionNames.SKIP_USER_MANAGEMENT);
const jdlObject = convertEntitiesToJDL({ entities, skippedUserManagement });
const jdlObject = convertEntitiesToJDL({ entities });
entities.forEach((entity, entityName) => jdlApplication.addEntityName(entityName));
jdlObject.addApplication(jdlApplication);
return mergeJDLObjects(existingJDLObject, jdlObject);
Expand Down
40 changes: 1 addition & 39 deletions jdl/converters/json-to-jdl-entity-converter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import { unaryOptions, relationshipOptions, binaryOptions } from '../jhipster/in
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

const { JPA_DERIVED_IDENTIFIER } = relationshipOptions;
const { JPA_DERIVED_IDENTIFIER, BUILT_IN_ENTITY } = relationshipOptions;

const {
Options: { DTO, SEARCH, PAGINATION, MICROSERVICE, ANGULAR_SUFFIX, SERVICE },
Expand Down Expand Up @@ -202,44 +202,6 @@ describe('jdl - JSONToJDLEntityConverter', () => {
expect(jdlObject.relationships.getOneToOne('OneToOne_Country{user}_User')).not.to.be.undefined;
});
});
context('when there is a User.json entity', () => {
let entities;

before(() => {
entities = new Map([
['Country', readJsonEntity('Country')],
['User', readJsonEntity('Region')],
]);
});

it('should fail', () => {
expect(() => {
convertEntitiesToJDL({
entities,
});
}).to.throw('User entity name is reserved if skipUserManagement is not set.');
});
});
});
context('when skipUserManagement flag is set', () => {
let jdlObject;

before(() => {
const regionEntity = readJsonEntity('Region');
regionEntity.relationships[0].otherEntityRelationshipName = 'user';
const entities = new Map([
['Country', readJsonEntity('Country')],
['User', regionEntity],
]);
jdlObject = convertEntitiesToJDL({ entities, skippedUserManagement: true });
});

it('should parse the User entity', () => {
expect(jdlObject.entities.Country).not.to.be.undefined;
expect(jdlObject.entities.User).not.to.be.undefined;
expect(jdlObject.entities.User.fields.regionId).not.to.be.undefined;
expect(jdlObject.relationships.getOneToOne('OneToOne_Country{user}_User{country}')).not.to.be.undefined;
});
});
context('without relationship', () => {
let jdlObject;
Expand Down
Loading