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

Admin interface not rendering dropdown for Enum on HydraAdmin #494

Open
mihai-amihailesei opened this issue Jan 29, 2023 · 2 comments
Open

Comments

@mihai-amihailesei
Copy link

API Platform version(s) affected: 3.1.1

Description
HydraAdmin component does not render an Enum type field as a dropdown selection. OpenApiAdmin component renders correctly.

How to reproduce
Create an entity and a backed Enum, set the backed enum as a property on the entity. Configure the property openapi context with enum and values from the backed enum.

API documentation properly shows the property as an enum on both schemas, however, when rendered with the HydraAdmin component it gets rendered as a plain text field.

https://github.com/mihai-amihailesei/api-platform-admin-bug

@alanpoulain
Copy link
Member

It's because API Platform doesn't return information about the enum with Hydra.

@PawelSuwinski
Copy link
Contributor

A workaround for now to have OpenAPi3 enums with Hydra is a proxy of Hydra doc parser, ex.

import {
  parseOpenApi3Documentation,
  parseHydraDocumentation,
} from "@api-platform/api-doc-parser";

// parseHydraDocumentation proxy to add enum fields values what for now is
// supported only in openapi parser.
export const parseHydraDocumentationWithEnums = new Proxy(
  parseHydraDocumentation,
  {
    async apply(target, thisArg, args) {
      const result = await target.apply(thisArg, args);
      const enums = (
        await parseOpenApi3Documentation(args[0] + "docs.json")
      ).api.resources.reduce(
        (enums, resource) =>
          Object.assign(enums, {
            [resource.name]: Object.fromEntries(
              resource.fields
                .filter((field) => field.enum)
                .map((field) =>
                  Object.assign(enums[resource.name] ?? {}, [
                    field.name,
                    { enum: field.enum },
                  ])
                )
            ),
          }),
        {}
      );
      result.api.resources = result.api.resources.map((resource) =>
        Object.assign(resource, {
          fields: resource.fields.map((field) =>
            Object.assign(field, enums[resource.name][field.name] ?? {})
          ),
        })
      );

      return result;
    },
  }
);

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

No branches or pull requests

3 participants