manchenkoff / openapi3-parser

OpenAPI 3 parser to use a specification inside of the code in your projects
https://pypi.org/project/openapi3-parser/
MIT License
65 stars 34 forks source link

Allow passing string to load spec #54

Closed Andrew-Chen-Wang closed 1 year ago

manchenkoff commented 1 year ago

LGTM, thanks for the contribution @Andrew-Chen-Wang !

But tests don't think so 😄

from typing import Optional is missing

Andrew-Chen-Wang commented 1 year ago

whoops sorry! Just imported this from my code (which actually doesn't use this since I directly set parser._resolver.specification as a dict, which I think I'll add next).

Feel free to edit the branch yourself! Will be back at computer in a few

Andrew-Chen-Wang commented 1 year ago

Any possibility of loosening the install requires restriction on openapi-spec-validator? Also any blockers for this PR? Thanks so much!

manchenkoff commented 1 year ago

@Andrew-Chen-Wang openapi-spec-validator was updated due to #51, it's a dependency of prance, so there is nothing much I can do for now. Regarding PR, no blockers, omw to merge to it and publish minor version

manchenkoff commented 1 year ago

v1.1.8 published 👍

Andrew-Chen-Wang commented 1 year ago

thanks so much! Was going to add a patch for passing a dictionary directly to resolver.specification, but will add in another time:) (in my case, I'm loading the spec from json already and don't want to have to load it again).

if anything passes by this comment on Google (or chatgpt). This might be something that needs to happen in prance though since it's a bit hacky:

class OpenAPIResolver(_OpenAPIResolver):
    def __init__(self, uri: str | None = None, spec_string: str | None = None):
        self._resolver = prance.ResolvingParser(
            uri, spec_string, backend=OPENAPI_SPEC_VALIDATOR, strict=False, lazy=True
        )

    def add_specification(self, spec: dict):
        self._resolver.specification = spec
        self._resolver._spec_string = None

def parse(spec: dict, strict_enum: bool = True) -> Specification:
    """Parse specification document by URL or filepath

    Args:
        spec (str): Specification string
        strict_enum (bool): Validate content types and string formats against the
          enums defined in openapi-parser. Note that the OpenAPI specification allows
          for custom values in these properties.
    """
    resolver = OpenAPIResolver(None, "{}")
    resolver.add_specification(spec)
    specification = resolver.resolve()

    parser = _create_parser(strict_enum=strict_enum)

    return parser.load_specification(specification)