Skip to content

add middleware to convert all requests with yaml content type to json#29

Closed
Adamkadaban wants to merge 5 commits intomainfrom
adam-route-accept-yaml
Closed

add middleware to convert all requests with yaml content type to json#29
Adamkadaban wants to merge 5 commits intomainfrom
adam-route-accept-yaml

Conversation

@Adamkadaban
Copy link
Member

@Adamkadaban Adamkadaban commented Feb 8, 2025

Checklist

  • I have added a version label to my PR (e.g., patch, minor, major).
  • I have tested my changes locally and verified they work as expected.
  • I have added relevant tests to cover my changes.
  • I have made any necessary updates to the documentation.

Description

This PR will allow users to supply yaml, which will be converted to JSON

This adds a FastAPI middleware to convert all requests with a content-type of yaml to one with a json body, content-type, etc.

Content type of application/yaml is used as per RFC 9512.

I ended up going for content-type instead of the get param ?yaml=true, as curl, fastapi test client, requests library, etc. all implicitly will set the content-type to json when a user provides json. I think this is better, since ?yaml=false won't have to be provided to requests.

Partially Fixes: #24
Note: There is currently no support for subnet-level tagging

@Adamkadaban Adamkadaban added the minor Increment the minor version when merged label Feb 8, 2025
@Adamkadaban
Copy link
Member Author

@alexchristy it seems that I can set up manual scoping bc request in the middleware has a url attribute.

So I can make a set of routes that are allowed here and put it in some config file somewhere.

Where do you think that config should go? I also think we may want to put the middleware in a separate file than main.py

@alexchristy
Copy link
Member

@alexchristy it seems that I can set up manual scoping bc request in the middleware has a url attribute.

So I can make a set of routes that are allowed here and put it in some config file somewhere.

Where do you think that config should go? I also think we may want to put the middleware in a separate file than main.py

I was thinking in a folder src/app/middleware and we could call it something along the lines of yaml_to_json_middleware.py. Source: https://github.com/igorbenav/FastAPI-boilerplate/tree/main/src/app/middleware

@Adamkadaban
Copy link
Member Author

looks like I don't even have to do anything weird with the url field. I can just make the middleware only for the templates router
https://gist.github.com/geospatial-jeff/17d677202f1223eacd3b32960ac29c60

@Adamkadaban
Copy link
Member Author

Closing this in favor of new branch to make PR easier

@Adamkadaban Adamkadaban closed this Mar 9, 2025
@Adamkadaban Adamkadaban deleted the adam-route-accept-yaml branch March 11, 2025 01:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

minor Increment the minor version when merged

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Allow YAML to be accepted in API routes. Convert YAML to JSON

2 participants