private OpenApiSchema GenerateReferencedSchema(
            DataContract dataContract,
            SchemaRepository schemaRepository,
            Func <OpenApiSchema> definitionFactory,
            ParameterInfo parameterInfo)
        {
            var  dataContractType   = dataContract.UnderlyingType;
            var  baseControllerType = typeof(BaseApiController);
            var  baseParameterType  = typeof(RepresentationParameter);
            var  schemaId           = _generatorOptions.SchemaIdSelector(dataContract.UnderlyingType);
            Type controllerType     = null;

            if (baseParameterType.IsAssignableFrom(dataContract.UnderlyingType))
            {
                if (baseControllerType.IsAssignableFrom(parameterInfo.Member.ReflectedType))
                {
                    schemaId         = $"{parameterInfo.Member.ReflectedType.Name}{schemaId}";
                    controllerType   = parameterInfo.Member.ReflectedType;
                    dataContractType = controllerType;
                }
            }

            if (schemaRepository.TryLookupByType(dataContractType, out OpenApiSchema referenceSchema))
            {
                return(referenceSchema);
            }

            schemaRepository.RegisterType(dataContractType, schemaId);

            var schema = definitionFactory();

            ApplyFilters(schema, dataContractType, schemaRepository);
            if (controllerType != null)
            {
                var controller = (BaseApiController)_serviceProvider.GetService(controllerType);
                var scimSchema = _scimSchemaQueryRepository.FindRootSCIMSchemaByResourceType(controller.ResourceType).Result;
                if (scimSchema != null)
                {
                    Enrich(scimSchema, scimSchema.HierarchicalAttributes.Select(a => a.Leaf).ToList(), schema.Properties);
                }
                else
                {
                    _logger.LogError($"the schema '{controller.ResourceType}' doesn't exist !");
                }

                var kvp = schema.Properties.FirstOrDefault(_ => _.Key == "attributes");
                if (!kvp.Equals(default(KeyValuePair <string, OpenApiSchema>)) && !string.IsNullOrWhiteSpace(kvp.Key))
                {
                    schema.Properties.Remove(kvp);
                }
            }

            return(schemaRepository.AddDefinition(schemaId, schema));
        }
示例#2
0
        private OpenApiSchema GenerateReferencedSchema(
            DataContract dataContract,
            SchemaRepository schemaRepository,
            Func <OpenApiSchema> definitionFactory)
        {
            if (schemaRepository.TryLookupByType(dataContract.UnderlyingType, out OpenApiSchema referenceSchema))
            {
                return(referenceSchema);
            }

            var schemaId = _generatorOptions.SchemaIdSelector(dataContract.UnderlyingType);

            schemaRepository.RegisterType(dataContract.UnderlyingType, schemaId);

            var schema = definitionFactory();

            ApplyFilters(schema, dataContract.UnderlyingType, schemaRepository);

            return(schemaRepository.AddDefinition(schemaId, schema));
        }