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

[HTTP/OAS] Support specifying content types #181697

Merged

Conversation

jloleysens
Copy link
Contributor

@jloleysens jloleysens commented Apr 25, 2024

Summary

Related to #180056

Adds the ability to specify a content type for the request / response of an API. The current approach is to use the options.accepts value already present on the route but to also add the ability to specify a content type per response, otherwise fallback to application/json.

Notes

  • Fixed an issue where we might not be iterating over response codes correctly (and updated test cases)
  • Refactored the generate_oas.ts script to be more maintainable (apologies for the noise)

@jloleysens jloleysens added Feature:http Team:Core Core services & architecture: plugins, logging, config, saved objects, http, ES client, i18n, etc release_note:skip Skip the PR/issue when compiling release notes v8.15.0 labels Apr 25, 2024
@jloleysens jloleysens self-assigned this Apr 25, 2024
@jloleysens jloleysens changed the title [HTTP/OAS] Support different content types [HTTP/OAS] Support specifying content types for responses Apr 25, 2024
@jloleysens jloleysens changed the title [HTTP/OAS] Support specifying content types for responses [HTTP/OAS] Support specifying content types Apr 25, 2024
@@ -62,6 +62,7 @@ const getRouterDefaults = () => ({
200: {
body: schema.string({ maxLength: 10, minLength: 1 }),
},
unsafe: { body: true },
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added to make the tests more representative

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Refactored this file to make things a bit more maintainable

Comment on lines 42 to 43
} else if ((schema.type as string) === 'stream') {
mutations.processStream(schema);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's possible that a response body is a stream, in this case we just map it to object which is similar to "any" in OAS. Not sure there is good way to represent this at present.

@jloleysens
Copy link
Contributor Author

/ci

@jloleysens
Copy link
Contributor Author

/ci

@jloleysens jloleysens marked this pull request as ready for review April 29, 2024 14:17
@jloleysens jloleysens requested a review from a team as a code owner April 29, 2024 14:17
@elasticmachine
Copy link
Contributor

Pinging @elastic/kibana-core (Team:Core)

return route.handlers.reduce<OpenAPIV3.ResponsesObject>((acc, handler) => {
const schemas = extractValidationSchemaFromVersionedHandler(handler);
if (!schemas?.response) return acc;
const { unsafe, ...responses } = schemas.response;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is fix/change that is obscured due to refactor. Ensure that we are looping over status code-like key values only.

const fullConfig = getResponseValidation(route.validationSchemas);

if (fullConfig) {
const { unsafe, ...validationSchemas } = fullConfig;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is fix/change that is obscured due to refactor. Ensure that we are looping over status code-like key values only.

@jloleysens jloleysens enabled auto-merge (squash) April 30, 2024 12:55
@kibana-ci
Copy link
Collaborator

💚 Build Succeeded

Metrics [docs]

✅ unchanged

History

To update your PR or re-run it, just comment with:
@elasticmachine merge upstream

cc @jloleysens

@jloleysens jloleysens merged commit bef5007 into elastic:main May 2, 2024
17 checks passed
@kibanamachine kibanamachine added the backport:skip This commit does not require backporting label May 2, 2024
@jloleysens jloleysens deleted the http-oas/support-different-content-types branch May 2, 2024 10:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport:skip This commit does not require backporting Feature:http release_note:skip Skip the PR/issue when compiling release notes Team:Core Core services & architecture: plugins, logging, config, saved objects, http, ES client, i18n, etc v8.15.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants