@voxgig/apidef 8.23.0 → 8.25.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. package/dist/builder/entity/entity.d.ts +2 -1
  2. package/dist/builder/entity/entity.js +14 -11
  3. package/dist/builder/entity/entity.js.map +1 -1
  4. package/dist/guide/guide.js +5 -2
  5. package/dist/guide/guide.js.map +1 -1
  6. package/dist/guide/heuristic01.d.ts +2 -1
  7. package/dist/guide/heuristic01.js +251 -86
  8. package/dist/guide/heuristic01.js.map +1 -1
  9. package/dist/model.d.ts +1 -0
  10. package/dist/transform/args.d.ts +4 -1
  11. package/dist/transform/args.js +42 -13
  12. package/dist/transform/args.js.map +1 -1
  13. package/dist/transform/body.d.ts +21 -2
  14. package/dist/transform/body.js +228 -15
  15. package/dist/transform/body.js.map +1 -1
  16. package/dist/transform/field.d.ts +3 -1
  17. package/dist/transform/field.js +154 -91
  18. package/dist/transform/field.js.map +1 -1
  19. package/dist/transform/operation.js +77 -24
  20. package/dist/transform/operation.js.map +1 -1
  21. package/dist/transform/select.js +10 -4
  22. package/dist/transform/select.js.map +1 -1
  23. package/dist/transform/top.d.ts +2 -1
  24. package/dist/transform/top.js +50 -3
  25. package/dist/transform/top.js.map +1 -1
  26. package/dist/tsconfig.tsbuildinfo +1 -1
  27. package/dist/utility.d.ts +10 -1
  28. package/dist/utility.js +145 -13
  29. package/dist/utility.js.map +1 -1
  30. package/model/apidef.aontu +1 -0
  31. package/package.json +1 -1
  32. package/src/builder/entity/entity.ts +17 -15
  33. package/src/guide/guide.ts +5 -3
  34. package/src/guide/heuristic01.ts +316 -94
  35. package/src/model.ts +2 -0
  36. package/src/transform/args.ts +50 -14
  37. package/src/transform/body.ts +280 -16
  38. package/src/transform/field.ts +184 -91
  39. package/src/transform/operation.ts +97 -26
  40. package/src/transform/select.ts +13 -4
  41. package/src/transform/top.ts +59 -3
  42. package/src/utility.ts +177 -13
@@ -5,11 +5,14 @@ import { each, getx } from 'jostraca'
5
5
  import type { TransformResult, Transform } from '../transform'
6
6
 
7
7
  import {
8
- validator, canonizeField, inferFieldType, normalizeFieldName, envelopeProp,
8
+ validator, fieldName, inferFieldType, envelopeProp,
9
9
  composedEnvelopeProp, mergedProperties, canonizeCmpName,
10
- scanUntaggedUnion, firstSentence, humanTitle,
10
+ scanUntaggedUnion, firstSentence, humanTitle, collapseScalarAllOf,
11
+ itemEnvelopeOf,
11
12
  } from '../utility'
12
13
 
14
+ import { arrayCarrier, guideMedia, requestFieldSchema, sameType } from './body'
15
+
13
16
  import { KIT } from '../types'
14
17
 
