I am using Jmix 3.1.0 with the Dynamic Model add-on. I can define entities, attributes and enumerations in the admin UI (Dynamic model settings view) and apply them successfully.
My question: Is it possible to create or update the dynamic model (entities, attributes, enumerations) through the REST API instead of the admin UI?
As Mladen wrote, there is no standard operation for updating the dynamic model remotely.
However, all the process is encapsulated in just a few bean methods, so you could create your own REST controller like this one (this is AI-generated untested code):
@RestController
@RequestMapping("/api/dynmodel")
public class DynamicModelController {
@Autowired DynamicModelSettingsService settingsService;
@Autowired DynamicModelManager modelManager;
@Autowired DynamicModelDefinitionSerializer serializer;
@Autowired DataManager dataManager;
record ModelDto(@Nullable UUID baseId, String content) {}
@GetMapping
public ModelDto get() {
DynamicModelSettings s = settingsService.loadActive();
// baseId is null when no model was ever saved (loadActive returns a new record then)
return new ModelDto(entityStates.isNew(s) ? null : s.getId(), s.getContent());
}
@PostMapping("/apply")
public void apply(@RequestBody ModelDto dto) {
DynamicModelSettings settings = dto.baseId() == null
? settingsService.loadActive() // first-ever save
: dataManager.load(DynamicModelSettings.class).id(dto.baseId()).one();
settings.setContent(dto.content());
DynamicModelDefinition modelDef = serializer.deserialize(dto.content());
List<DynamicModelMigration> migrations = modelManager.getMigrations(modelDef);
settingsService.applyAndSave(migrations, settings);
}
}
Could you describe your use case in more detail, so we would consider adding this as a standard feature?