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

openapi enum support #123

Merged
merged 1 commit into from
Sep 23, 2022
Merged

Conversation

PawelSuwinski
Copy link
Contributor

@PawelSuwinski PawelSuwinski commented Sep 19, 2022

This is the opening of a discussion or a proposition of enum keyword support, at the moment only for openapi, I haven't explored how it could be realized in hydra or swagger.

If OpenApi docs contains enum property (api-platform, decorated api_platform.metadata.property.metadata_factory service) SwaggerUI renders it automatically in a nice way, for example:
obraz
obraz
but there is no way to get into it on any level using @api-platform/admin or @api-platform/api-doc-parser, docs need to be parsed again manually.

With this small change I can already have some EnumInput without need of parsing openapi json by hand:

export const EnumInput = (props) => {
  const resource = useResourceContext(props);
  const [choices, setChoices] = useState([]);
  const [validate, setValidate] = useState(props.validate ?? null);

  useEffect(() => {
    parseOpenApi3Documentation(window.origin + "/docs.json").then(({ api }) => {
      const field = api.resources
        .find((r) => r.name === resource)
        .fields.find(({ name }) => name === props.source);
      setChoices((field.enum ?? []).map((v) => ({ id: v, name: v })));
      !validate && field.required && setValidate([required()]);
    });
  }, []); // eslint-disable-line react-hooks/exhaustive-deps

  return (
    <SelectInput key={props.source} {...{ choices, validate }} {...props} />
  );
};

@alanpoulain
Copy link
Member

Sure, good idea!
Tests seem to fail.
Once it is merged, it would be great to have the support in the admin too.
Theoretically it's possible to do it with Hydra / RDF too but I don't know exactly how. Maybe @dunglas can help on this.

@PawelSuwinski
Copy link
Contributor Author

Tests seem to fail.

Fixed.

@PawelSuwinski
Copy link
Contributor Author

Added enum test case.

@dunglas
Copy link
Member

dunglas commented Sep 19, 2022

Regarding RDF, there is rdf:List: https://stackoverflow.com/a/32154403

This can also be interesting:

@alanpoulain
Copy link
Member

Maybe the best course of action would be to have a full Hydra enum support in API Platform before doing it here since we don't really know how it will be done.

@PawelSuwinski
Copy link
Contributor Author

Fixed for openapi, and added enum support with test case to swagger, as I figured out it share same syntax (openapi-types).

@PawelSuwinski
Copy link
Contributor Author

Platform before doing it here since we don't really know how it will be done.

I think that in a case of openapi/swagger on parser level there is not much room to move, enums are simple lists and that is all. I would be nice to cover it by api-doc-parser to have some method to cope with it some how just now without waiting for full integrated hydra solution, that I guess would be resolved mostly in core and admin components.

@alanpoulain alanpoulain force-pushed the openapi_enum branch 2 times, most recently from 74ae874 to 6754afa Compare September 23, 2022 13:08
@alanpoulain alanpoulain merged commit 934f32f into api-platform:main Sep 23, 2022
@alanpoulain
Copy link
Member

Thank you @PawelSuwinski!

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

Successfully merging this pull request may close these issues.

3 participants