extractInlineResponseSchemas method
Map<String, dynamic>
extractInlineResponseSchemas(
)
Implementation
Map<String, dynamic> extractInlineResponseSchemas() {
final paths = getPaths();
final Map<String, dynamic> inlineSchemas = {};
paths.forEach((path, methods) {
final cleanPath =
path
.split('/')
.where(
(segment) => segment.isNotEmpty && !segment.startsWith('{'),
)
.join('')
.capitalize;
final defaultModelName =
cleanPath.isNotEmpty ? '${cleanPath}Response' : 'GenericResponse';
methods.forEach((method, details) {
final responses = details['responses'] ?? {};
responses.forEach((code, responseDetail) {
final schema =
responseDetail['content']?['application/json']?['schema'];
final description = responseDetail['description'] ?? '';
if (schema == null) {
inlineSchemas[defaultModelName] = {
"type": "object",
"properties": {
"message": {
"type": "string",
"description":
description.isNotEmpty
? description
: "Operation completed successfully",
},
},
};
return;
}
if (schema['type'] == 'object' && schema['\$ref'] == null) {
inlineSchemas[defaultModelName] = schema;
} else if (schema['type'] == 'array' &&
schema['items']?['\$ref'] != null) {
final ref = schema['items']['\$ref'].split('/').last;
final listModelName =
'${ref.toString().pluralToSingular}ListResponse';
inlineSchemas[listModelName] = {
"type": "object",
"properties": {
"items": {
"type": "array",
"items": {"\$ref": "#/components/schemas/$ref"},
},
},
};
} else if (schema['type'] == 'array' &&
schema['items'] != null &&
schema['items']['\$ref'] == null &&
schema['items']['type'] == 'object') {
final listModelName = '${defaultModelName}ItemListResponse';
final itemModelName = '${defaultModelName}Item';
inlineSchemas[itemModelName] = schema['items'];
inlineSchemas[listModelName] = {
"type": "object",
"properties": {
"items": {
"type": "array",
"items": {"\$ref": "#/components/schemas/$itemModelName"},
},
},
};
}
});
});
});
return inlineSchemas;
}