OpenTool.fromJson constructor

OpenTool.fromJson(
  1. Map<String, dynamic> json, {
  2. SchemaReferenceResolver? schemaReferenceResolver,
})

Implementation

factory OpenTool.fromJson(
  Map<String, dynamic> json, {
  SchemaReferenceResolver? schemaReferenceResolver,
}) {
  final rawSchemas = json['schemas'];
  if (rawSchemas != null && rawSchemas is! Map<String, dynamic>) {
    throw const FormatException('schemas: expected an object.');
  }
  final schemaResolver = SchemaResolver.withReferenceResolver(
    definitions: rawSchemas as Map<String, dynamic>?,
    referenceResolver: schemaReferenceResolver,
  );
  final parsedSchemas = rawSchemas == null
      ? null
      : schemaResolver.resolveAll();

  final rawFunctions = json['functions'];
  if (rawFunctions is! List) {
    throw const FormatException('functions: expected an array.');
  }
  final functions = <FunctionModel>[];
  for (var i = 0; i < rawFunctions.length; i++) {
    final rawFunction = rawFunctions[i];
    if (rawFunction is! Map<String, dynamic>) {
      throw FormatException('functions[$i]: expected an object.');
    }
    functions.add(
      FunctionModel.fromJson(
        rawFunction,
        schemaResolver: schemaResolver,
        path: 'functions[$i]',
      ),
    );
  }

  return OpenTool(
    opentool: json['opentool'] as String,
    info: Info.fromJson(json['info'] as Map<String, dynamic>),
    server: json['server'] == null
        ? null
        : ServerConfig.fromJson(json['server'] as Map<String, dynamic>),
    functions: functions,
    schemas: parsedSchemas,
  );
}