-
-
Notifications
You must be signed in to change notification settings - Fork 470
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Doc, Guide and Test. Change SCHEMA_GENERATION_CLASS to SCHEMA_GEN…
…ERATOR_CLASS
- Loading branch information
Showing
7 changed files
with
135 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# Custom Schema Generator | ||
|
||
**Django Ninja** allows you to custom schema generator by change `NINJA_SCHEMA_GENERATOR_CLASS` in your settings.py | ||
|
||
Example Full Qualified Schema Name: | ||
|
||
```python | ||
from ninja.schema import NinjaGenerateJsonSchema | ||
from pydantic.json_schema import CoreModeRef, DefsRef | ||
|
||
|
||
class CustomNinjaGenerateJsonSchema(NinjaGenerateJsonSchema): | ||
|
||
def get_defs_ref(self, core_mode_ref: CoreModeRef) -> DefsRef: | ||
module_qualname_occurrence_mode = super().get_defs_ref(core_mode_ref) | ||
name_choices = self._prioritized_defsref_choices[module_qualname_occurrence_mode] | ||
name_choices.pop(0) | ||
name_choices.pop(0) | ||
self._prioritized_defsref_choices[module_qualname_occurrence_mode] = name_choices | ||
return module_qualname_occurrence_mode | ||
``` | ||
|
||
In your `settings.py`: | ||
|
||
```python | ||
NINJA_SCHEMA_GENERATOR_CLASS = 'path.to.CustomNinjaGenerateJsonSchema' | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import pytest | ||
from pydantic.json_schema import CoreModeRef, DefsRef | ||
|
||
from ninja import NinjaAPI | ||
from ninja.conf import settings | ||
from ninja.schema import Field, NinjaGenerateJsonSchema, Schema | ||
|
||
|
||
class FullSchemaNameGenerator(NinjaGenerateJsonSchema): | ||
def get_defs_ref(self, core_mode_ref: CoreModeRef) -> DefsRef: | ||
temp = super().get_defs_ref(core_mode_ref) | ||
choices = self._prioritized_defsref_choices[temp] | ||
choices.pop(0) | ||
choices.pop(0) | ||
self._prioritized_defsref_choices[temp] = choices | ||
return temp | ||
|
||
|
||
class Payload(Schema): | ||
i: int | ||
f: float | ||
|
||
|
||
def to_camel(string: str) -> str: | ||
return "".join(word.capitalize() for word in string.split("_")) | ||
|
||
|
||
class Response(Schema): | ||
i: int | ||
f: float = Field(..., title="f title", description="f desc") | ||
|
||
class Config(Schema.Config): | ||
alias_generator = to_camel | ||
populate_by_name = True | ||
|
||
|
||
@pytest.fixture(scope="function") | ||
def schema(): | ||
# setup up | ||
settings.SCHEMA_GENERATOR_CLASS = ( | ||
"tests.test_custom_schema_generator.FullSchemaNameGenerator" | ||
) | ||
api = NinjaAPI() | ||
|
||
@api.post("/test", response=Response) | ||
def method(request, data: Payload): | ||
return data.dict() | ||
|
||
yield api.get_openapi_schema() | ||
# reset | ||
settings.SCHEMA_GENERATOR_CLASS = "ninja.schema.NinjaGenerateJsonSchema" | ||
|
||
|
||
def test_full_name_schema(schema): | ||
method = schema["paths"]["/api/test"]["post"] | ||
|
||
assert method["requestBody"] == { | ||
"content": { | ||
"application/json": { | ||
"schema": { | ||
"$ref": "#/components/schemas/test_custom_schema_generator__Payload" | ||
} | ||
} | ||
}, | ||
"required": True, | ||
} | ||
assert method["responses"] == { | ||
200: { | ||
"content": { | ||
"application/json": { | ||
"schema": { | ||
"$ref": "#/components/schemas/test_custom_schema_generator__Response" | ||
} | ||
} | ||
}, | ||
"description": "OK", | ||
} | ||
} | ||
assert schema.schemas == { | ||
"test_custom_schema_generator__Response": { | ||
"title": "Response", | ||
"type": "object", | ||
"properties": { | ||
"i": {"title": "I", "type": "integer"}, | ||
"f": {"description": "f desc", "title": "f title", "type": "number"}, | ||
}, | ||
"required": ["i", "f"], | ||
}, | ||
"test_custom_schema_generator__Payload": { | ||
"title": "Payload", | ||
"type": "object", | ||
"properties": { | ||
"i": {"title": "I", "type": "integer"}, | ||
"f": {"title": "F", "type": "number"}, | ||
}, | ||
"required": ["i", "f"], | ||
}, | ||
} |