Coder Social home page Coder Social logo

Comments (3)

kemingy avatar kemingy commented on May 29, 2024

Thanks for your feedback.

Refer to the code

spectree/spectree/spec.py

Lines 345 to 356 in 3d5f31a

def _get_model_definitions(self) -> Dict[str, Any]:
"""
handle nested models
"""
definitions = {}
for name, schema in self.models.items():
if "definitions" in schema:
for key, value in schema["definitions"].items():
definitions[f"{name}.{key}"] = value
del schema["definitions"]
return definitions

It's possible to remove the duplicate schema. Let's you can guarantee that there is no Schema with the same name. Then you can change the key format in definitions to f'{key}' and change the value in the properties.xxx.items.$ref.

This time you should be able to inherit and override the behavior.

from spectree.

AndreasBBS avatar AndreasBBS commented on May 29, 2024

Hi, thanks for the quick reply. I'm going to try for this refactor. I had already been looking a bit at the code to try and figure out where I would need to change things. From my tests I need to change where you said and here:

spectree/spectree/utils.py

Lines 232 to 244 in 3d5f31a

def get_model_schema(model: ModelType, naming_strategy: NamingStrategy = get_model_key):
"""
return a dictionary representing the model as JSON Schema with a hashed
infix in ref to ensure name uniqueness
:param model: `pydantic.BaseModel` query, json, headers or cookies from
request or response
"""
assert issubclass(model, BaseModel)
return model.schema(
ref_template=f"#/components/schemas/{naming_strategy(model)}.{{model}}"
)

But given this last change is in the utils.py I don't think I can define this behavior as a staticmethod in the Spectree class. Otherwise, when I import Spectree in utils to use the method Spectree.new_behavior_static_method it'll cause a circular import because spec.py imports utils.py.

from spectree.

AndreasBBS avatar AndreasBBS commented on May 29, 2024

I made another strategy implementation and you can check the merge here #305

If you want to test the example above with my branch this is how it works with the change:

from pydantic import BaseModel
from falcon.asgi import App
from spectree import SpecTree, Response

app = App(
    cors_enable=True
)

spec = SpecTree(
    'falcon-asgi',
    title = 'DEMO',
    version = 'v0.1',
    naming_strategy=lambda model: f"{model.__name__}",
    nested_naming_strategy=lambda _, child: child 
)

class Pet(BaseModel):
    name: str

class Person(BaseModel):
    pets: list[Pet]

class People(BaseModel):
    people: list[Person]

class Pets(BaseModel):
    pets: list[Pet]

class PeopleResource:
    @spec.validate(resp=Response(HTTP_200=People))

    async def on_get(self):
        pass

class PetsResource:
    @spec.validate(resp=Response(HTTP_200=Pets))

    async def on_get(self):
        pass

app.add_route('/people', PeopleResource())
app.add_route('/pets', PetsResource())

spec.register(app)

This removes the hash from the root models and removes the parent from the nested models.

from spectree.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.