Skip to content

Add JSON format option for orm:mapping:describe command output #12071

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

Open
wants to merge 1 commit into
base: 3.6.x
Choose a base branch
from

Conversation

stlgaits
Copy link
Contributor

Following suggestion from doctrine/DoctrineBundle#1883, this PR adds the --format=json option to the orm:mapping:describe command to enable JSON output (hence allowing jq filtering of said-output).

For example, when running symfony console doctrine:mapping:describe App\\Entity\\Book --format=json, we get the following output :

{
    "name": "App\\Entity\\Book",
    "rootEntityName": "App\\Entity\\Book",
    "customGeneratorDefinition": null,
    "customRepositoryClassName": "App\\Repository\\BookRepository",
    "isMappedSuperclass": false,
    "isEmbeddedClass": false,
    "parentClasses": [],
    "subClasses": [],
    "embeddedClasses": [],
    "identifier": [
        "id"
    ],
    "inheritanceType": 1,
    "discriminatorColumn": null,
    "discriminatorValue": null,
    "discriminatorMap": [],
    "generatorType": 4,
    "table": {
        "name": "book"
    },
    "isIdentifierComposite": false,
    "containsForeignIdentifier": false,
    "containsEnumIdentifier": false,
    "sequenceGeneratorDefinition": null,
    "changeTrackingPolicy": 1,
    "isVersioned": false,
    "versionField": null,
    "isReadOnly": false,
    "entityListeners": [],
    "associationMappings": {
        "author": {
            "cascade": [],
            "fetch": 2,
            "inherited": null,
            "declared": null,
            "cache": null,
            "id": null,
            "isOnDeleteCascade": true,
            "originalClass": null,
            "originalField": null,
            "orphanRemoval": false,
            "unique": null,
            "fieldName": "author",
            "sourceEntity": "App\\Entity\\Book",
            "targetEntity": "App\\Entity\\Author",
            "inversedBy": "books",
            "indexBy": null,
            "orderBy": [],
            "joinTable": {
                "quoted": null,
                "joinColumns": [
                    {
                        "deferrable": null,
                        "unique": null,
                        "quoted": null,
                        "fieldName": null,
                        "onDelete": "CASCADE",
                        "columnDefinition": null,
                        "nullable": null,
                        "options": null,
                        "name": "book_id",
                        "referencedColumnName": "id"
                    }
                ],
                "inverseJoinColumns": [
                    {
                        "deferrable": null,
                        "unique": null,
                        "quoted": null,
                        "fieldName": null,
                        "onDelete": "CASCADE",
                        "columnDefinition": null,
                        "nullable": null,
                        "options": null,
                        "name": "author_id",
                        "referencedColumnName": "id"
                    }
                ],
                "options": [],
                "schema": null,
                "name": "book_author"
            },
            "joinTableColumns": [
                "book_id",
                "author_id"
            ],
            "relationToSourceKeyColumns": {
                "book_id": "id"
            },
            "relationToTargetKeyColumns": {
                "author_id": "id"
            }
        }
    },
    "fieldMappings": {
        "id": {
            "length": null,
            "id": true,
            "nullable": false,
            "notInsertable": null,
            "notUpdatable": null,
            "columnDefinition": null,
            "generated": null,
            "enumType": null,
            "precision": null,
            "scale": null,
            "unique": false,
            "index": false,
            "inherited": null,
            "originalClass": null,
            "originalField": null,
            "quoted": null,
            "declared": null,
            "declaredField": null,
            "options": null,
            "version": null,
            "default": null,
            "type": "integer",
            "fieldName": "id",
            "columnName": "id"
        },
        "title": {
            "length": 255,
            "id": null,
            "nullable": false,
            "notInsertable": null,
            "notUpdatable": null,
            "columnDefinition": null,
            "generated": null,
            "enumType": null,
            "precision": null,
            "scale": null,
            "unique": false,
            "index": false,
            "inherited": null,
            "originalClass": null,
            "originalField": null,
            "quoted": null,
            "declared": null,
            "declaredField": null,
            "options": null,
            "version": null,
            "default": null,
            "type": "string",
            "fieldName": "title",
            "columnName": "title"
        },
        "price": {
            "length": null,
            "id": null,
            "nullable": false,
            "notInsertable": null,
            "notUpdatable": null,
            "columnDefinition": null,
            "generated": null,
            "enumType": null,
            "precision": null,
            "scale": null,
            "unique": false,
            "index": false,
            "inherited": null,
            "originalClass": null,
            "originalField": null,
            "quoted": null,
            "declared": null,
            "declaredField": null,
            "options": null,
            "version": null,
            "default": null,
            "type": "integer",
            "fieldName": "price",
            "columnName": "price"
        },
        "topics": {
            "length": null,
            "id": null,
            "nullable": true,
            "notInsertable": null,
            "notUpdatable": null,
            "columnDefinition": null,
            "generated": null,
            "enumType": null,
            "precision": null,
            "scale": null,
            "unique": false,
            "index": false,
            "inherited": null,
            "originalClass": null,
            "originalField": null,
            "quoted": null,
            "declared": null,
            "declaredField": null,
            "options": null,
            "version": null,
            "default": null,
            "type": "json",
            "fieldName": "topics",
            "columnName": "topics"
        }
    }
}

which we can then filter using jq - for example :

symfony console doctrine:mapping:describe App\\Entity\\Book --format=json | jq '.fieldMappings | to_entries | map({(.key): .value.type}) | add'

will output a simple fieldMapping + typehint list
image

Bonus small fix of embeddedClasses output which falsely returned subClasses

@stlgaits stlgaits marked this pull request as ready for review July 11, 2025 10:07
@greg0ire

This comment was marked as resolved.

@stlgaits stlgaits force-pushed the mapping-command-json-output branch from dc3fb09 to 021c815 Compare July 11, 2025 14:34
@stlgaits
Copy link
Contributor Author

I've just updated the command help / description + added autocomplete for --format option (with text as default )

@greg0ire

This comment was marked as resolved.

@greg0ire

This comment was marked as resolved.

@stlgaits stlgaits force-pushed the mapping-command-json-output branch from 021c815 to f68b1d3 Compare July 12, 2025 11:30
@@ -55,6 +56,7 @@ protected function configure(): void
->addArgument('entityName', InputArgument::REQUIRED, 'Full or partial name of entity')
->setDescription('Display information about mapped objects')
->addOption('em', null, InputOption::VALUE_REQUIRED, 'Name of the entity manager to operate on')
->addOption('format', null, InputOption::VALUE_REQUIRED, 'Output format (json, text)', 'text')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There seems to be an indention problem here.

Suggested change
->addOption('format', null, InputOption::VALUE_REQUIRED, 'Output format (json, text)', 'text')
->addOption('format', null, InputOption::VALUE_REQUIRED, 'Output format (json, text)', 'text')

@@ -72,7 +83,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int

$entityManager = $this->getEntityManager($input);

$this->displayEntity($input->getArgument('entityName'), $entityManager, $ui);
$this->displayEntity($input->getArgument('entityName'), $entityManager, $ui, $input->getOption('format'));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think $input->getOption('format') should be parsed into an enum representing the possible formats, or at least validated against getAvailableFormatOptions

@@ -100,9 +115,47 @@ private function displayEntity(
string $entityName,
EntityManagerInterface $entityManager,
SymfonyStyle $ui,
string|null $format,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is null really necessary? Or could text be the default?


To use a specific entity manager (e.g., for multi-DB projects), use the <info>--em</info> option:

<info>%command.full_name% My\Namespace\Entity\MyEntity --em=my_custom_entity_manager</info>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should go in a separate commit.

@@ -115,7 +168,7 @@ private function displayEntity(
$this->formatField('Embedded class?', $metadata->isEmbeddedClass),
$this->formatField('Parent classes', $metadata->parentClasses),
$this->formatField('Sub classes', $metadata->subClasses),
$this->formatField('Embedded classes', $metadata->subClasses),
$this->formatField('Embedded classes', $metadata->embeddedClasses),
Copy link
Member

@greg0ire greg0ire Jul 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should go to a separate commit. If it's a bugfix, it should even go to a separate branch (maybe even a 2.x branch)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants