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

Changes to avoid duplication of sub-array schema with same reference #116

Merged
merged 1 commit into from
Jul 24, 2023
Merged
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: 2 additions & 0 deletions languages/c/Types.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ function getSchemaTypeInfo(module = {}, json = {}, name = '', schemas = {}, pref
}


name = name.endsWith("_ArrayType") ? name.split('_ArrayType')[0] : name
prefix = prefix ? prefix + name : name
let n = getTypeName(getModuleName(module), capitalize(res.name), prefix)
structure.name = name ? name + capitalize(res.name) : res.name
Expand Down Expand Up @@ -539,6 +540,7 @@ function getSchemaShapeInfo(json, module, schemas = {}, { name = '', prefix = ''
}

if (!isCPP) {
name = name.endsWith("_ArrayType") ? name.split('_ArrayType')[0] : name
let subPrefix = prefix ? prefix + name : name
let info = getSchemaTypeInfo(module, j, j.title || name, schemas, subPrefix, {level : level, descriptions: descriptions, title: true})

Expand Down
25 changes: 20 additions & 5 deletions src/macrofier/engine.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import isString from 'crocks/core/isString.js'
import predicates from 'crocks/predicates/index.js'
const { isObject, isArray, propEq, pathSatisfies, propSatisfies } = predicates

import { isRPCOnlyMethod, isProviderInterfaceMethod, getProviderInterface, getPayloadFromEvent, providerHasNoParameters, isTemporalSetMethod, hasMethodAttributes, getMethodAttributes, isEventMethodWithContext, getSemanticVersion, getSetterFor, getProvidedCapabilities, isPolymorphicPullMethod, hasPublicAPIs, createPolymorphicMethods } from '../shared/modules.mjs'
import { isRPCOnlyMethod, isProviderInterfaceMethod, getProviderInterface, getPayloadFromEvent, providerHasNoParameters, isTemporalSetMethod, isCallsMetricsMethod, isExcludedMethod, hasMethodAttributes, getMethodAttributes, isEventMethodWithContext, getSemanticVersion, getSetterFor, getProvidedCapabilities, isPolymorphicPullMethod, hasPublicAPIs, createPolymorphicMethods } from '../shared/modules.mjs'
import isEmpty from 'crocks/core/isEmpty.js'
import { getLinkedSchemaPaths, getSchemaConstraints, isSchema, localizeDependencies, isDefinitionReferencedBySchema } from '../shared/json-schema.mjs'

Expand Down Expand Up @@ -356,9 +356,24 @@ const generateAggregateMacros = (openrpc, modules, templates, library) => Object
})

const addContentDescriptorSubSchema = (descriptor, prefix, obj) => {
const title = prefix.charAt(0).toUpperCase() + prefix.substring(1) + descriptor.name.charAt(0).toUpperCase() + descriptor.name.substring(1)
if (obj.components.schemas[title]) {
throw 'Generated name `' + title + '` already exists...'
let title = ''
if (descriptor.schema.type === 'array' && descriptor.schema.items && descriptor.schema.items['$ref']) {
let refName = descriptor.schema.items['$ref'].split('/').pop()
title = refName.charAt(0).toUpperCase() + refName.substring(1) + '_ArrayType'
if (obj.components.schemas[title]) {
descriptor.schema = {
$ref: "#/components/schemas/" + title
}
return
}
}
else {
let descriptorName = capitalize(descriptor.name)
let prefixName = capitalize(prefix)
title = (prefixName !== descriptorName) ? prefixName + descriptorName : descriptorName
if (obj.components.schemas[title]) {
throw 'Generated name `' + title + '` already exists...'
}
}
obj.components.schemas[title] = descriptor.schema
obj.components.schemas[title].title = title
Expand All @@ -367,7 +382,7 @@ const addContentDescriptorSubSchema = (descriptor, prefix, obj) => {
}
}

// only consider sub-objects and sub enums to be sub-schemas
// only consider sub-objects, sub-array and sub-enums to be sub-schemas
const isSubSchema = (schema) => schema.type === 'object' || (schema.type === 'string' && schema.enum) || (schema.type === 'array' && schema.items)

const promoteAndNameSubSchemas = (obj) => {
Expand Down
Loading