Example #1
0
 public void Read(OpenApiReadConfiguration configuration)
 {
     foreach (OpenApiDocument document in transferObjects.OfType <TransferObject <OpenApiDocument> >().Select(x => x.Value).Where(x => x.Components != null).ToList())
     {
         foreach (OpenApiSchema type in document.Components.Schemas.Values)
         {
             this.Read(configuration, type);
         }
         this.Read(configuration, document);
     }
 }
Example #2
0
 public void Read(OpenApiReadConfiguration configuration)
 {
     Logger.Trace("Read OpenApi...");
     if (configuration.File != null)
     {
         this.fileReader.Read(configuration.File);
     }
     if (configuration.Url != null)
     {
         this.urlReader.Read(configuration.File);
     }
     this.documentReader.Read(configuration);
 }
Example #3
0
        public void Read(ConfigurationBase configurationBase, List <ITransferObject> transferObjects)
        {
            Logger.Trace("Read OpenApi...");
            OpenApiReadConfiguration configuration = (OpenApiReadConfiguration)configurationBase;

            if (configuration.File != null)
            {
                this.fileReader.Read(configuration.File, transferObjects);
            }
            if (configuration.Url != null)
            {
                this.urlReader.Read(configuration.File, transferObjects);
            }
            this.documentReader.Read(configuration, transferObjects);
        }
Example #4
0
        private TypeTransferObject Read(OpenApiReadConfiguration configuration, OpenApiSchema schema)
        {
            if (schema.Reference == null)
            {
                return(new TypeTransferObject
                {
                    Name = schema.Type
                });
            }

            ModelTransferObject model = transferObjects.OfType <ModelTransferObject>().FirstOrDefault(x => x.Name == schema.Reference.Id);

            if (model != null)
            {
                return(model);
            }

            if (schema.Type == "array")
            {
                return(new TypeTransferObject
                {
                    Name = schema.Type,
                    Generics =
                    {
                        new GenericAliasTransferObject {
                            Type = this.Read(configuration, schema.Items)
                        }
                    }
                });
            }
            if (schema.Type == "object")
            {
                model = new ModelTransferObject
                {
                    Name      = schema.Reference.Id,
                    Namespace = configuration.Namespace,
                    Language  = OpenApiLanguage.Instance
                };
                foreach (KeyValuePair <string, OpenApiSchema> propertyPair in schema.Properties)
                {
                    PropertyTransferObject property = new PropertyTransferObject
                    {
                        Name = propertyPair.Key,
                        Type = this.Read(configuration, propertyPair.Value)
                    };

                    if (configuration.DataAnnotations && propertyPair.Value.MaxLength.HasValue)
                    {
                        property.Attributes.Add(new AttributeTransferObject
                        {
                            Name      = "MaxLength",
                            Namespace = "System.ComponentModel.DataAnnotations",
                            Value     = propertyPair.Value.MaxLength.Value
                        });
                    }
                    model.Properties.Add(property);
                }

                transferObjects.Add(model);
                return(model);
            }
            throw new InvalidOperationException($"Unknown schema type {schema.Type}");
        }
Example #5
0
        private void Read(OpenApiReadConfiguration configuration, OpenApiDocument document)
        {
            HttpServiceTransferObject service = new HttpServiceTransferObject
            {
                Name = configuration.Name ?? document.Info.Description.Replace("Class ", "").Trim()
            };

            foreach (KeyValuePair <string, OpenApiPathItem> pathPair in document.Paths)
            {
                foreach (KeyValuePair <OperationType, OpenApiOperation> operationPair in pathPair.Value.Operations)
                {
                    HttpServiceActionTypeTransferObject type   = operationPair.Key.ToActionType();
                    HttpServiceActionTransferObject     action = new HttpServiceActionTransferObject
                    {
                        Name  = pathPair.Key.Split('/').Last(),
                        Route = pathPair.Key,
                        Type  = type,
                        RequireBodyParameter = type.IsBodyParameterRequired()
                    };
                    foreach (OpenApiParameter parameter in operationPair.Value.Parameters)
                    {
                        action.Parameters.Add(new HttpServiceActionParameterTransferObject
                        {
                            Name = parameter.Name,
                            Type = this.Read(configuration, parameter.Schema)
                        });
                    }
                    OpenApiMediaType content = operationPair.Value.RequestBody.Content.Single().Value;
                    if (action.RequireBodyParameter)
                    {
                        action.Parameters.Add(new HttpServiceActionParameterTransferObject
                        {
                            Name     = "request",
                            Type     = this.Read(configuration, content.Schema),
                            FromBody = true
                        });
                    }
                    else
                    {
                        foreach (KeyValuePair <string, OpenApiSchema> propertyPair in content.Schema.Properties)
                        {
                            action.Parameters.Add(new HttpServiceActionParameterTransferObject
                            {
                                Name = propertyPair.Key,
                                Type = this.Read(configuration, propertyPair.Value)
                            });
                        }
                    }
                    OpenApiResponse response = operationPair.Value.Responses.SingleOrDefault(x => x.Key == "200").Value;
                    if (response == null)
                    {
                        action.ReturnType = new TypeTransferObject {
                            Name = "void"
                        };
                    }
                    else
                    {
                        action.ReturnType = this.Read(configuration, response.Content.Single().Value.Schema);
                    }
                    service.Actions.Add(action);
                }
            }
        }