Exemple #1
0
 private OpenApiSchema CreateReferenceSchema(Type type, SchemaRepository schemaRepository, Func <OpenApiSchema> factoryMethod)
 {
     return(schemaRepository.GetOrAdd(
                type: type,
                schemaId: _generatorOptions.SchemaIdSelector(type),
                factoryMethod: factoryMethod));
 }
 private OpenApiSchema GenerateReferencedSchema(SerializerMetadata serializerMetadata, SchemaRepository schemaRepository)
 {
     return(schemaRepository.GetOrAdd(
                serializerMetadata.Type,
                _generatorOptions.SchemaIdSelector(serializerMetadata.Type),
                () =>
     {
         var schema = GenerateInlineSchema(serializerMetadata, schemaRepository);
         ApplyFilters(schema, serializerMetadata.Type, schemaRepository);
         return schema;
     }));
 }
 private OpenApiSchema GenerateReferencedSchema(DataContract dataContract, SchemaRepository schemaRepository)
 {
     return(schemaRepository.GetOrAdd(
                dataContract.UnderlyingType,
                _generatorOptions.SchemaIdSelector(dataContract.UnderlyingType),
                () =>
     {
         var schema = GenerateInlineSchema(dataContract, schemaRepository);
         ApplyFilters(schema, dataContract.UnderlyingType, schemaRepository);
         return schema;
     }));
 }
Exemple #4
0
        public OpenApiSchema GenerateSchema(ApiModel apiModel, SchemaRepository schemaRepository)
        {
            if (!CanGenerateSchema(apiModel, out bool shouldBeReferenced))
            {
                return(Next.GenerateSchema(apiModel, schemaRepository));
            }

            if (shouldBeReferenced)
            {
                var schemaId = Options.SchemaIdSelector(apiModel.Type);

                return(schemaRepository.GetOrAdd(apiModel.Type,
                                                 schemaId,
                                                 () => GenerateDefinitionSchemaAndApplyFilters(apiModel, schemaRepository)));
            }

            return(GenerateDefinitionSchemaAndApplyFilters(apiModel, schemaRepository));
        }
        private OpenApiSchema CreateObjectSchema(Type type, SchemaRepository schemaRepository)
        {
            var schema = new OpenApiSchema
            {
                Type       = "object",
                Properties = new Dictionary <string, OpenApiSchema>(),
                Required   = new SortedSet <string>(),
                AdditionalPropertiesAllowed = false
            };

            // If it's a baseType with known subTypes, add the discriminator property
            if (_generatorOptions.GeneratePolymorphicSchemas && _generatorOptions.SubTypesResolver(type).Any())
            {
                var discriminatorName = _generatorOptions.DiscriminatorSelector(type);
                schema.Properties.Add(discriminatorName, new OpenApiSchema {
                    Type = "string"
                });
                schema.Required.Add(discriminatorName);
                schema.Discriminator = new OpenApiDiscriminator {
                    PropertyName = discriminatorName
                };
            }

            var serializableProperties = type.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
                                         .Where(property =>
            {
                return
                ((property.IsPubliclyReadable() || property.IsPubliclyWritable()) &&
                 !(property.GetIndexParameters().Any()) &&
                 !(property.HasAttribute <JsonIgnoreAttribute>()) &&
                 !(_serializerOptions.IgnoreReadOnlyProperties && !property.IsPubliclyWritable()));
            });

            foreach (var property in serializableProperties)
            {
                var customAttributes = property.GetInlineOrMetadataTypeAttributes();

                if (_generatorOptions.IgnoreObsoleteProperties && customAttributes.OfType <ObsoleteAttribute>().Any())
                {
                    continue;
                }

                var name = property.GetCustomAttribute <JsonPropertyNameAttribute>()?.Name
                           ?? _serializerOptions.PropertyNamingPolicy?.ConvertName(property.Name) ?? property.Name;

                schema.Properties[name] = CreatePropertySchema(property, customAttributes, schemaRepository);

                if (customAttributes.OfType <RequiredAttribute>().Any())
                {
                    schema.Required.Add(name);
                }

                if (property.HasAttribute <JsonExtensionDataAttribute>() && property.PropertyType.IsDictionary(out Type keyType, out Type valueType))
                {
                    schema.AdditionalPropertiesAllowed = true;
                    schema.AdditionalProperties        = _schemaGenerator.GenerateSchema(valueType, schemaRepository);
                }
            }


            // If it's a known subType, reference the baseType for inheritied properties
            if (_generatorOptions.GeneratePolymorphicSchemas && type.BaseType != null && _generatorOptions.SubTypesResolver(type.BaseType).Contains(type))
            {
                var baseType = type.BaseType;

                var baseSchemaReference = schemaRepository.GetOrAdd(
                    type: baseType,
                    schemaId: _generatorOptions.SchemaIdSelector(baseType),
                    factoryMethod: () => CreateObjectSchema(baseType, schemaRepository));

                var baseSchema = schemaRepository.Schemas[baseSchemaReference.Reference.Id];

                schema.AllOf = new[] { baseSchemaReference };

                foreach (var basePropertyName in baseSchema.Properties.Keys)
                {
                    schema.Properties.Remove(basePropertyName);
                }
            }

            return(schema);
        }