Keep generated code honest — make it again in CI
Generated files rot the moment somebody edits one by hand. One CI step makes that impossible.
Generated code has one failure, and it is always the same: somebody opens the file, fixes a field, and commits it. Now the file and its source disagree, and the next person who runs the generator wipes the fix.
The cure is a rule a machine can enforce.
The step
Generate the files again in CI and fail if anything changed:
npm run generate
git diff --exit-code generated/
git diff --exit-code returns 1 when there is a difference. That is the whole
check. If a developer edited a generated file by hand, the diff shows it and
the build stops with the evidence on screen.
In GitHub Actions:
- run: npm run generate
- name: The generated files must match the schemas
run: git diff --exit-code generated/
What npm run generate does
Use the quicktype command line tool, which is the same engine this site runs in your browser:
npx quicktype \
--src schemas/user.schema.json --src-lang schema \
--lang typescript --just-types \
--out generated/user.ts
One line for each language. Put them in a script so there is one command to run:
{
"scripts": {
"generate": "node scripts/generate.mjs"
}
}
Open this example in the tool →
Keep the options in that script, not in people's heads. The option set is part
of the contract: --just-types in one service and not in another gives you
two different files from one schema, and an argument about which is correct.
Make the file say it is generated
Every generator writes a header. Keep it:
// Code generated from JSON Schema using quicktype. DO NOT EDIT.
That exact Go wording is a convention tools understand: golangci-lint and
others will skip the file. Other languages have their own: C# uses
<auto-generated />, which makes Visual Studio and most analysers leave it
alone.
Add generated/ to .gitattributes with linguist-generated=true and GitHub
collapses those files in a pull request, so a reviewer looks at the schema
change instead of three thousand lines of output.
Commit the output, or generate at build time?
Both work. Pick one and write down why.
Commit it when you want the code review to show what changed in every language, when your build must work without network access, and when a new developer should be able to open the project and see real code. This is the common choice.
Generate at build time when the output is large and nobody reads it, and when you already have a reliable build step. You avoid a lot of diff noise.
What does not work is half of each: generating at build time and committing the result, with no check that they agree.
Fail on a schema change too
The second useful check is for an API you do not own. Fetch the API's current schema or a known sample in a nightly job, generate from it, and fail if the output differs from what is committed. You learn about a change in the API on the day it happens, not on the day a user hits it.
What to do with the field somebody wanted to add by hand
They had a reason. Usually one of these:
- the schema is wrong — fix the schema;
- they need a method on the type — put it in a different file, as an extension or a wrapper;
- the generator cannot express it — write that type by hand, in its own file,
outside
generated/, and say so in a comment.
All three end with the generated folder still untouched.
The smallest version
If you do nothing else: add git diff --exit-code after your generate step.
It is one line, it has no dependencies, and it catches the only fault that
really matters.
Try it on your own JSON. The generator runs in your browser. Nothing is uploaded, and nothing is stored on a server.
Open JSON to CodeRead next
The API changed. Now what?
A new field, a removed field, a type that flipped. Which changes break your generated code, which break it quietly, and how to find out first.
From a sample, to a schema, to code
The way to use a generator that still works in six months: make the schema once, read it, commit it, and make the code from it every time.