Skip to content

Commit

Permalink
Handle more cases
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanoverna committed Aug 27, 2024
1 parent 16299ba commit 42ad919
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 45 deletions.
77 changes: 37 additions & 40 deletions generate/extractInfoFromSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ function findPropertiesInProperty(
schema: JsonRefParser.JSONSchema,
property: string,
): string[] {
if (schema.type === 'array') {
return [];
}

if (schema.type === 'object') {
if (!schema.properties || typeof schema.properties !== 'object') {
throw new Error('Ouch');
Expand Down Expand Up @@ -142,55 +146,51 @@ function findPropertiesInProperty(
throw new Error("Don't know how to handle!");
}

function findTypeInDataObject(dataSchema: JsonRefParser.JSONSchema) {
if (dataSchema.type !== 'object') {
throw new Error('Data not an object?');
}

if (typeof dataSchema.properties !== 'object') {
throw new Error('Missing data?');
}
function findTypeInDataObjects(
dataSchemas: JsonRefParser.JSONSchema[],
): string {
const types = dataSchemas.map(
(dataSchema) => (dataSchema.properties!.type as any).example as string,
);

if (
!dataSchema.properties.type ||
typeof dataSchema.properties.type !== 'object'
) {
throw new Error('Missing type?');
}
const uniqueTypes = [...new Set(types)];

return (dataSchema.properties.type as any).example as string;
return uniqueTypes.length === 1 ? uniqueTypes[0]! : '*';
}

function findTypeInDataProperty(schema: JsonRefParser.JSONSchema) {
function findDataObjects(schema: JsonRefParser.JSONSchema) {
if (typeof schema.properties?.data !== 'object') {
throw new Error('Missing data!');
}

let dataSchema = schema.properties.data;
function find(
maybeDataSchema: JsonRefParser.JSONSchema,
): JsonRefParser.JSONSchema[] {
if (maybeDataSchema.type === 'array') {
if (typeof maybeDataSchema.items !== 'object') {
throw new Error('No items?');
}
return find(maybeDataSchema.items);
}

if (schema.type === 'array') {
if (typeof schema.items !== 'object') {
throw new Error('No items?');
if (maybeDataSchema.anyOf) {
return maybeDataSchema.anyOf.flatMap((s) =>
find(s as JsonRefParser.JSONSchema),
);
}
dataSchema = schema.items!;
}

if (dataSchema.anyOf) {
const types = [
...new Set(
dataSchema.anyOf.map((s) =>
findTypeInDataObject(s as JsonRefParser.JSONSchema),
),
),
];
if (types.length !== 1) {
return '*';
if (maybeDataSchema.type !== 'object') {
throw new Error('Data not an object?');
}

return types[0]!;
return [maybeDataSchema];
}

return findTypeInDataObject(dataSchema);
return find(schema.properties.data);
}

function findTypeInDataProperty(schema: JsonRefParser.JSONSchema) {
return findTypeInDataObjects(findDataObjects(schema));
}

function findRequiredIdInDataObject(dataSchema: JsonRefParser.JSONSchema) {
Expand All @@ -216,13 +216,10 @@ function findIdInDataProperty(schema: JsonRefParser.JSONSchema) {
throw new Error('Missing data!');
}

let dataSchema = schema.properties.data;
const dataSchema = schema.properties.data;

if (schema.type === 'array') {
if (typeof schema.items !== 'object') {
throw new Error('No items?');
}
dataSchema = schema.items!;
if (dataSchema.type === 'array') {
return undefined;
}

if (dataSchema.anyOf) {
Expand Down
15 changes: 10 additions & 5 deletions generate/generateSimplifiedSchema.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
function simplifySchema(objectSchema: any) {
function simplifySchema(objectSchema: any, dropIdRequired: boolean) {
const { attributes, relationships, meta, id, type } =
objectSchema.properties as any;

return {
...objectSchema,
additionalProperties: Boolean(attributes && !attributes.properties),
required: [
...(objectSchema.required?.includes('id') && !dropIdRequired
? ['id']
: []),
...(objectSchema.required?.includes('attributes')
? attributes?.required || []
: []),
Expand Down Expand Up @@ -108,7 +111,7 @@ function simplifyEntityRelationships(schema: any) {
function applyToInnerObject(
name: string,
schema: any,
apply: (schema: any) => any,
apply: (schema: any, dropIdRequired: boolean) => any,
) {
if (!schema) {
return schema;
Expand All @@ -119,7 +122,7 @@ function applyToInnerObject(
}

if (schema.type === 'object') {
return apply(schema);
return apply(schema, true);
}

if (schema.anyOf) {
Expand All @@ -132,10 +135,12 @@ function applyToInnerObject(
if (schema.type === 'array') {
if (schema.items && Array.isArray(schema.items)) {
schema.items = schema.items.map((i: any) =>
applyToInnerObject(name, i, apply),
applyToInnerObject(name, i, (x) => apply(x, false)),
);
} else if (schema.items) {
schema.items = applyToInnerObject(name, schema.items, apply);
schema.items = applyToInnerObject(name, schema.items, (x) =>
apply(x, false),
);
}

return schema;
Expand Down

0 comments on commit 42ad919

Please sign in to comment.