Repository navigation
Only emit action constraint helper types when the ontology defines Action - #229
rootkiller6788 wants to merge 2 commits into
Conversation
…tion The InputActionConstraints / OutputActionConstraints / WithActionConstraints helpers reference ActionBase and PropertyValueSpecification, but were unconditionally appended to every generated file since google#202. For custom ontologies that do not define those classes the generated .ts failed to compile (TS2304: Cannot find name 'ActionBase'). Gate the emission on the ontology actually containing Action and PropertyValueSpecification, mirroring the existing hasRole check, and add a baseline test that keeps the helpers for an ontology with an Action class.
All custom test ontologies in baselines/ define no Action class, so the generated output no longer includes the three action constraint helper types. Refresh the inline snapshots to match.
mangeshraut712
left a comment
There was a problem hiding this comment.
Gating the helpers the same way as hasRole makes sense, and the new baseline with a real Action is a good positive control. One remaining case where the output still references a name that isn't emitted.
| // The action-constraint helpers reference ActionBase and | ||
| // PropertyValueSpecification, so only emit them when the ontology | ||
| // actually defines those classes. | ||
| hasAction: !!( |
There was a problem hiding this comment.
hasAction checks that the Action class exists, but the helpers reference the ActionBase interface (hard-coded as ActionBaseName = 'ActionBase' in ts/action_constraints.ts). These aren't the same condition. In ts/class.ts, skipBase() returns true when a class has exactly one named parent and no own properties, and then baseDecl() returns undefined, so no ActionBase is generated. A minimal custom ontology that declares Action rdfs:subClassOf Thing without attaching any property to it (e.g. it only uses Action subclasses, or puts the properties on those) would still produce TS2304: Cannot find name 'ActionBase' with this change. The new baseline doesn't catch that because it gives Action a query property.
Two possible fixes: gate on the Action class not skipping its base (e.g. expose something like hasBaseDeclaration() on Class), or have the helpers use the Action class's actual baseName(context) instead of the hard-coded string. A second baseline with a property-less Action would pin it either way. I've reasoned from class.ts rather than running the generator on that input, so it's worth confirming with a quick snapshot.
|
Thanks for the contribution, @rootkiller6788 , this looks like a good change. Can you iterate to make sure CI is happy? Then I'm happy to merge. |
Since #202 the generator always appends the InputActionConstraints / OutputActionConstraints / WithActionConstraints helper types to the generated file. Those helpers reference
ActionBaseandPropertyValueSpecification, which only exist in the output when the ontology actually defines those classes. Running the generator against a custom ontology that has no Action class therefore produced a .ts file that does not compile:This change gates the three helper types on the ontology defining both
ActionandPropertyValueSpecification, following the same pattern as the existinghasRolecheck. The real schema.org vocabulary still defines both classes, so the published output is unchanged; only custom/minimal ontologies get a clean, compilable result.Adds a baseline test that keeps the helpers for an ontology that does define an Action class, and updates the existing custom-ontology baselines that no longer include them.
Tested with the full schema-dts-gen suite: 41 suites, 142 tests, 43 snapshots all passing.