Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/test-pr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ jobs:
matrix:
fixture:
- typescript,typescript-zod,schema-typescript-zod,typescript-effect-schema
- javascript,schema-javascript
- javascript,schema-javascript,schema-description-unification
- golang,schema-golang
- cjson,schema-cjson
- cjson-default,cjson-multi-header,cjson-multi-split
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,10 @@ export class JSONSchemaRenderer extends ConvenienceRenderer {
const req: string[] = [];
for (const [name, p] of o.getProperties()) {
const prop = this.schemaForType(p.type);
if (prop.description === undefined) {
addDescriptionToSchema(
prop,
this.descriptionForClassProperty(o, name),
);
}
addDescriptionToSchema(
prop,
this.descriptionForClassProperty(o, name),
);

props[name] = prop;
if (!p.isOptional) {
Expand Down
71 changes: 71 additions & 0 deletions test/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -929,6 +929,76 @@ class JSONSchemaFixture extends LanguageFixture {
}
}

// Regression fixture for #2801. It verifies schema-specific output that
// cannot be observed by compiling and running code generated from a JSON Schema.
class JSONSchemaToSchemaFixture extends Fixture {
readonly name = "schema-description-unification";

constructor() {
super(languages.TypeScriptLanguage);
}

getSamples(sources: string[]): { priority: Sample[]; others: Sample[] } {
return samplesFromSources(
sources,
["test/inputs/schema/description-unification.schema"],
[],
"schema",
);
}

async runWithSample(
sample: Sample,
index: number,
total: number,
): Promise<void> {
const cwd = this.getRunDirectory();
const message = this.runMessageStart(sample, index, total, cwd, false);
const output = path.join(cwd, "schema.json");
mkdirs(cwd);

await quicktype({
src: [sample.path],
srcLang: "schema",
lang: "schema",
topLevel: "DescriptionUnification",
out: output,
telemetry: "disable",
});

const schema = JSON.parse(fs.readFileSync(output, "utf8")) as {
definitions: Record<
string,
{ properties: Record<string, { description?: unknown }> }
>;
};
const expectedDescriptions = [
["Allow", "labels", "The labels to apply to the policy"],
[
"Metadata",
"annotations",
"Additional annotations for the generated resource",
],
] as const;

for (const [definition, property, expected] of expectedDescriptions) {
const actual =
schema.definitions[definition]?.properties[property]
?.description;
if (actual !== expected) {
failWith("Schema property has the wrong description", {
definition,
property,
expected,
actual,
});
}
}

this.runMessageEnd(message, expectedDescriptions.length);
}
}

// `leadingComments` is a quicktype-core API option, so the CLI fixture path
// cannot exercise it.
class LeadingCommentsGoFixture extends JSONSchemaFixture {
Expand Down Expand Up @@ -1785,6 +1855,7 @@ export const allFixtures: Fixture[] = [
new JSONFixture(languages.ElixirLanguage),
new JSONSchemaJSONFixture(languages.CSharpLanguage),
new JSONTypeScriptFixture(languages.CSharpLanguage),
new JSONSchemaToSchemaFixture(),
// new JSONSchemaFixture(languages.CrystalLanguage),
new JSONSchemaFixture(languages.JSONSchemaLanguage),
new JSONSchemaFixture(languages.CSharpLanguage),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"allow": {
"labels": {
"env": "production"
}
}
}
14 changes: 14 additions & 0 deletions test/inputs/schema/description-unification.1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"allow": {
"labels": {
"env": "production",
"team": "core"
}
},
"metadata": {
"annotations": {
"note": "generated by quicktype",
"owner": "platform"
}
}
}
34 changes: 34 additions & 0 deletions test/inputs/schema/description-unification.schema
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "DescriptionUnification",
"type": "object",
"properties": {
"allow": {
"type": "object",
"properties": {
"labels": {
"description": "The labels to apply to the policy",
"type": "object",
"additionalProperties": {
"type": "string"
}
}
},
"required": ["labels"]
},
"metadata": {
"type": "object",
"properties": {
"annotations": {
"description": "Additional annotations for the generated resource",
"type": "object",
"additionalProperties": {
"type": "string"
}
}
},
"required": ["annotations"]
}
},
"required": ["allow", "metadata"]
}
3 changes: 3 additions & 0 deletions test/languages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -678,6 +678,7 @@ export const CJSONLanguage: Language = {
/* Required properties absent are not checked (for the current implementation, can be added later, should abord parsing and return NULL) */
"intersection.schema",
"required.schema",
"description-unification.schema",
// The default-value fail sample also relies on required-property
// enforcement, which cJSON does not do.
"default-value.schema",
Expand Down Expand Up @@ -1918,6 +1919,7 @@ export const HaskellLanguage: Language = {
"keyword-unions.schema",
"optional-any.schema",
"required.schema",
"description-unification.schema",
// The default-value fail sample also relies on required-property enforcement.
"default-value.schema",
"required-non-properties.schema",
Expand Down Expand Up @@ -2277,6 +2279,7 @@ export const ElixirLanguage: Language = {
"default-value.schema",
"boolean-subschema.schema",
"intersection.schema",
"description-unification.schema",
"optional-any.schema",

// The test incorrectly succeeds due to the emitter being permissive for unions that contain only primitives. A future enhancement
Expand Down
Loading