Example #1
0
        protected override OpenApiSchema GenerateSchemaFor(ModelMetadata modelMetadata, SchemaRepository schemaRepository, JsonContract jsonContract)
        {
            var jsonDictionaryContract = (JsonDictionaryContract)jsonContract;

            var keyType   = jsonDictionaryContract.DictionaryKeyType ?? typeof(object);
            var valueType = jsonDictionaryContract.DictionaryValueType ?? typeof(object);

            if (keyType.IsEnum)
            {
                // This is a special case where we can include named properties based on the enum values
                return(new OpenApiSchema
                {
                    Type = "object",
                    Properties = jsonDictionaryContract.DictionaryKeyType.GetEnumNames()
                                 .ToDictionary(
                        name => name,
                        name => SchemaGenerator.GenerateSchema(modelMetadata.GetMetadataForType(valueType), schemaRepository)
                        )
                });
            }

            return(new OpenApiSchema
            {
                Type = "object",
                AdditionalPropertiesAllowed = true,
                AdditionalProperties = SchemaGenerator.GenerateSchema(modelMetadata.GetMetadataForType(valueType), schemaRepository)
            });
        }
Example #2
0
        protected override OpenApiSchema GenerateSchemaFor(ModelMetadata modelMetadata, SchemaRepository schemaRepository, JsonContract jsonContract)
        {
            var jsonArrayContract = (JsonArrayContract)jsonContract;

            var itemsType = jsonArrayContract.CollectionItemType ?? typeof(object);

            return(new OpenApiSchema
            {
                Type = "array",
                Items = SchemaGenerator.GenerateSchema(modelMetadata.GetMetadataForType(itemsType), schemaRepository),
                UniqueItems = jsonArrayContract.UnderlyingType.IsSet() ? (bool?)true : null
            });
        }
        protected override OpenApiSchema GenerateSchemaFor(ModelMetadata modelMetadata, SchemaRepository schemaRepository, JsonContract jsonContract)
        {
            var schema = new OpenApiSchema
            {
                OneOf = new List <OpenApiSchema>()
            };

            foreach (var subType in SchemaGeneratorOptions.SubTypesResolver(modelMetadata.ModelType))
            {
                var subTypeMetadata = modelMetadata.GetMetadataForType(subType);
                var subSchema       = SchemaGenerator.GenerateSchema(subTypeMetadata, schemaRepository);

                schema.OneOf.Add(subSchema);
            }

            return(schema);
        }
Example #4
0
        private OpenApiSchema GeneratePropertySchemaFor(
            ModelMetadata modelMetadata,
            SchemaRepository schemaRepository,
            JsonProperty jsonProperty)
        {
            var schema = SchemaGenerator.GenerateSchema(modelMetadata, schemaRepository);

            // Only apply contextual metadata (e.g. property attributes etc.) if it's an inline definition
            if (schema.Reference == null)
            {
                schema.ApplyCustomAttributes(modelMetadata.GetCustomAttributes());

                schema.WriteOnly = jsonProperty.Writable && !jsonProperty.Readable;
                schema.ReadOnly  = jsonProperty.Readable && !jsonProperty.Writable;
            }

            return(schema);
        }
Example #5
0
        protected override OpenApiSchema GenerateSchemaFor(ModelMetadata modelMetadata, SchemaRepository schemaRepository, JsonContract jsonContract)
        {
            var jsonObjectContract = (JsonObjectContract)jsonContract;

            var additionalProperties = (jsonObjectContract.ExtensionDataValueType != null)
                ? SchemaGenerator.GenerateSchema(modelMetadata.GetMetadataForType(jsonObjectContract.ExtensionDataValueType), schemaRepository)
                : null;

            var schema = new OpenApiSchema
            {
                Type       = "object",
                Properties = new Dictionary <string, OpenApiSchema>(),
                Required   = new SortedSet <string>(),
                AdditionalPropertiesAllowed = (additionalProperties != null),
                AdditionalProperties        = additionalProperties
            };

            foreach (var jsonProperty in jsonObjectContract.Properties)
            {
                var propertyMetadata = modelMetadata.Properties[jsonProperty.UnderlyingName]
                                       ?? modelMetadata.GetMetadataForType(jsonProperty.PropertyType);

                var propertyAttributes = propertyMetadata.GetCustomAttributes();

                if (propertyAttributes.OfType <ObsoleteAttribute>().Any() || jsonProperty.Ignored)
                {
                    continue;
                }

                schema.Properties.Add(jsonProperty.PropertyName, GeneratePropertySchemaFor(propertyMetadata, schemaRepository, jsonProperty));

                if (propertyAttributes.OfType <RequiredAttribute>().Any() ||
                    jsonProperty.Required == Required.AllowNull ||
                    jsonProperty.Required == Required.Always)
                {
                    schema.Required.Add(jsonProperty.PropertyName);
                }
            }

            return(schema);
        }
Example #6
0
 public JsonArrayHandler(SchemaGeneratorOptions schemaGeneratorOptions, SchemaGenerator schemaGenerator, JsonSerializerSettings jsonSerializerSettings)
     : base(schemaGeneratorOptions, schemaGenerator, jsonSerializerSettings)
 {
 }
Example #7
0
 public PolymorphicTypeHandler(SchemaGeneratorOptions options, SchemaGenerator generator)
     : base(options, generator)
 {
 }