private Schema CreatePrimitiveSchema(JsonPrimitiveContract primitiveContract) { var type = Nullable.GetUnderlyingType(primitiveContract.UnderlyingType) ?? primitiveContract.UnderlyingType; var typeInfo = type.GetTypeInfo(); if (typeInfo.IsEnum) { var converter = primitiveContract.Converter; var describeAsString = _options.DescribeAllEnumsAsStrings || (converter != null && converter.GetType() == typeof(StringEnumConverter)); return(describeAsString ? new Schema { Type = "string", Enum = Enum.GetNames(type) } : new Schema { Type = "integer", Format = "int32", Enum = Enum.GetValues(type).Cast <object>().ToArray() }); } if (PrimitiveTypeMap.ContainsKey(type)) { return(PrimitiveTypeMap[type]()); } // None of the above, fallback to string return(new Schema { Type = "string" }); }
public override OpenApiSchema CreateDefinitionSchema(Type type, SchemaRepository schemaRepository) { var isNullable = type.IsNullable(out Type innerType); var schema = isNullable ? PrimitiveTypeMap[innerType]() : PrimitiveTypeMap[type](); schema.Nullable = (!type.IsValueType || isNullable); return(schema); }
public override OpenApiSchema CreateDefinitionSchema(Type type, SchemaRepository schemaRepository) { if (!(_contractResolver.ResolveContract(type) is JsonPrimitiveContract jsonPrimitiveContract)) { throw new InvalidOperationException($"Type {type} does not resolve to a JsonPrimitiveContract"); } return(PrimitiveTypeMap.ContainsKey(type) ? PrimitiveTypeMap[type]() : new OpenApiSchema { Type = "string" }); }
protected override OpenApiSchema GenerateDefinitionSchema(ApiModel apiModel, SchemaRepository schemaRepository) { var apiPrimitive = (ApiPrimitive)apiModel; if (apiPrimitive.IsEnum) { return(GenerateEnumSchema(apiPrimitive)); } return(PrimitiveTypeMap.ContainsKey(apiPrimitive.Type) ? PrimitiveTypeMap[apiPrimitive.Type]() : PrimitiveTypeMap[typeof(string)]()); // if no mapping exists, default to string }
private Schema CreatePrimitiveSchema(JsonPrimitiveContract primitiveContract) { var type = Nullable.GetUnderlyingType(primitiveContract.UnderlyingType) ?? primitiveContract.UnderlyingType; if (type.GetTypeInfo().IsEnum) return CreateEnumSchema(primitiveContract, type); if (PrimitiveTypeMap.ContainsKey(type)) return PrimitiveTypeMap[type](); // None of the above, fallback to string return new Schema { Type = "string" }; }
private OpenApiSchema GenerateEnumSchema(ApiPrimitive apiPrimitive) { var schema = apiPrimitive.IsStringEnum ? PrimitiveTypeMap[typeof(string)]() : PrimitiveTypeMap[apiPrimitive.Type.GetEnumUnderlyingType()](); schema.Enum = apiPrimitive.ApiEnumValues .Select(value => { return(OpenApiAnyFactory.TryCreateFor(schema, value, out IOpenApiAny openApiAny) ? openApiAny : null); }) .ToList(); return(schema); }
public override bool CanCreateSchemaFor(Type type, out bool shouldBeReferenced) { if (PrimitiveTypeMap.ContainsKey(type) || (type.IsNullable(out Type innerType) && PrimitiveTypeMap.ContainsKey(innerType))) { shouldBeReferenced = false; return(true); } shouldBeReferenced = false; return(false); }
public override bool CanCreateSchemaFor(Type type, out bool shouldBeReferenced) { if (PrimitiveTypeMap.ContainsKey(type)) { shouldBeReferenced = false; return(true); } shouldBeReferenced = false; return(false); }
public override OpenApiSchema CreateDefinitionSchema(Type type, SchemaRepository schemaRepository) { var isNullable = type.IsNullable(out Type innerType); var schema = isNullable ? PrimitiveTypeMap[innerType]() : PrimitiveTypeMap[type](); if (_serializerSettings.NullValueHandling == NullValueHandling.Ignore) { schema.Nullable = false; } else { schema.Nullable = (!type.IsValueType || isNullable); } return(schema); }
private Schema CreatePrimitiveSchema(JsonPrimitiveContract primitiveContract) { // If Nullable<T>, use the type argument var type = primitiveContract.UnderlyingType.IsNullable() ? Nullable.GetUnderlyingType(primitiveContract.UnderlyingType) : primitiveContract.UnderlyingType; if (type.GetTypeInfo().IsEnum) { return(CreateEnumSchema(primitiveContract, type)); } if (PrimitiveTypeMap.ContainsKey(type)) { return(PrimitiveTypeMap[type]()); } // None of the above, fallback to string return(new Schema { Type = "string" }); }
private string generateDefinition(Type target) { if (PrimitiveTypeMap.ContainsKey(target)) { return(PrimitiveTypeMap[target]); } var jsonContract = _jsonContractResolver.ResolveContract(target); if (jsonContract is JsonPrimitiveContract) { var primitiveContract = (JsonPrimitiveContract)jsonContract; var type = Nullable.GetUnderlyingType(primitiveContract.UnderlyingType) ?? primitiveContract.UnderlyingType; // TODO: if (type.GetTypeInfo().IsEnum) ... if (PrimitiveTypeMap.ContainsKey(type)) { return(PrimitiveTypeMap[type]); } } if (jsonContract is JsonDictionaryContract) { var dictContract = (JsonDictionaryContract)jsonContract; var kType = Generate(dictContract.DictionaryKeyType); var vType = Generate(dictContract.DictionaryValueType); return($"{{ [k: {kType}]: {vType} }}"); } if (jsonContract is JsonArrayContract) { var arrayContract = (JsonArrayContract)jsonContract; return($"{Generate(arrayContract.CollectionItemType)}[]"); } if (jsonContract is JsonObjectContract) { var objectContract = (JsonObjectContract)jsonContract; var propTypes = new List <string>(); foreach (var p in objectContract.Properties) { propTypes.Add(p.PropertyName + " : " + Generate(p.PropertyType)); } return($"{{ {string.Join(", ", propTypes)} }}"); } return("any"); }
private ParameterInfo ResolveParameter(CXType originalType, string name = null, int index = 0) { string renamed; var type = originalType; if (type.kind == CXTypeKind.CXType_FunctionProto) { throw new NotImplementedException(); } if (type.kind == CXTypeKind.CXType_FunctionNoProto) { throw new NotImplementedException(); } var typeKind = CanonizeType(ref type, out var typeDeclCursor); if (typeKind == CXTypeKind.CXType_Pointer) { var pointeeType = clang.getPointeeType(type); if (clang.getFunctionTypeCallingConv(pointeeType) != CXCallingConv.CXCallingConv_Invalid) { var delegateTypeName = originalType.ToString(); var possibleDelegateType = Module.GetType(delegateTypeName); if (possibleDelegateType != null) { return(new ParameterInfo(name, possibleDelegateType, index)); } return(new ParameterInfo(name, IncompleteTypeReference.Get(Module, null, delegateTypeName), index)); } var resolvedParameter = ResolveParameter(pointeeType); return(new ParameterInfo(name, resolvedParameter.Type.MakePointerType(), index)); } if (typeKind == CXTypeKind.CXType_DependentSizedArray) { throw new NotImplementedException(); } if (typeKind == CXTypeKind.CXType_ConstantArray) { var arraySize = (int)clang.getArraySize(type); var elementType = clang.getArrayElementType(type); var resolvedParameter = ResolveParameter(elementType, name); var clrElementType = resolvedParameter.Type; if (clrElementType.IsPointer) { clrElementType = Module.TypeSystem.IntPtr; } var arrayType = resolvedParameter.Type.MakeArrayType(); if (!PrimitiveUnmanagedTypeMap.TryGetValue(clrElementType.GetRuntimeType(), out var unmanagedType)) { throw new NotImplementedException(); } return(new ParameterInfo(name, arrayType, index, ParameterAttributes.None, arraySize)); } if (PrimitiveTypeMap.TryGetValue(typeKind, out var primitiveType)) { if (primitiveType == null) { throw new NotImplementedException(); } var originalTypeName = originalType.ToString(); var typeName = originalTypeName; if (TypeRedirects.TryGetValue(originalTypeName, out renamed)) { typeName = renamed; } if (originalType.kind == CXTypeKind.CXType_Typedef) { if (KnownTypes.ContainsKey(typeName)) { var knownType = Module.GetType(typeName) ?? Module.GetType(originalTypeName) ?? throw new NotImplementedException(); return(new ParameterInfo(name, knownType, index)); } } else { var found = Module.GetType(typeName); if (found != null) { return(new ParameterInfo(name, found, index)); } } return(new ParameterInfo(name, primitiveType.Import(Module), index)); } var typeDeclName = typeDeclCursor.ToString(); if (TypeRedirects.TryGetValue(typeDeclName, out renamed)) { typeDeclName = renamed; } var possibleType = Module.GetType(typeDeclName); if (possibleType != null) { return(new ParameterInfo(name, possibleType, index)); } return(new ParameterInfo(name, IncompleteTypeReference.Get(Module, null, typeDeclName), index)); }
private IClangType ParseTypeDef(CXCursor cursor) { var originalType = clang.getCursorType(cursor); var canonType = clang.getCanonicalType(originalType); var typeDeclCursor = clang.getTypeDeclaration(canonType); if (IsCursorInSystemHeader(typeDeclCursor)) { return(null); } var name = cursor.ToString(); if (typeDeclCursor.kind == CXCursorKind.CXCursor_NoDeclFound) { if (canonType.kind != CXTypeKind.CXType_Pointer) { // likely simple type alias if (TypeRedirects.TryGetValue(name, out var renamed)) { name = renamed; } if (KnownTypes.TryGetValue(name, out var knownType)) { if (PrimitiveTypeMap.TryGetValue(canonType.kind, out var primitiveType)) { var existingType = Module.GetType(name); if (existingType == null) { throw new NotImplementedException(); } switch (knownType) { case KnownType.Bitmask: case KnownType.Enum: { existingType.ChangeUnderlyingType(primitiveType.Import(Module)); break; } default: break; } IncrementStatistic("typedefs"); } else { throw new NotImplementedException(); } } return(null); } var pointeeType = clang.getPointeeType(canonType); var callConv = clang.getFunctionTypeCallingConv(pointeeType); if (callConv == CXCallingConv.CXCallingConv_Invalid) { // likely a pointer type alias return(null); } return(ParseDelegate(cursor, callConv)); } switch (typeDeclCursor.kind) { case CXCursorKind.CXCursor_UnionDecl: case CXCursorKind.CXCursor_StructDecl: { var typeName = typeDeclCursor.ToString(); if (name == typeName) { return(null); } throw new NotImplementedException(); } case CXCursorKind.CXCursor_EnumDecl: { if (TypeRedirects.TryGetValue(name, out var renamed)) { name = renamed; } if (KnownTypes.TryGetValue(name, out var knownType)) { var existingType = Module.GetType(name); if (existingType != null) { return(null); } switch (knownType) { case KnownType.Enum: { throw new NotImplementedException(); } case KnownType.Bitmask: { throw new NotImplementedException(); } default: throw new NotImplementedException(); } } throw new NotImplementedException(); } } IncrementStatistic("typedefs"); throw new NotImplementedException(); }
public override OpenApiSchema CreateDefinitionSchema(Type type, SchemaRepository schemaRepository) { return(PrimitiveTypeMap[type]()); }