← Back to issue list

feat: add inheritance-based validation and schemas

View original Github issue

Metadata

Project
snapcraft
Number
#5730
Type
pull request
State
merged
Author
EdmilsonRodrigues
Labels
—
Created
Updated
Closed

Current evaluation

Merged as commit 7cfb25977, adding inheritance-based pydantic validation and JSON schema generation (tools/schema.py, snapcraft/models/project.py) for snapcraft.yaml, closing issue #5543.

Resolution: closed resolved

Reason: PR #5730 was approved by two reviewers and merged to main as commit 7cfb25977 ("feat: add inheritance-based validation and schemas (#5730)"), adding SnapcraftProject submodels, schema generation and validate-schema/diff-schema CI jobs. It fixed issue #5543, and its 8.13.0 rollout was later reverted by #5857 due to model-selection regressions, with those fixed in #5846 (3249bed8e).

Evaluated by: mr-cal-bot

No scores available.

Issue body

- [X] Have you followed the [guidelines for contributing](https://github.com/canonical/snapcraft/blob/main/CONTRIBUTING.md)? - [X] Have you signed the [CLA](http://www.ubuntu.com/legal/contributors/)? - [x] Have you successfully run `make lint`? - [X] Have you successfully run `make test`? --- I created the new inheritance based Project validation, with the simplified scope discussed in the clinics. I took the charmcraft project as inspiration for the schema generation, and validation, used its pipeline as my base for the schema.yaml pipeline to assure that the schema file will always match the current state of the code. In the charmcraft project, there is a validation of the schema, made using files from the smoketests directory. We do not have a smoketests directory in the snapcraft project. Is there a place where there are files ready for this, should I add these files in a specific directory, or is it out of scope? For now that job is blocked so it won't be run. In the `Makefile`, I only added the task to generate the schema. It is to `new_snapcraft.json` so the reviewer can check if the generated json is in the right format, having the old one as a comparison. If the schema generation is approved, I will make a commit changing to `snapcraft.json` as it should be. In `snapcraft.models.__init__.py`, I only exported the new models, so they can be imported in the tests. In `snapcraft.models.project.py` I created the project models, and the validation logic, and moved some validation from the monolithic model to the new models. Also, I overrode the unmarshal function to validate the data through the TypeAdapter of `SnapcraftProject`. I had to use Discriminator functions, instead of field discriminators, because as this was an initial, with only three supported bases and two build bases, there should be a default fallback for the ones that are neither of them, but I used the enums, to generate them based on the Enum, as discussed. In `conftest.py` I made a small change in the fixture `setup_project` to use unmarshall instead of model_validate, so the correct submodel will be used, as specific validations are made by them. In `tests/unit/models/test_projects.py` I added a test for the unmarshaller, inside of the `TestProjectValidation` class, and changed a line that checked for "core22" base to check an instance of Core22Project, so mypy would not complain. In `tests/unit/services/test_buildplan.py` I just changed a Project.model_validate to Project.unmarshall to use the correct submodel. In `tools/schema.py` I based myself in the `tools/schema.py` of the charmcraft project. This PR is still not ready to be merged, since I need some guidance in relation to the validate-schema job, and also if the generated schema is compliant with the expectations. I would also like to know if there is anything missing, or anything that I got wrong so I can fix it. I did not change the docs, to document my change, because I was not sure if it is in the scope of this issue. I am also sorry for the delay in this delivery, but I will be alert, waiting for the comments, and review. Fixes #5543

Evaluation history

Date Model Scores Action Summary
stealth/space-bunny-alpha — closed resolved Merged as commit 7cfb25977, adding inheritance-based pydantic validation and JSON schema generation (tools/schema.py, snapcraft/models/project.py) for snapcraft.yaml, closing issue #5543.
stealth/space-bunny-alpha — closed resolved Merged #5730 (commit 7cfb25977), introducing inheritance-based project validation and generated JSON schemas, splitting the monolithic model, and fixing #5543.
stealth/space-bunny-alpha — closed resolved Merged as #5730 (commit 7cfb25977), introducing inheritance-based project validation and generated schemas, refactoring monolithic models into specialized submodels. Closes #5543.
stealth/space-bunny-alpha — closed resolved Merged in #5730 (commit 7cfb25977), adding inheritance-based project validation and generated JSON schemas; it fixes #5543 and addresses reviewer feedback.
qwen/qwen3.6-35b-a3b — — Merged. Introduces inheritance-based project validation and automated schema generation by refactoring monolithic models into specialized submodels. Incorporates reviewer feedback, updates test fixtures, and resolves transient CI failures. Closes #5543.
qwen3.6-35b-a3b-mtp-q6 — — Merged feature adding inheritance-based project validation and schema generation, resolving #5543. Reviewers dismissed unrelated test failures as network issues. Author incorporated feedback and completed the merge.

Update history

No update history recorded yet.

Related work

  • Likely Fixed By: canonical/snapcraft#5846 (confidence 80%)

    Follow-up fix merged as 3249bed8e correcting model selection introduced by this PR's changes.

  • Caused By: canonical/snapcraft#5857 (confidence 70%)

    Reverted the project model changes from this PR for 8.13.0; re-applied later in 063c3c89e.

Related issues

Issue Project State Summary Similarity
#3633 projects: add project model and validation (CRAFT-763) snapcraft merged Merged PR #3633 as d6636e12c, adding Pydantic models and validators for snapcraft.yaml, tests, and a package layout change to avoid cyclic imports.
75%
#3654 projects: add grammar validation snapcraft merged Merged #3654 via commit 17fcb85f8, validating snapcraft.yaml with craft-grammar Pydantic models before processing grammar-aware properties.
74%
#781 feat: JSON schema generation for parts craft-parts merged Merged JSON Schema generation for parts in commit 09a99811, adding Pydantic-based schemas and tests after prerequisite #778.
73%
#419 Properly validate the schema snapcraft merged Merged PR #419 in commit 116352b7d, refactoring schema validation to remove duplicated logic from snapcraft.yaml and fixing autopackage tests.
70%
#1806055 Snapcraft schema validation with bases snapcraft (launchpad) closed Fixed by snapcore/snapcraft#2473, merged as 3f7d63cdf, which improved Go/Python plugin schema validation and covered no-op part configurations.
70%
#5924 docs: clean up 'snapcraft.yaml' snapcraft merged Merged snapcraft.yaml documentation cleanup in #5924 via commit 83f961ebc, upgrading pydantic-kitbash, correcting docstrings and links, and regenerating the JSON schema.
70%