Skip to content

feature/#54-circular-reference #467

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ namespace Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Visitors
/// </summary>
public class ObjectTypeVisitor : TypeVisitor
{
private readonly Dictionary<Type, OpenApiSchemaAcceptor> visitedTypes = new Dictionary<Type, OpenApiSchemaAcceptor>();

private readonly HashSet<Type> _noVisitableTypes = new HashSet<Type>
{
typeof(Guid),
Expand Down Expand Up @@ -118,7 +120,25 @@ public override void Visit(IAcceptor acceptor, KeyValuePair<string, Type> type,
.Where(p => !p.ExistsCustomAttribute<JsonIgnoreAttribute>())
.ToDictionary(p => p.GetJsonPropertyName(namingStrategy), p => p);

this.ProcessProperties(instance, name, properties, namingStrategy);
// Get subAcceptor
OpenApiSchemaAcceptor subAcceptor;
if (!this.visitedTypes.ContainsKey(type.Value))
{
subAcceptor = new OpenApiSchemaAcceptor
{
Properties = properties,
RootSchemas = instance.RootSchemas,
Schemas = new Dictionary<string, OpenApiSchema>(),
};
this.visitedTypes.Add(type.Value, subAcceptor);
subAcceptor.Accept(this.VisitorCollection, namingStrategy);
}
else
{
subAcceptor = this.visitedTypes[type.Value];
}

this.ProcessProperties(instance, subAcceptor, name, properties, namingStrategy);

// Adds the reference.
var reference = new OpenApiReference()
Expand Down Expand Up @@ -172,19 +192,8 @@ public override OpenApiSchema PayloadVisit(Type type, NamingStrategy namingStrat
return this.PayloadVisit(dataType: "object", dataFormat: null);
}

private void ProcessProperties(IOpenApiSchemaAcceptor instance, string schemaName, Dictionary<string, PropertyInfo> properties, NamingStrategy namingStrategy)
private void ProcessProperties(IOpenApiSchemaAcceptor instance, IOpenApiSchemaAcceptor subAcceptor, string schemaName, Dictionary<string, PropertyInfo> properties, NamingStrategy namingStrategy)
{
var schemas = new Dictionary<string, OpenApiSchema>();

var subAcceptor = new OpenApiSchemaAcceptor()
{
Properties = properties,
RootSchemas = instance.RootSchemas,
Schemas = schemas,
};

subAcceptor.Accept(this.VisitorCollection, namingStrategy);

// Add required properties to schema.
var jsonPropertyAttributes = properties.Where(p => !p.Value.GetCustomAttribute<JsonPropertyAttribute>(inherit: false).IsNullOrDefault())
.Select(p => new KeyValuePair<string, JsonPropertyAttribute>(p.Key, p.Value.GetCustomAttribute<JsonPropertyAttribute>(inherit: false)))
Expand Down Expand Up @@ -226,6 +235,7 @@ private void ProcessProperties(IOpenApiSchemaAcceptor instance, string schemaNam
var schemasToBeAdded = subAcceptor.Schemas
.Where(p => !instance.Schemas.Keys.Contains(p.Key))
.Where(p => p.Value.IsOpenApiSchemaObject())
.Where(p => !string.IsNullOrEmpty(p.Value.Title))
.GroupBy(p => p.Value.Title)
.Select(p => p.First())
.ToDictionary(p => p.Value.Title, p => p.Value);
Expand All @@ -239,17 +249,6 @@ private void ProcessProperties(IOpenApiSchemaAcceptor instance, string schemaNam

instance.RootSchemas.Add(schema.Key, schema.Value);
}

// Removes title of each property.
var subSchemas = instance.Schemas[schemaName].Properties;
subSchemas = subSchemas.Select(p =>
{
p.Value.Title = null;
return new KeyValuePair<string, OpenApiSchema>(p.Key, p.Value);
})
.ToDictionary(p => p.Key, p => p.Value);

instance.Schemas[schemaName].Properties = subSchemas;
}

private IOpenApiAny GetExample(Type type, NamingStrategy namingStrategy = null)
Expand Down