private OpenApiSchema GeneratePropertySchema(DataProperty serializerMember, SchemaRepository schemaRepository) { var schema = GenerateSchemaForType(serializerMember.MemberType, schemaRepository); if (serializerMember.MemberInfo != null) { ApplyMemberMetadata(schema, serializerMember.MemberType, serializerMember.MemberInfo); } if (schema.Reference == null) { schema.Nullable = serializerMember.IsNullable && schema.Nullable; schema.ReadOnly = serializerMember.IsReadOnly; schema.WriteOnly = serializerMember.IsWriteOnly; ApplyFilters(schema, serializerMember.MemberType, schemaRepository, serializerMember.MemberInfo); } return(schema); }
private OpenApiSchema GenerateSchemaForMember( Type modelType, SchemaRepository schemaRepository, MemberInfo memberInfo, DataProperty dataProperty = null) { var dataContract = GetDataContractFor(modelType); var schema = _generatorOptions.UseOneOfForPolymorphism && IsBaseTypeWithKnownTypesDefined(dataContract, out var knownTypesDataContracts) ? GeneratePolymorphicSchema(dataContract, schemaRepository, knownTypesDataContracts) : GenerateConcreteSchema(dataContract, schemaRepository); if (_generatorOptions.UseAllOfToExtendReferenceSchemas && schema.Reference != null) { schema.AllOf = new[] { new OpenApiSchema { Reference = schema.Reference } }; schema.Reference = null; } if (schema.Reference == null) { var customAttributes = memberInfo.GetInlineAndMetadataAttributes(); // Nullable, ReadOnly & WriteOnly are only relevant for Schema "properties" (i.e. where dataProperty is non-null) if (dataProperty != null) { schema.Nullable = _generatorOptions.SupportNonNullableReferenceTypes ? dataProperty.IsNullable && !customAttributes.OfType <RequiredAttribute>().Any() && !memberInfo.IsNonNullableReferenceType() : dataProperty.IsNullable && !customAttributes.OfType <RequiredAttribute>().Any(); schema.ReadOnly = dataProperty.IsReadOnly; schema.WriteOnly = dataProperty.IsWriteOnly; } var defaultValueAttribute = customAttributes.OfType <DefaultValueAttribute>().FirstOrDefault(); if (defaultValueAttribute != null) { var defaultAsJson = dataContract.JsonConverter(defaultValueAttribute.Value); schema.Default = OpenApiAnyFactory.CreateFromJson(defaultAsJson); } var obsoleteAttribute = customAttributes.OfType <ObsoleteAttribute>().FirstOrDefault(); if (obsoleteAttribute != null) { schema.Deprecated = true; } // NullableAttribute behaves diffrently for Dictionaries if (modelType.IsGenericType && modelType.GetGenericTypeDefinition() == typeof(Dictionary <,>)) { schema.AdditionalProperties.Nullable = !memberInfo.IsDictionaryValueNonNullable(); } schema.ApplyValidationAttributes(customAttributes); ApplyFilters(schema, modelType, schemaRepository, memberInfo: memberInfo); } return(schema); }