Skip to content

openapi enum support - #123

Merged
alanpoulain merged 1 commit into
api-platform:mainfrom
PawelSuwinski:openapi_enum
Sep 23, 2022
Merged

openapi enum support#123
alanpoulain merged 1 commit into
api-platform:mainfrom
PawelSuwinski:openapi_enum

Conversation

@PawelSuwinski

@PawelSuwinski PawelSuwinski commented Sep 19, 2022

Copy link
Copy Markdown
Contributor

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
Copy Markdown
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
Copy Markdown
Contributor Author

Tests seem to fail.

Fixed.

@PawelSuwinski

Copy link
Copy Markdown
Contributor Author

Added enum test case.

@dunglas

dunglas commented Sep 19, 2022

Copy link
Copy Markdown
Member

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

This can also be interesting:

@alanpoulain

Copy link
Copy Markdown
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.

Comment thread src/openapi3/handleJson.ts
@PawelSuwinski

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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