Skip to content

Commit

Permalink
internal: add relationship microserviceName check (#27912)
Browse files Browse the repository at this point in the history
  • Loading branch information
mshima authored Nov 19, 2024
1 parent 7e11a44 commit 03b1bcd
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 15 deletions.
3 changes: 1 addition & 2 deletions generators/base-application/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import {
GENERATOR_BOOTSTRAP_APPLICATION_SERVER,
} from '../generator-list.js';
import type { TaskTypes as DefaultTaskTypes } from '../../lib/types/application/tasks.js';
import type { ApplicationType } from '../../lib/types/application/application.js';
import type { Entity } from '../../lib/types/application/entity.js';
import type { GenericTaskGroup } from '../../lib/types/base/tasks.js';
import type { ApplicationConfiguration } from '../../lib/types/application/yo-rc.js';
Expand Down Expand Up @@ -94,7 +93,7 @@ export default class BaseApplicationGenerator<
static POST_WRITING_ENTITIES = asPriority(POST_WRITING_ENTITIES);

declare jhipsterConfig: ApplicationConfiguration & Record<string, any>;
declare sharedData: SharedData<ApplicationType>;
declare sharedData: SharedData;

constructor(args: string | string[], options: JHipsterGeneratorOptions, features: JHipsterGeneratorFeatures) {
super(args, options, features);
Expand Down
4 changes: 2 additions & 2 deletions generators/base-core/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1390,7 +1390,7 @@ templates: ${JSON.stringify(existingTemplates, null, 2)}`;
return this.options.sharedData.applications?.[this.calculateApplicationId(applicationFolder)];
}

private createSharedData({ help }: { help?: boolean }): SharedData<BaseApplication> {
private createSharedData({ help }: { help?: boolean }): SharedData {
const applicationId = this.options.applicationId ?? this.calculateApplicationId(this.destinationPath());
if (this.options.sharedData.applications === undefined) {
this.options.sharedData.applications = {};
Expand All @@ -1401,7 +1401,7 @@ templates: ${JSON.stringify(existingTemplates, null, 2)}`;
}
const { ignoreNeedlesError } = this.options;

return new SharedData<BaseApplication>(
return new SharedData(
sharedApplications[applicationId],
{ destinationPath: this.destinationPath(), memFs: this.env.sharedFs, log: this.log, logCwd: this.env.logCwd },
{ ignoreNeedlesError },
Expand Down
18 changes: 10 additions & 8 deletions generators/base/shared-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@ import { lt as semverLessThan } from 'semver';
import { defaults } from 'lodash-es';
import type { MemFsEditor } from 'mem-fs-editor';
import { create } from 'mem-fs-editor';
import { type BaseApplication } from '../base-application/types.js';
import { GENERATOR_JHIPSTER } from '../generator-constants.js';
import type { ApplicationType } from '../../lib/types/application/application.js';
import type { Entity } from '../../lib/types/application/entity.js';
import type { Entity as BaseEntity } from '../../lib/types/base/entity.js';
import type { CleanupArgumentType, Control } from './types.js';

export default class SharedData<ApplicationType extends BaseApplication = BaseApplication> {
export default class SharedData<EntityType extends BaseEntity = Entity, Application = ApplicationType> {
_storage: any;
_editor: MemFsEditor;
_log: any;
Expand Down Expand Up @@ -145,32 +147,32 @@ export default class SharedData<ApplicationType extends BaseApplication = BaseAp
return this._storage.control;
}

getApplication(): ApplicationType {
getApplication(): Application {
if (!this._storage.sharedApplication) throw new Error('Shared application not loaded');
return this._storage.sharedApplication;
}

setEntity(entityName: string, entity) {
setEntity(entityName: string, entity: { name: string } & Partial<EntityType>): void {
this._storage.sharedEntities[entityName] = entity;
}

hasEntity(entityName) {
hasEntity(entityName): boolean {
return Boolean(this._storage.sharedEntities[entityName]);
}

getEntity(entityName) {
getEntity(entityName): EntityType {
const entity = this._storage.sharedEntities[entityName];
if (!entity) {
throw new Error(`Entity definition not loaded for ${entityName}`);
}
return entity;
}

getEntities(entityNames = Object.keys(this._storage.sharedEntities)) {
getEntities(entityNames = Object.keys(this._storage.sharedEntities)): { entityName: string; entity: EntityType }[] {
return entityNames.map(entityName => ({ entityName, entity: this.getEntity(entityName) }));
}

getEntitiesMap() {
getEntitiesMap(): Record<string, EntityType> {
return this._storage.sharedEntities;
}
}
17 changes: 14 additions & 3 deletions generators/bootstrap-application-base/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,11 +239,11 @@ export default class BootstrapApplicationBase extends BaseApplicationGenerator {
}

if (entityConfig.changelogDate) {
entityConfig.annotations.changelogDate = entityConfig.changelogDate;
entityConfig.annotations!.changelogDate = entityConfig.changelogDate;
delete entityConfig.changelogDate;
}
if (!entityConfig.annotations.changelogDate) {
entityConfig.annotations.changelogDate = this.dateFormatForLiquibase();
if (!entityConfig.annotations!.changelogDate) {
entityConfig.annotations!.changelogDate = this.dateFormatForLiquibase();
entityStorage.save();
}
},
Expand Down Expand Up @@ -359,6 +359,17 @@ export default class BootstrapApplicationBase extends BaseApplicationGenerator {
this.validateResult(loadEntitiesOtherSide(entities, { application }));

for (const entity of entities) {
if (!entity.builtIn) {
const invalidRelationship = entity.relationships.find(
({ otherEntity }) => !otherEntity.builtIn && entity.microserviceName !== otherEntity.microserviceName,
);
if (invalidRelationship) {
throw new Error(
`Microservice entities cannot have relationships with entities from other microservice: '${entity.name}.${invalidRelationship.relationshipName}'`,
);
}
}

for (const field of entity.fields) {
if (isFieldBinaryType(field)) {
if (isFieldBlobType(field)) {
Expand Down
1 change: 1 addition & 0 deletions lib/types/base/entity.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export type Entity<F extends Field = Field, R extends Relationship = Relationshi

fields?: F[];
relationships?: R[];
annotations?: Record<string, string | boolean>;

readOnly?: boolean;
embedded?: boolean;
Expand Down

0 comments on commit 03b1bcd

Please sign in to comment.