Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/avro #81

Merged
merged 11 commits into from
Aug 28, 2024
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,7 @@ dist/
# AMF models
/demo/*.json
!demo/apis.json
!demo/avro.json
!demo/avro2.json

.idea/
617 changes: 617 additions & 0 deletions demo/avro.json

Large diffs are not rendered by default.

2,053 changes: 2,053 additions & 0 deletions demo/avro2.json

Large diffs are not rendered by default.

7 changes: 5 additions & 2 deletions demo/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class ApiDemo extends ApiDemoPage {

_navChanged(e) {
const { selected, type } = e.detail;
this.hasType = false;
this.hasType = false;
this.mediaType = undefined;
this.mediaTypes = undefined;

Expand All @@ -40,7 +40,7 @@ class ApiDemo extends ApiDemoPage {
}

setBodyData(id) {
const webApi = this._computeWebApi(this.amf);
const webApi = this._computeWebApi(this.amf) || this._computeApi(this.amf);
const method = this._computeMethodModel(webApi, id);
const expects = this._computeExpects(method);
const payload = expects ? this._computePayload(expects)[0] : {};
Expand Down Expand Up @@ -100,6 +100,9 @@ class ApiDemo extends ApiDemoPage {
_apiListTemplate() {
return [
['demo-api', 'Demo API'],
['avro', 'avro'],
['avro2', 'avro2'],
['jldAsync26', 'jldAsync26'],
['APIC-649', 'Deprecated properties'],
['APIC-429', 'APIC 429'],
['read-only-properties', 'Read Only Properties API'],
Expand Down
18 changes: 9 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@api-components/api-type-document",
"description": "A documentation table for type (resource) properties. Works with AMF data model",
"version": "4.2.29",
"version": "4.2.30",
"license": "Apache-2.0",
"main": "index.js",
"module": "index.js",
Expand Down Expand Up @@ -99,4 +99,4 @@
"eslint --fix"
]
}
}
}
20 changes: 14 additions & 6 deletions src/ApiTypeDocument.js
Original file line number Diff line number Diff line change
Expand Up @@ -560,9 +560,9 @@ export class ApiTypeDocument extends PropertyDocumentMixin(LitElement) {
if (Array.isArray(item)) {
return item;
}

const propertyKey = this._getAmfKey(this.ns.w3.shacl.property);
const itemProperties = this._ensureArray(item[propertyKey])
const itemProperties = this._ensureArray(item[propertyKey]||[])
const additionalPropertiesKey = this._getAmfKey(this.ns.w3.shacl.additionalPropertiesSchema);

// If the item doesn't have additional properties, filter the read-only properties and return
Expand All @@ -571,9 +571,9 @@ export class ApiTypeDocument extends PropertyDocumentMixin(LitElement) {
}

const additionalPropertiesSchema = this._ensureArray(item[additionalPropertiesKey])

// If the item does have additional properties, ensure they are in an array
const additionalProperties = this._ensureArray(additionalPropertiesSchema[0][propertyKey])
const additionalProperties = this._ensureArray(additionalPropertiesSchema[0][propertyKey] || additionalPropertiesSchema[0])

// Combine the item's properties and additional properties
const combinedProperties = [...itemProperties, ...additionalProperties]
Expand Down Expand Up @@ -819,6 +819,14 @@ export class ApiTypeDocument extends PropertyDocumentMixin(LitElement) {
return this._multiTypeTemplate({ label, items, typeName, selected, selectTypeCallback, type });
}

_getItemLabel(item){
if(item.label==='Unknown type' && item.avroValue){
return item.avroValue
}
return item.label

}

/**
*
* @param {Object} args
Expand All @@ -843,8 +851,8 @@ export class ApiTypeDocument extends PropertyDocumentMixin(LitElement) {
aria-pressed="${selected === index ? 'true' : 'false'}"
@click="${selectTypeCallback}"
?compatibility="${this.compatibility}"
title="Select ${item.label} type"
>${item.label}</anypoint-button
title="Select ${this._getItemLabel(item)} type"
>${this._getItemLabel(item)}</anypoint-button
>`
)}
</div>
Expand Down
38 changes: 38 additions & 0 deletions src/PropertyDocumentMixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ const mxFunction = (base) => {
return 'String';
case this._getAmfKey(sc.integer):
case sc.integer:
case sc.int:
return 'Integer';
case this._getAmfKey(sc.long):
case sc.long:
Expand Down Expand Up @@ -205,6 +206,10 @@ const mxFunction = (base) => {
case this._getAmfKey(rs.password):
case rs.password:
return 'Password';
case sc.bytes:
return 'Bytes'
case sc.fixed:
return 'Fixed'
default:
return 'Unknown type';
}
Expand Down Expand Up @@ -356,6 +361,37 @@ const mxFunction = (base) => {
return this._hasProperty(range, this.ns.w3.shacl.and);
}

/**
* Computes source values of the property. Only for async / avro
*
* @param {Object} data Range object of current shape.
*/
_computeAvroSourceMap(data) {
try{
const sourcesKey = this._getAmfKey(this.ns.aml.vocabularies.docSourceMaps.sources)
const avroSchemaKey = this._getAmfKey(this.ns.aml.vocabularies.docSourceMaps.avroSchema)
const valueKey = this._getAmfKey(this.ns.aml.vocabularies.docSourceMaps.value)
if(data[sourcesKey] && data[sourcesKey][0][avroSchemaKey]){
const avroValues = this._ensureArray(data[sourcesKey][0][avroSchemaKey])
return avroValues[0][valueKey][0]['@value']
}
return undefined
}catch(_){
return undefined
}

}

/**
* Computes source values of the property. Only for async / avro
*
* @param {Object} data Range object of current shape.
* @return {Object} Size of the property
*/
_computeAvroShapeRangeSourceMap(data) {
return this._computeAvroSourceMap(data)
}

/**
* Computes list of type labels to render.
*
Expand All @@ -369,6 +405,7 @@ const mxFunction = (base) => {
return undefined;
}
return list.map((obj) => {
const avroValue = this._computeAvroShapeRangeSourceMap(obj) || null
let item = obj;
if (Array.isArray(item)) {
[item] = item;
Expand Down Expand Up @@ -419,6 +456,7 @@ const mxFunction = (base) => {
isArray,
isType,
label,
avroValue
};
});
}
Expand Down
Loading
Loading