/// <summary> /// Get CodeTypeReference and description of requestBody of operation. /// </summary> /// <param name="op"></param> /// <returns>bool is whether to support generating codes for this.</returns> public Tuple <CodeTypeReference, string, bool> GetBodyContent(OpenApiOperation op, string httpMethod, string path) { if (op.RequestBody != null && op.RequestBody.Content != null) { OpenApiMediaType content; string description = op.RequestBody.Description; if (op.RequestBody.Reference != null) { if (op.RequestBody.Content.TryGetValue("application/json", out content) && (content.Schema.Type != null && content.Schema.Type != "object")) { try { return(Tuple.Create(com2CodeDom.PropertySchemaToCodeTypeReference(content.Schema, actionName, httpMethod + "Body"), description, true)); } catch (ArgumentException ex) { throw new CodeGenException($"Definition {path}=>{httpMethod} for op.RequestBody.Reference triggers error: {ex.Message}"); } } var ns = NameFunc.GetNamespaceOfClassName(op.RequestBody.Reference.Id); string typeName = NameFunc.RefineTypeName(op.RequestBody.Reference.Id, ns); CodeTypeReference codeTypeReference = new CodeTypeReference(NameFunc.CombineNamespaceWithClassName(ns, typeName)); return(Tuple.Create(codeTypeReference, description, true)); } else if (op.RequestBody.Content.TryGetValue("application/json", out content)) { if (content.Schema != null) { return(Tuple.Create(com2CodeDom.PropertySchemaToCodeTypeReference(content.Schema, actionName, "Body"), description, true)); } } else if (op.RequestBody.Content.Count > 0) // with content but not supported { return(Tuple.Create <CodeTypeReference, string, bool>(null, null, false)); } } return(null); //empty post }
/// <summary> /// /// </summary> /// <param name="op"></param> /// <returns>item2 indicates whether return is a string.</returns> public Tuple <CodeTypeReference, bool> GetOperationReturnTypeReference(OpenApiOperation op) { if (op.Responses.TryGetValue("200", out OpenApiResponse goodResponse)) { CodeTypeReference codeTypeReference; if (goodResponse.Content.TryGetValue("application/json", out OpenApiMediaType content)) // application/json has better to be first. { if (content == null || content.Schema == null) { return(Tuple.Create <CodeTypeReference, bool>(null, false)); } try { codeTypeReference = com2CodeDom.PropertySchemaToCodeTypeReference(content.Schema, actionName, "Return"); } catch (ArgumentException ex) { throw new CodeGenException(ex.Message) { Pending = true }; } return(Tuple.Create(codeTypeReference, false)); } if (goodResponse.Content.TryGetValue("text/plain", out content)) { if (content.Schema != null) { string schemaType = content.Schema.Type; if (schemaType != null) { string schemaFormat = content.Schema.Format; Type type = TypeRefHelper.PrimitiveSwaggerTypeToClrType(schemaType, schemaFormat); return(Tuple.Create(new CodeTypeReference(type), type == typeOfString)); } } } } return(Tuple.Create <CodeTypeReference, bool>(null, false)); }
public CodeTypeReference OpenApiParameterSchemaToCodeTypeReference(OpenApiSchema apiParameterSchema, string apiParameterName) { return(com2CodeDom.PropertySchemaToCodeTypeReference(apiParameterSchema, actionName, apiParameterName)); }