Skip to content

Allow full schema customization - #36

Open
197g wants to merge 5 commits into
Maillol:mainfrom
197g:customize-schema
Open

Allow full schema customization#36
197g wants to merge 5 commits into
Maillol:mainfrom
197g:customize-schema

Conversation

@197g

@197g 197g commented Apr 26, 2022

Copy link
Copy Markdown

Adds a full callback hook to customize schema after the type annotations have been processed, not unlike pydantics own __modify_schema__ customization point. The goal here is to provide a suitable fallback for arbitrary modifications to the OpenAPI specification, in case the provided attribute processing is not yet ready for the details. The alternative, implementing the full available OpenAPI fields, is unlikely to be feasible and not readily translate to annotations.

Comment thread aiohttp_pydantic/oas/view.py
@codecov-commenter

codecov-commenter commented Apr 26, 2022

Copy link
Copy Markdown

Codecov Report

Merging #36 (0afbd28) into main (3648dde) will increase coverage by 0.07%.
The diff coverage is 83.33%.

❗ Current head 0afbd28 differs from pull request most recent head 1bcfa1a. Consider uploading reports for the commit 1bcfa1a to get more accurate results

@@            Coverage Diff             @@
##             main      #36      +/-   ##
==========================================
+ Coverage   93.12%   93.20%   +0.07%     
==========================================
  Files          11       11              
  Lines         771      780       +9     
==========================================
+ Hits          718      727       +9     
  Misses         53       53              
Impacted Files Coverage Δ
aiohttp_pydantic/oas/struct.py 91.02% <66.66%> (+0.11%) ⬆️
aiohttp_pydantic/oas/view.py 94.69% <100.00%> (+0.14%) ⬆️
aiohttp_pydantic/injectors.py 96.00% <0.00%> (+0.08%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 3648dde...1bcfa1a. Read the comment docs.

@197g
197g force-pushed the customize-schema branch from f773c73 to 4bbcbb1 Compare April 29, 2022 12:30
self._spec = spec.setdefault("components", {})

@property
def examples(self) -> dict:

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Should return an object with attribute summary, description, value ...
https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.0.md#example-object

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

To clarify, you'd like me to add a class abstraction similar to the other structs?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I had followed what the schemas method was doing. Maybe the usage differs significantly enough or is there a better reaon to make the interface not consistent across them?

Comment thread demo/customize.py Outdated
Comment thread demo/customize.py Outdated
@Maillol

Maillol commented Apr 30, 2022

Copy link
Copy Markdown
Owner

Please, in the readme, add paragraph "Custom Open API Schema" between "Custom Validation error" and "Demo" paragraphs.
and a unittest

@197g 197g left a comment

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Some maintainer input on this interface decision would be good for consistency and to ensure future developments follows similar concepts.

Comment thread README.rst
attribute, which is invoked as a callback each time the schema is generated.

.. code-block:: python3
def add_custom_example(oas, oas_path, view, oas_operation):

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Actually, this brings up a bit of an unresolved interface problem. These 4 parameters are somewhat arbitrary. Would it be better to provide a namedtuple so that additional context can be added later, if it turns out that it was missing?

@197g 197g May 2, 2022

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Most straightforward, imho, would be to differentiate between the item that the modification should be performed on and the remaining context as a namespace (containing the three other parameters as attributes):

def add_custom_example(ctx, oas_operation):
    ctx.oas.components.setdefault( //

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