Skip to content
This repository was archived by the owner on Feb 12, 2022. It is now read-only.

Commit a2dbc44

Browse files
fixing error finding elements in the model, and report of line location correctly for scalar YAML nodes. fixes #46 #43
1 parent 4f84db6 commit a2dbc44

File tree

9 files changed

+141
-99
lines changed

9 files changed

+141
-99
lines changed

js/js-support-bundle.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1472,10 +1472,10 @@ function readPlainScalar(state, nodeIndent, withinFlowCollection) {
14721472
var endIndex = state.position;
14731473

14741474
var positions = {
1475-
"start-line": startLine,
1475+
"start-line": startLine + 1,
14761476
"start-column": startColumn,
14771477
"start-index": startIndex,
1478-
"end-line": endLine,
1478+
"end-line": endLine + 1,
14791479
"end-column": endColumn,
14801480
"end-index": endIndex
14811481
};
@@ -1534,10 +1534,10 @@ function readSingleQuotedScalar(state, nodeIndent) {
15341534
var endIndex = state.position;
15351535

15361536
var positions = {
1537-
"start-line": startLine,
1537+
"start-line": startLine + 1,
15381538
"start-column": startColumn,
15391539
"start-index": startIndex,
1540-
"end-line": endLine,
1540+
"end-line": endLine + 1,
15411541
"end-column": endColumn,
15421542
"end-index": endIndex
15431543
};
@@ -1599,10 +1599,10 @@ function readDoubleQuotedScalar(state, nodeIndent) {
15991599
var endIndex = state.position;
16001600

16011601
var positions = {
1602-
"start-line": startLine,
1602+
"start-line": startLine + 1,
16031603
"start-column": startColumn,
16041604
"start-index": startIndex,
1605-
"end-line": endLine,
1605+
"end-line": endLine + 1,
16061606
"end-column": endColumn,
16071607
"end-index": endIndex
16081608
};

js/js-yaml/lib/js-yaml/loader.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -512,10 +512,10 @@ function readPlainScalar(state, nodeIndent, withinFlowCollection) {
512512
var endIndex = state.position;
513513

514514
var positions = {
515-
"start-line": startLine,
515+
"start-line": startLine + 1,
516516
"start-column": startColumn,
517517
"start-index": startIndex,
518-
"end-line": endLine,
518+
"end-line": endLine + 1,
519519
"end-column": endColumn,
520520
"end-index": endIndex
521521
};
@@ -574,10 +574,10 @@ function readSingleQuotedScalar(state, nodeIndent) {
574574
var endIndex = state.position;
575575

576576
var positions = {
577-
"start-line": startLine,
577+
"start-line": startLine + 1,
578578
"start-column": startColumn,
579579
"start-index": startIndex,
580-
"end-line": endLine,
580+
"end-line": endLine + 1,
581581
"end-column": endColumn,
582582
"end-index": endIndex
583583
};
@@ -639,10 +639,10 @@ function readDoubleQuotedScalar(state, nodeIndent) {
639639
var endIndex = state.position;
640640

641641
var positions = {
642-
"start-line": startLine,
642+
"start-line": startLine + 1,
643643
"start-column": startColumn,
644644
"start-index": startIndex,
645-
"end-line": endLine,
645+
"end-line": endLine + 1,
646646
"end-column": endColumn,
647647
"end-index": endIndex
648648
};

src/api_modeling_framework/core.cljc

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -236,17 +236,27 @@
236236

237237
(defn find-element* [model id]
238238
(cond
239-
(map? model) (if (= id (or (:id model) (get model "@id")))
240-
model
241-
(->> (vals model)
242-
(map (fn [m] (find-element* m id)))
243-
(filter some?)
244-
first))
245-
(coll? model) (->> model
246-
(map (fn [m] (find-element* m id)))
247-
(filter some?)
248-
first)
249-
:else nil))
239+
;; we prefer elements in declarations if present
240+
(and (map? model)
241+
(:declares model)
242+
(some #(= id (:id %))
243+
(:declares model))) (->> (:declares model)
244+
(filter #(= id (:id %)))
245+
first)
246+
247+
(map? model) (if (= id (or (:id model) (get model "@id")))
248+
model
249+
(->> (vals model)
250+
(map (fn [m] (find-element* m id)))
251+
(filter some?)
252+
first))
253+
254+
(coll? model) (->> model
255+
(map (fn [m] (find-element* m id)))
256+
(filter some?)
257+
first)
258+
259+
:else nil))
250260

251261
(defn to-model
252262
([res]

src/api_modeling_framework/generators/domain/shapes_raml_types.cljc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,8 @@
174174
(if (utils/object-no-properties? base)
175175
(first types)
176176
(assoc base :type (first types)))
177-
{:type types})))
177+
{:type "union"
178+
:anyOf types})))
178179

179180
(defmethod parse-shape :raml-expression [shape _] (-> shape (get (v/shapes-ns "ramlTypeExpression")) first (get "@value")))
180181

src/api_modeling_framework/parser/document/raml.cljc

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -86,35 +86,38 @@
8686
(mapv (fn [annotation]
8787
[(document/name annotation) annotation]))
8888
(into {}))
89+
working-declarations library-declarations
90+
;; we parse traits and types and add the information into the context
91+
types (domain-parser/process-types (syntax/<-data node) {:location (str location "#")
92+
:fragments fragments
93+
:references working-declarations
94+
:document-parser parse-ast
95+
:parsed-location location})
96+
working-declarations (merge working-declarations types)
8997
doc-annotations (domain-parser/process-annotations (syntax/<-data node) {:base-uri location
98+
:references working-declarations
9099
:location (str location "#")
91100
:parsed-location (str location "#")})
92101
annotations (merge library-declarations doc-annotations)
93-
;; we parse traits and types and add the information into the context
94102
traits (domain-parser/process-traits (syntax/<-data node) {:location (str location "#")
95103
:fragments fragments
96-
:references library-declarations
104+
:references working-declarations
97105
:document-parser parse-ast
98106
:annotations annotations
99107
:parsed-location (str location "#")})
100-
types (domain-parser/process-types (syntax/<-data node) {:location (str location "#")
101-
:fragments fragments
102-
:references library-declarations
103-
:annotations annotations
104-
:document-parser parse-ast
105-
:parsed-location location})
106-
declarations (merge traits types)
108+
working-declarations (merge working-declarations traits)
107109
encoded (domain-parser/parse-ast (syntax/<-data node) {:location (str location "#")
108110
:fragments fragments
109111
:parsed-location (str location "#")
110112
:annotations annotations
111-
:references (merge declarations library-declarations)
113+
:references working-declarations
112114
:document-parser parse-ast
113115
:is-fragment false})]
114116
(-> (document/map->ParsedDocument {:id location
115117
:location location
116118
:encodes encoded
117-
:declares (concat (vals declarations)
119+
:declares (concat (vals types)
120+
(vals traits)
118121
(vals doc-annotations))
119122
:references (compute-fragments
120123
(concat (vals @fragments)
@@ -137,33 +140,36 @@
137140
(mapv (fn [annotation]
138141
[(document/name annotation) annotation]))
139142
(into {}))
143+
working-declarations library-declarations
144+
;; we parse traits and types and add the information into the context
145+
types (domain-parser/process-types (syntax/<-data node) {:location (str location "#")
146+
:fragments fragments
147+
:alias-chain alias-chain
148+
:references working-declarations
149+
:document-parser parse-ast
150+
:parsed-location location})
151+
working-declarations (merge working-declarations types)
140152
doc-annotations (domain-parser/process-annotations (syntax/<-data node) {:base-uri location
141153
:location (str location "#")
142154
:parsed-location (str location "#/annotations")
143-
:references library-declarations})
155+
:references working-declarations})
144156
annotations (merge libraries-annotation doc-annotations)
145-
;; we parse traits and types and add the information into the context
146157
traits (domain-parser/process-traits (syntax/<-data node) {:location (str location "#")
147158
:fragments fragments
148-
:references library-declarations
159+
:references working-declarations
149160
:alias-chain alias-chain
150161
:document-parser parse-ast
151162
:annotations annotations
152163
:parsed-location (str location "#")})
153-
types (domain-parser/process-types (syntax/<-data node) {:location (str location "#")
154-
:fragments fragments
155-
:alias-chain alias-chain
156-
:references library-declarations
157-
:annotations annotations
158-
:document-parser parse-ast
159-
:parsed-location location})
160-
declarations (merge traits types)
164+
161165
usage (:usage (syntax/<-data node))]
162166
(-> (document/map->ParsedModule (utils/clean-nils
163167
{:id location
164168
:location location
165169
:description usage
166-
:declares (concat (vals declarations) (vals doc-annotations))
170+
:declares (concat (vals types)
171+
(vals traits)
172+
(vals doc-annotations))
167173
:references (compute-fragments
168174
(concat (vals @fragments)
169175
(flatten (vals libraries))))

src/api_modeling_framework/parser/domain/raml.cljc

Lines changed: 45 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -345,44 +345,53 @@
345345
(assoc acc (keyword (utils/alias-chain trait-name context)) parsed-trait)))
346346
{}))))
347347

348-
(defn process-types [node {:keys [location parsed-location alias-chain] :as context}]
348+
(defn process-types [node {:keys [location parsed-location alias-chain references] :as context}]
349349
(let [types (or (:types node) (:schemas node) {})
350350
path-label (if (some? (:types node)) "types" "schemas")
351351
location (utils/path-join parsed-location "/" path-label)
352352
nested-context (-> context (assoc :location location) (assoc :parsed-location parsed-location))]
353353
(debug "Processing " (count types) " types")
354-
(->> types
355-
(reduce (fn [acc [type-name type-node]]
356-
(debug (str "Processing type " type-name))
357-
(let [location-meta (meta type-node)
358-
type-node (common/purge-ast type-node)
359-
type-node (if (syntax/fragment? type-node)
360-
;; avoiding situations where we transform this into an include
361-
;; and then we cannot transform this back into type because there's
362-
;; no way to tell it without source maps
363-
{:type type-node}
364-
type-node)
365-
type-name (url/url-encode (utils/safe-str type-name))
366-
type-id (common/type-reference parsed-location type-name)
367-
references (get nested-context :references {})
368-
type-fragment (parse-ast type-node (-> nested-context
369-
(assoc :references (merge references acc))
370-
(assoc :location location)
371-
(assoc :parsed-location type-id)
372-
(assoc :is-fragment false)
373-
(assoc :type-hint :type)))
374-
sources (or (-> type-fragment :sources) [])
375-
;; we annotate the parsed type with the is-type source map so we can distinguish it from other declarations
376-
sources (concat sources (common/generate-is-type-sources type-name
377-
(utils/path-join location type-name)
378-
type-id))
379-
parsed-type (assoc type-fragment :sources sources)
380-
parsed-type (if (nil? (:name parsed-type))
381-
(assoc parsed-type :name type-name)
382-
parsed-type)
383-
parsed-type (assoc parsed-type :lexical location-meta)]
384-
(assoc acc (keyword (utils/alias-chain type-name context)) parsed-type)))
385-
{}))))
354+
(let [;; we will mark the positions of references in the types node
355+
ahead-references (->> types
356+
(map (fn [[type-name _]]
357+
(let [type-name (url/url-encode (utils/safe-str type-name))
358+
type-id (common/type-reference parsed-location type-name)]
359+
[(keyword type-name) {:x-ahead-declaration type-id}])))
360+
(into {}))
361+
working-references (atom (merge references ahead-references))]
362+
(->> types
363+
(reduce (fn [acc [type-name type-node]]
364+
(debug (str "Processing type " type-name))
365+
(let [location-meta (meta type-node)
366+
type-node (common/purge-ast type-node)
367+
type-node (if (syntax/fragment? type-node)
368+
;; avoiding situations where we transform this into an include
369+
;; and then we cannot transform this back into type because there's
370+
;; no way to tell it without source maps
371+
{:type type-node}
372+
type-node)
373+
type-name (url/url-encode (utils/safe-str type-name))
374+
type-id (common/type-reference parsed-location type-name)
375+
type-fragment (parse-ast type-node (-> nested-context
376+
(assoc :references @working-references)
377+
(assoc :location location)
378+
(assoc :parsed-location type-id)
379+
(assoc :is-fragment false)
380+
(assoc :type-hint :type)))
381+
sources (or (-> type-fragment :sources) [])
382+
;; we annotate the parsed type with the is-type source map so we can distinguish it from other declarations
383+
sources (concat sources (common/generate-is-type-sources type-name
384+
(utils/path-join location type-name)
385+
type-id))
386+
parsed-type (assoc type-fragment :sources sources)
387+
parsed-type (if (nil? (:name parsed-type))
388+
(assoc parsed-type :name type-name)
389+
parsed-type)
390+
parsed-type (assoc parsed-type :lexical location-meta)]
391+
;; let's also update the working reference to this ahead declaration
392+
(swap! working-references (fn [old-working-references] (assoc old-working-references (keyword type-name) parsed-type)))
393+
(assoc acc (keyword (utils/alias-chain type-name context)) parsed-type)))
394+
{})))))
386395

387396
(defn find-extend-tags [{:keys [location parsed-location references] :as context}]
388397
(->> references
@@ -428,7 +437,9 @@
428437
:scheme (utils/ensure-not-blank (root->scheme node))
429438
:base-path (utils/ensure-not-blank (base-uri->basepath (extract-scalar (:baseUri node))))
430439
:accepts (filterv some? (flatten [(extract-scalar (:mediaType node))]))
431-
:content-type (filterv some? (flatten [(:mediaType node)]))
440+
:content-type (->> (flatten [(:mediaType node)])
441+
(map extract-scalar)
442+
(filterv some?))
432443
:version (extract-scalar (:version node))
433444
:provider nil
434445
:terms-of-service nil

0 commit comments

Comments
 (0)