Skip to content

OpenAPI 3.0 request schema generation#73

Open
Serbel97 wants to merge 3 commits into
Sibyx:masterfrom
Serbel97:feat/openapi-schema
Open

OpenAPI 3.0 request schema generation#73
Serbel97 wants to merge 3 commits into
Sibyx:masterfrom
Serbel97:feat/openapi-schema

Conversation

@Serbel97

@Serbel97 Serbel97 commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

Closes #72

What

Adds a dependency-free generate_form_schema() helper (new django_api_forms.openapi module, exported from the package root) that converts any Form class into an OpenAPI 3.0 Schema Object describing its JSON request body:

from django_api_forms import generate_form_schema

schema = generate_form_schema(AlbumForm)  # plain dict, ready to mount into an OpenAPI document

How

  • Field → schema conversion is dispatched through a class → handler registry resolved along each field's MRO, so custom subclasses inherit their parent's behaviour and unknown field types degrade gracefully to {} (any type) instead of raising
  • Django scalars map to proper type/format pairs (date-time, uuid, email, uri, ...) with minLength/maxLength/minimum/maximum constraints where defined
  • Library fields are fully covered: FormField recurses into nested objects, FormFieldList/FieldList become arrays with minItems/maxItems, EnumField/ChoiceField become enums, DictionaryField uses additionalProperties, FileField/ImageField become string/byte (Data URI payload)
  • required=True fields are collected into the object's required array; label/help_text are exported as title/description
  • Meta.mapping is respected, so property names match the JSON keys clients actually send (e.g. _name instead of name)

Scope

Deliberately schema-generation only — no bundled Swagger UI/ReDoc views and no new dependencies. The returned dict plugs into whatever documentation pipeline a project already uses; docs/openapi.md shows how to serve a complete OpenAPI document with Swagger UI/ReDoc from it.

Testing

  • 17 new unit tests in tests/test_openapi.py covering every supported field type, nesting, required propagation, Meta.mapping and the unknown-field fallback
  • flake8 clean, full suite passes

🤖 Generated with Claude Code

Adds a dependency-free generate_form_schema() helper which converts any
Form class into an OpenAPI 3.0 Schema Object describing its JSON request
body: scalar type/format mapping, nested FormField/FormFieldList objects,
FieldList arrays, EnumField/ChoiceField enums, DictionaryField
additionalProperties, required propagation and Meta.mapping-aware
property names. Unknown field types fall back to an untyped schema.

Closes Sibyx#72

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@codecov

codecov Bot commented Jul 7, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 96.23%. Comparing base (e27be39) to head (7ab2e6a).

Additional details and impacted files
@@            Coverage Diff             @@
##           master      #73      +/-   ##
==========================================
+ Coverage   95.09%   96.23%   +1.14%     
==========================================
  Files           9       10       +1     
  Lines         469      585     +116     
==========================================
+ Hits          446      563     +117     
+ Misses         23       22       -1     
Files with missing lines Coverage Δ
django_api_forms/__init__.py 100.00% <100.00%> (ø)
django_api_forms/fields.py 97.21% <100.00%> (+0.64%) ⬆️
django_api_forms/openapi.py 100.00% <100.00%> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Serbel97 and others added 2 commits July 7, 2026 15:48
Adds tests for non-string enums, grouped/boolean choices and
FormFieldList maxItems, bringing openapi.py to 100% coverage.
Introduces read-only properties (FieldList.field, min_length,
max_length, DictionaryField.value_field) so schema generation no
longer reaches into protected members.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
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.

Auto-generate Swagger/OpenAPI docs

1 participant