15
18
  import type {
@@ -40,7 +43,7 @@ const fieldTransform: Transform = async function(
40
43
 
41
44
  const opFieldPrecedence: OpName[] = ['load', 'create', 'update', 'patch', 'list']
42
45
 
43
- each(kit.entity, (ment: ModelEntity, _entname: string) => {
46
+ each(kit.entity, (ment: ModelEntity, entname: string) => {
44
47
  const fields = ment.fields
45
48
 
46
49
  for (let opname of opFieldPrecedence) {
@@ -49,20 +52,25 @@ const fieldTransform: Transform = async function(
49
52
  const mpoints = mop.points
50
53
 
51
54
  for (let mpoint of mpoints) {
52
- const opfields = resolveOpFields(ment, mop, mpoint, def)
55
+ const media = guideMedia(guide, entname, opname, mpoint).body
56
+ const opfields = resolveOpFields(ment, mop, mpoint, def, media)
57
+ const carrier = carrierOf(mpoint, def, media)?.name
53
58
 
54
59
  for (let opfield of opfields) {
55
- if (!Object.prototype.hasOwnProperty.call(fields, opfield.n)) {
56
- fields[opfield.n] = opfield
57
- }
58
- else {
59
- mergeField(mop, fields[opfield.n], opfield)
60
- }
60
+ addField(fields, mop, opfield, carrier === opfield.n)
61
61
  }
62
62
  }
63
63
  }
64
64
  }
65
65
 
66
+ // A remove's response is no record, so its carrier is all it adds.
67
+ for (const mpoint of ment.op.remove?.points ?? []) {
68
+ const carrier = carrierOf(mpoint, def, guideMedia(guide, entname, 'remove', mpoint).body)
69
+ if (null != carrier) {
70
+ addField(fields, ment.op.remove as ModelOp, modelField(carrierDef(carrier)), true)
71
+ }
72
+ }
73
+
66
74
 
67
75
  const gent = guide?.entity?.[ment.name]
68
76
  const composite = compositeId(ment, gent, def)
@@ -563,58 +571,76 @@ function resolveOpFields(
563
571
  ment: ModelEntity,
564
572
  mop: ModelOp,
565
573
  mpoint: ModelPoint,
566
- def: any
574
+ def: any,
575
+ media?: string,
567
576
  ): ModelField[] {
568
- const mfields: ModelField[] = []
569
- const fielddefs = findFieldDefs(ment, mop, mpoint, def)
570
-
571
- for (let fielddef of fielddefs) {
572
- const fieldname = (fielddef as any).key$ as string
573
- // Field names are WIRE identifiers — see canonizeField. Using the
574
- // entity-name canonizer here renamed modelType -> model_type and
575
- // items -> item, so the SDK read keys the server never sends.
576
- const name = canonizeField(normalizeFieldName(fieldname))
577
- const mfield: ModelField = {
578
- n: name,
579
- h: humanTitle(name),
580
- t: inferFieldType(name, validator(fielddef.type)),
581
- r: !!fielddef.required,
582
- op: {},
583
- }
584
- const fdesc = (fielddef as any).description
585
- if ('string' === typeof fdesc && '' !== fdesc.trim()) {
586
- const short = firstSentence(fdesc)
587
- if ('' !== short) {
588
- mfield.sh = short
589
- }
590
- }
577
+ return findFieldDefs(ment, mop, mpoint, def, media).map(modelField)
578
+ }
591
579
 
592
- for (const [flag, attr] of [['readOnly', 'ro'], ['writeOnly', 'wo'], ['deprecated', 'de']] as const) {
593
- if (true === (fielddef as any)[flag]) {
594
- mfield[attr] = true
595
- }
596
- }
597
580
 
598
- // `format` is an open vocabulary — OpenAPI defines a handful and lets a
599
- // spec coin its own — so it is carried as the string it is rather than
600
- // interpreted here. `password` is the one a generator acts on today.
601
- const ffmt = (fielddef as any).format
602
- if ('string' === typeof ffmt && '' !== ffmt.trim()) {
603
- mfield.fo = ffmt.trim()
581
+ // The names of a route's fields, read with no action and no carrier of its own.
582
+ function routeFieldNames(
583
+ ment: ModelEntity,
584
+ opname: string,
585
+ mpoint: ModelPoint,
586
+ def: any,
587
+ media?: string,
588
+ ): string[] {
589
+ const route = { k: mpoint.k, m: mpoint.m, o: mpoint.o, q: {} } as ModelPoint
590
+ return resolveOpFields(ment, { name: opname } as ModelOp, route, def, media).map((mfield) => mfield.n)
591
+ }
592
+
593
+
594
+ function modelField(fielddef: SchemaDef): ModelField {
595
+ const name = fieldName((fielddef as any).key$ as string)
596
+ const mfield: ModelField = {
597
+ n: name,
598
+ h: humanTitle(name),
599
+ t: inferFieldType(name, validator(fielddef.type)),
600
+ r: !!fielddef.required,
601
+ op: {},
602
+ }
603
+ const fdesc = (fielddef as any).description
604
+ if ('string' === typeof fdesc && '' !== fdesc.trim()) {
605
+ const short = firstSentence(fdesc)
606
+ if ('' !== short) {
607
+ mfield.sh = short
604
608
  }
609
+ }
605
610
 
606
- // Record an untagged union under this field. The field is already typed
607
- // openly ($ANY/$ARRAY/$OBJECT) because there is nothing to narrow it to;
608
- // this says WHY, so the generated docs can explain the open type instead
609
- // of leaving it looking like a modelling failure.
610
- const union = scanUntaggedUnion(fielddef)
611
- if (null != union) {
612
- mfield.union = union
611
+ for (const [flag, attr] of [['readOnly', 'ro'], ['writeOnly', 'wo'], ['deprecated', 'de']] as const) {
612
+ if (true === (fielddef as any)[flag]) {
613
+ mfield[attr] = true
613
614
  }
614
- mfields.push(mfield)
615
615
  }
616
616
 
617
- return mfields
617
+ // `format` is an open vocabulary — OpenAPI defines a handful and lets a
618
+ // spec coin its own — so it is carried as the string it is rather than
619
+ // interpreted here. `password` is the one a generator acts on today.
620
+ const ffmt = (fielddef as any).format
621
+ if ('string' === typeof ffmt && '' !== ffmt.trim()) {
622
+ mfield.fo = ffmt.trim()
623
+ }
624
+
625
+ // Record an untagged union under this field. The field is already typed
626
+ // openly ($ANY/$ARRAY/$OBJECT) because there is nothing to narrow it to;
627
+ // this says WHY, so the generated docs can explain the open type instead
628
+ // of leaving it looking like a modelling failure.
629
+ const union = scanUntaggedUnion(fielddef)
630
+ if (null != union) {
631
+ mfield.union = union
632
+ }
633
+ return mfield
634
+ }
635
+
636
+
637
+ function addField(fields: Record<string, ModelField>, mop: ModelOp, field: ModelField, carrier: boolean) {
638
+ if (!Object.prototype.hasOwnProperty.call(fields, field.n)) {
639
+ fields[field.n] = field
640
+ }
641
+ else {
642
+ mergeField(mop, fields[field.n], field, carrier)
643
+ }
618
644
  }
619
645
 
620
646
 
@@ -693,7 +719,8 @@ function findFieldDefs(
693
719
  ment: ModelEntity,
694
720
  mop: ModelOp,
695
721
  mpoint: ModelPoint,
696
- def: any
722
+ def: any,
723
+ media?: string,
697
724
  ): SchemaDef[] {
698
725
  if ('graphql' === mpoint.k) {
699
726
  return findGraphqlFieldDefs(ment, mpoint, def)
@@ -728,8 +755,10 @@ function findFieldDefs(
728
755
  if ('list' == mop.name) {
729
756
  const unwrapped = unwrapArrayWrapper(fieldSets)
730
757
  envelope = null == unwrapped ? composedEnvelopeProp(fieldSets, 'list') : null
758
+ const itemkey = itemEnvelopeOf(mpoint.t?.res)
731
759
  if (unwrapped) {
732
- fieldSets = unwrapped
760
+ fieldSets = null == itemkey ? unwrapped :
761
+ mergedProperties(unwrapped)?.[itemkey] ?? unwrapped
733
762
  }
734
763
  else if (null != envelope) {
735
764
  fieldSets = mergedProperties(fieldSets)?.[envelope]?.items
@@ -764,45 +793,21 @@ function findFieldDefs(
764
793
  return fielddefs
765
794
  }
766
795
 
767
- // A QUERY (RFC 10008) request body is a filter/query schema, not the
768
- // entity shape, so it must not contribute entity fields. Fields for a
769
- // QUERY op come from its response only. Other methods (POST/PUT/PATCH)
770
- // carry the entity in the body, so merge as usual -- except for an
771
- // action, whose body is the verb's arguments and never the record.
772
- if (requestBody && 'query' !== method && !isAction) {
773
- fieldSets = [
774
- fieldSets,
775
- getx(requestBody, 'content "application/json" schema') ??
776
- getx(requestBody, 'schema')
777
- ]
796
+ // A QUERY (RFC 10008) body is a filter, not the entity shape, so a QUERY
797
+ // op's fields come from its response alone; an action's body is the
798
+ // verb's arguments, never the record.
799
+ const reqschema = requestFieldSchema(def, mpoint, media)
800
+ if ((requestBody || null != reqschema) && 'query' !== method && !isAction) {
801
+ fieldSets = [fieldSets, reqschema]
778
802
  }
779
803
 
780
804
 
781
- if (fieldSets) {
782
- if (Array.isArray(fieldSets.allOf)) {
783
- fieldSets = fieldSets.allOf
784
- }
785
- else if (fieldSets.properties) {
786
- fieldSets = [fieldSets]
787
- }
805
+ if (fieldSets && (Array.isArray(fieldSets.allOf) || fieldSets.properties)) {
806
+ fieldSets = [fieldSets]
788
807
  }
789
808
 
790
809
  each(fieldSets, (fieldSet: any) => {
791
- const requiredNames: string[] = Array.isArray(fieldSet?.required)
792
- ? fieldSet.required : []
793
- each(fieldSet?.properties, (property: any) => {
794
- // Don't mutate the parsed schema: a $ref-resolved schema is shared
795
- // across every operation that references it, so flipping
796
- // `property.required = true` here would leak this operation's
797
- // required[] onto all the others. Derive `required` onto a shallow
798
- // copy instead (matches the Go port, which builds fresh field defs).
799
- if (!property.required && requiredNames.includes(property.key$)) {
800
- fielddefs.push({ ...property, required: true })
801
- }
802
- else {
803
- fielddefs.push(property)
804
- }
805
- })
810
+ fielddefs.push(...composedFields(fieldSet))
806
811
  })
807
812
  }
808
813
 
@@ -814,10 +819,94 @@ function findFieldDefs(
814
819
  }
815
820
  }
816
821
 
822
+ // An array body is sent from one field of the request data, and has no
823
+ // properties of its own to contribute.
824
+ const carrier = carrierOf(mpoint, def, media)
825
+ if (null != carrier) {
826
+ fielddefs.push(carrierDef(carrier))
827
+ }
828
+
817
829
  return fielddefs
818
830
  }
819
831
 
820
832
 
833
+ // An action's request fields are its own, so it declares no carrier.
834
+ function carrierOf(mpoint: ModelPoint, def: any, media?: string) {
835
+ if ('graphql' === mpoint.k || null != (mpoint as any).q?.['$action']) {
836
+ return undefined
837
+ }
838
+ return arrayCarrier(def, mpoint, media)
839
+ }
840
+
841
+
842
+ // Optional, as the record never holds it.
843
+ function carrierDef(carrier: { name: string, type: string | string[], description?: string }): SchemaDef {
844
+ return { key$: carrier.name, type: carrier.type, description: carrier.description } as SchemaDef
845
+ }
846
+
847
+
848
+ // A schema and its allOf members describe one object: a name any of them
849
+ // requires is required, and a property declared more than once is one field
850
+ // taking each fact from the first declaration that states it.
851
+ function composedFields(schema: any): any[] {
852
+ const parts: any[] = []
853
+ const seen = new Set<any>()
854
+ const visit = (node: any) => {
855
+ if (null == node || 'object' !== typeof node || seen.has(node)) {
856
+ return
857
+ }
858
+ seen.add(node)
859
+ parts.push(node)
860
+ if (Array.isArray(node.allOf)) {
861
+ node.allOf.forEach(visit)
862
+ }
863
+ }
864
+ visit(schema)
865
+
866
+ const required = new Set(parts.flatMap((part) => Array.isArray(part.required) ? part.required : []))
867
+ const decls: Record<string, any[]> = Object.create(null)
868
+ for (const part of parts) {
869
+ each(part.properties, (property: any) => {
870
+ (decls[property.key$] = decls[property.key$] ?? []).push(collapseScalarAllOf(property))
871
+ })
872
+ }
873
+
874
+ return Object.keys(decls).map((name) => {
875
+ const property = 1 === decls[name].length ? decls[name][0] : mergeDeclarations(decls[name])
876
+ // A copy: parsed schemas are shared by every operation referencing them.
877
+ return !property.required && required.has(name) ? { ...property, required: true } : property
878
+ })
879
+ }
880
+
881
+
882
+ function mergeDeclarations(decls: any[]): any {
883
+ const merged: any = {}
884
+ for (const decl of decls) {
885
+ for (const [key, value] of Object.entries(decl)) {
886
+ if (!statesFact(key, merged[key]) && statesFact(key, value)) {
887
+ merged[key] = value
888
+ }
889
+ }
890
+ }
891
+ return merged
892
+ }
893
+
894
+
895
+ const ANNOTATION_FLAGS = ['readOnly', 'writeOnly', 'deprecated']
896
+
897
+ // A blank string states nothing, and nor does a false annotation flag, so a
898
+ // later declaration may.
899
+ function statesFact(key: string, value: any): boolean {
900
+ if (null == value) {
901
+ return false
902
+ }
903
+ if ('string' === typeof value) {
904
+ return '' !== value.trim()
905
+ }
906
+ return !(false === value && ANNOTATION_FLAGS.includes(key))
907
+ }
908
+
909
+
821
910
  function answersOnlyAccepted(responses: any): boolean {
822
911
  return null == responses['200'] && null == responses['201'] && null != responses['202']
823
912
  }
@@ -935,12 +1024,15 @@ function inferTypeFromValue(value: any): string {
935
1024
  }
936
1025
 
937
1026
 
1027
+ // A carrier keeps its array type for its operation where it meets a field of
1028
+ // another type.
938
1029
  function mergeField(
939
1030
  mop: ModelOp,
940
1031
  existingField: ModelField,
941
- newField: ModelField
1032
+ newField: ModelField,
1033
+ carrier: boolean = false,
942
1034
  ) {
943
- if (newField.r !== existingField.r) {
1035
+ if (newField.r !== existingField.r || (carrier && !sameType(newField.t, existingField.t))) {
944
1036
  existingField.op[mop.name] = {
945
1037
  req: newField.r,
946
1038
  type: newField.t,
@@ -965,4 +1057,5 @@ export {
965
1057
  fieldTransform,
966
1058
  inferFieldsFromExamples,
967
1059
  inferTypeFromValue,
1060
+ routeFieldNames,
968
1061
  }
@@ -1,4 +1,14 @@
1
- import { guideActive } from '../utility'
1
+ import { bodyRequestTransform, guideActive } from '../utility'
2
+
3
+ import { arrayBodyField } from '../guide/heuristic01'
4
+
5
+ import {
6
+ arrayCarrier, arrayRequestSchema, guideMedia, nullableType, requestDecl, requestSchema, sameType,
7
+ } from './body'
8
+
9
+ import { routeFieldNames } from './field'
10
+
11
+ import { routeArgNames } from './args'
2
12
 
3
13
 
4
14
  import { each } from 'jostraca'
@@ -21,6 +31,7 @@ import type {
21
31
  ModelOpMap,
22
32
  ModelOp,
23
33
  ModelPoint,
34
+ ModelEntity,
24
35
  } from '../model'
25
36
 
26
37
 
@@ -40,7 +51,7 @@ const IGNORED_OPS = ['head', 'options', 'OPTIONS']
40
51
  const operationTransform: Transform = async function(
41
52
  ctx: any,
42
53
  ): Promise<TransformResult> {
43
- const { apimodel, guide } = ctx
54
+ const { apimodel, def, guide } = ctx
44
55
  const kit = apimodel.main[KIT]
45
56
 
46
57
  let msg = 'operation '
@@ -59,12 +70,24 @@ const operationTransform: Transform = async function(
59
70
  patch: undefined,
60
71
  }
61
72
 
62
- resolveLoad(opm, gent)
63
- resolveList(opm, gent)
64
- resolveCreate(opm, gent)
65
- resolveUpdate(opm, gent)
66
- resolveRemove(opm, gent)
67
- resolvePatch(opm, gent)
73
+ const on = { gent, def, entname, guide }
74
+ resolveLoad(opm, on)
75
+ resolveList(opm, on)
76
+ resolveCreate(opm, on)
77
+ resolveUpdate(opm, on)
78
+ resolveRemove(opm, on)
79
+ resolvePatch(opm, on)
80
+
81
+ // After patch has joined update, so each operation's routes are final.
82
+ for (const mop of Object.values(opm)) {
83
+ for (const mpoint of mop?.points ?? []) {
84
+ if (heuristicRequest(on, mop!, mpoint)) {
85
+ mpoint.t.req = undefined
86
+ }
87
+ mpoint.t.req = mpoint.t.req ?? requestDefault(on, kit.entity[entname], opm, mop!, mpoint)
88
+ mpoint.t.res = mpoint.t.res ?? '`body`'
89
+ }
90
+ }
68
91
 
69
92
  kit.entity[entname].op = opm
70
93
 
@@ -118,40 +141,40 @@ function collectOps(ctx: any, gent: GuideEntity) {
118
141
 
119
142
 
120
143
 
121
- function resolveLoad(opm: ModelOpMap, gent: GuideEntity): undefined | ModelOp {
122
- const opdesc = opm.load = resolveOp('load', gent)
144
+ function resolveLoad(opm: ModelOpMap, on: OpEntity): undefined | ModelOp {
145
+ const opdesc = opm.load = resolveOp('load', on)
123
146
  return opdesc
124
147
  }
125
148
 
126
149
 
127
- function resolveList(opm: ModelOpMap, gent: GuideEntity): undefined | ModelOp {
128
- const opdesc = opm.list = resolveOp('list', gent)
150
+ function resolveList(opm: ModelOpMap, on: OpEntity): undefined | ModelOp {
151
+ const opdesc = opm.list = resolveOp('list', on)
129
152
  return opdesc
130
153
  }
131
154
 
132
155
 
133
- function resolveCreate(opm: ModelOpMap, gent: GuideEntity): undefined | ModelOp {
134
- const opdesc = opm.create = resolveOp('create', gent)
156
+ function resolveCreate(opm: ModelOpMap, on: OpEntity): undefined | ModelOp {
157
+ const opdesc = opm.create = resolveOp('create', on)
135
158
  return opdesc
136
159
  }
137
160
 
138
161
 
139
- function resolveUpdate(opm: ModelOpMap, gent: GuideEntity): undefined | ModelOp {
140
- const opdesc = opm.update = resolveOp('update', gent)
162
+ function resolveUpdate(opm: ModelOpMap, on: OpEntity): undefined | ModelOp {
163
+ const opdesc = opm.update = resolveOp('update', on)
141
164
  return opdesc
142
165
  }
143
166
 
144
167
 
145
- function resolveRemove(opm: ModelOpMap, gent: GuideEntity): undefined | ModelOp {
146
- const opdesc = opm.remove = resolveOp('remove', gent)
168
+ function resolveRemove(opm: ModelOpMap, on: OpEntity): undefined | ModelOp {
169
+ const opdesc = opm.remove = resolveOp('remove', on)
147
170
  return opdesc
148
171
  }
149
172
 
150
173
 
151
- function resolvePatch(opm: ModelOpMap, gent: GuideEntity): undefined | ModelOp {
152
- const opdesc = resolveOp('patch', gent)
174
+ function resolvePatch(opm: ModelOpMap, on: OpEntity): undefined | ModelOp {
175
+ const opdesc = resolveOp('patch', on)
153
176
 
154
- if (null != opdesc && (null == opm.update || onlyActionPaths(gent, 'update'))) {
177
+ if (null != opdesc && (null == opm.update || onlyActionPaths(on.gent, 'update'))) {
155
178
  if (null != opm.update) {
156
179
  opdesc.points.push(...opm.update.points)
157
180
  }
@@ -174,9 +197,12 @@ function onlyActionPaths(gent: GuideEntity, opname: OpName): boolean {
174
197
  }
175
198
 
176
199
 
177
- function resolveOp(opname: OpName, gent: GuideEntity): undefined | ModelOp {
200
+ type OpEntity = { gent: GuideEntity, def: any, entname: string, guide: any }
201
+
202
+
203
+ function resolveOp(opname: OpName, on: OpEntity): undefined | ModelOp {
178
204
  let mop: undefined | ModelOp = undefined
179
- let opdesc = (gent as any).opm$[opname]
205
+ let opdesc = (on.gent as any).opm$[opname]
180
206
  if (opdesc) {
181
207
  mop = {
182
208
  name: opname,
@@ -195,9 +221,6 @@ function resolveOp(opname: OpName, gent: GuideEntity): undefined | ModelOp {
195
221
  }
196
222
  }
197
223
 
198
- mpoint.t.req = mpoint.t.req ?? '`reqdata`'
199
- mpoint.t.res = mpoint.t.res ?? '`body`'
200
-
201
224
  return mpoint
202
225
  })
203
226
  }
@@ -208,6 +231,54 @@ function resolveOp(opname: OpName, gent: GuideEntity): undefined | ModelOp {
208
231
 
209
232
 
210
233
 
234
+ // The request transform the heuristic took from the default body, still in
235
+ // place where the guide selects another media type for the point.
236
+ function heuristicRequest(on: OpEntity, mop: ModelOp, mpoint: ModelPoint): boolean {
237
+ const chosen = guideMedia(on.guide, on.entname, mop.name, mpoint).body
238
+ if (null == chosen || null == mpoint.t.req) return false
239
+ const generated = bodyRequestTransform(requestSchema(on.def, mpoint.m, mpoint.o), [on.gent.name])
240
+ return null != generated && sameTransform(mpoint.t.req, generated)
241
+ }
242
+
243
+
244
+ function sameTransform(a: any, b: any): boolean {
245
+ const canon = (t: any) => 'string' === typeof t ? t :
246
+ JSON.stringify(Object.keys(t ?? {}).sort().map((k: string) => [k, t[k]]))
247
+ return canon(a) === canon(b)
248
+ }
249
+
250
+
251
+ // An array body is sent from one field of the request data, named for its
252
+ // records, and never for an argument of its operation, a field another route of
253
+ // its entity has, as fields span operations, or a carrier already named for an
254
+ // array of another type or requiredness. Decided here, not by the guide
255
+ // heuristic, as the guide's media type decides the body.
256
+ function requestDefault(
257
+ on: OpEntity, ment: ModelEntity, opm: ModelOpMap, mop: ModelOp, mpoint: ModelPoint,
258
+ ): any {
259
+ const media = (opname: string, q: ModelPoint) => guideMedia(on.guide, on.entname, opname, q).body
260
+ const chosen = media(mop.name, mpoint)
261
+ const list = arrayRequestSchema(on.def, mpoint.m, mpoint.o, chosen)
262
+ if (null == list) {
263
+ return (null == chosen ? undefined :
264
+ bodyRequestTransform(requestSchema(on.def, mpoint.m, mpoint.o, chosen), [on.gent.name])) ?? '`reqdata`'
265
+ }
266
+ const required = true === requestDecl(on.def, mpoint.m, mpoint.o)?.required
267
+ const others = mop.points.filter((q) => q !== mpoint)
268
+ const routes = Object.values(opm).flatMap((op) =>
269
+ (op?.points ?? []).filter((q) => q !== mpoint).map((q) => ({ opname: op!.name, q })))
270
+ const taken = [
271
+ ...mop.points.flatMap((q) => routeArgNames(on.def, q)),
272
+ ...routes.flatMap(({ opname, q }) => routeFieldNames(ment, opname, q, on.def, media(opname, q))),
273
+ ...others.map((q) => arrayCarrier(on.def, q, media(mop.name, q)))
274
+ .filter((carrier) => null != carrier &&
275
+ !(sameType(carrier.type, nullableType(list)) && carrier.required === required))
276
+ .map((carrier) => carrier!.name),
277
+ ]
278
+ return '`reqdata.' + arrayBodyField(list, on.entname, taken) + '`'
279
+ }
280
+
281
+
211
282
  export {
212
283
  operationTransform,
213
284
  }
@@ -7,6 +7,8 @@ import type { TransformResult, Transform } from '../transform'
7
7
 
8
8
  import { KIT } from '../types'
9
9
 
10
+ import { arrayCarrier, guideMedia } from './body'
11
+
10
12
  import type {
11
13
  KitModel,
12
14
  Guide,
@@ -34,13 +36,14 @@ const selectTransform: Transform = async function(
34
36
 
35
37
  let msg = 'select '
36
38
 
37
- each(kit.entity, (ment: ModelEntity, _entname: string) => {
38
- each(ment.op, (mop: ModelOp, _opname: OpName) => {
39
+ each(kit.entity, (ment: ModelEntity, entname: string) => {
40
+ each(ment.op, (mop: ModelOp, opname: OpName) => {
39
41
  each(mop.points, (mpoint: ModelPoint) => {
40
42
  // GraphQL defs have no `paths`; the lookup is only passed through to
41
43
  // an unused parameter, so skip it rather than dereference undefined.
42
44
  const pdef: PathDef = def.paths?.[mpoint.o]
43
- resolveSelect(guide, ment, mop, mpoint, pdef)
45
+ const carrier = arrayCarrier(def, mpoint, guideMedia(guide, entname, opname, mpoint).body)
46
+ resolveSelect(guide, ment, mop, mpoint, pdef, carrier)
44
47
  })
45
48
  if (null != mop.points && 0 < mop.points.length) {
46
49
  sortPoints(guide, ment, mop)
@@ -60,7 +63,8 @@ function resolveSelect(
60
63
  ment: ModelEntity,
61
64
  _mop: ModelOp,
62
65
  mpoint: ModelPoint,
63
- _pdef: PathDef
66
+ _pdef: PathDef,
67
+ carrier?: { name: string, required: boolean },
64
68
  ) {
65
69
  const select: any = mpoint.q
66
70
  const margs: any = mpoint.g
@@ -85,6 +89,11 @@ function resolveSelect(
85
89
  })
86
90
  })
87
91
 
92
+ // The field an array body is sent from, when the body is required.
93
+ if (carrier?.required && !select.exist.includes(carrier.name)) {
94
+ select.exist.push(carrier.name)
95
+ }
96
+
88
97
  select.exist.sort()
89
98
 
90
99
  const gent = guide.entity[ment.name]