Example #1
0
        private void AddResponseFields(IList <ServiceFieldInfo> responseFields, string statusCode, SwaggerResponse swaggerResponse, string serviceMethodName, IList <ServiceAttributeParameterInfo> httpAttributeValues, bool isOnlyResponse, SwaggerService swaggerService, NamedTextPosition position)
        {
            var bodySchema = default(KeyValuePair <string, SwaggerSchema>);

            if (swaggerResponse.Schema != null)
            {
                bodySchema = swaggerService.ResolveDefinition(swaggerResponse.Schema, position);
            }

            if (bodySchema.Value != null && (bodySchema.Value.Type ?? SwaggerSchemaType.Object) == SwaggerSchemaType.Object &&
                (bodySchema.Key == null || bodySchema.Key.Equals(serviceMethodName + "Response", StringComparison.OrdinalIgnoreCase)))
            {
                httpAttributeValues.Add(new ServiceAttributeParameterInfo("code", statusCode, position));
                AddFieldsFromSchema(responseFields, swaggerService, position, bodySchema);
            }
            else if (swaggerResponse.Identifier == null && isOnlyResponse && swaggerResponse.Schema == null)
            {
                httpAttributeValues.Add(new ServiceAttributeParameterInfo("code", statusCode, position));
            }
            else
            {
                responseFields.Add(new ServiceFieldInfo(
                                       swaggerResponse.Identifier ?? CodeGenUtility.ToCamelCase(bodySchema.Key) ?? GetBodyFieldNameForStatusCode(statusCode),
                                       typeName: bodySchema.Key ?? (bodySchema.Value != null ? SwaggerUtility.FilterBodyTypeName(swaggerService.TryGetFacilityTypeName(bodySchema.Value, position)) : null) ?? "boolean",
                                       attributes: new[]
                {
                    new ServiceAttributeInfo("http",
                                             new[]
                    {
                        new ServiceAttributeParameterInfo("from", "body", position),
                        new ServiceAttributeParameterInfo("code", statusCode, position),
                    })
                },
                                       summary: PrepareSummary(swaggerResponse.Description),
                                       position: position));
            }
        }
Example #2
0
        internal static SwaggerResponse ResolveResponse(this SwaggerService swaggerService, SwaggerResponse swaggerResponse, NamedTextPosition position)
        {
            if (swaggerResponse.Ref != null)
            {
                const string refPrefix = "#/responses/";
                if (!swaggerResponse.Ref.StartsWith(refPrefix, StringComparison.Ordinal))
                {
                    throw new ServiceDefinitionException("Response $ref must start with '#/responses/'.", position);
                }

                string name = UnescapeRefPart(swaggerResponse.Ref.Substring(refPrefix.Length));
                if (!swaggerService.Responses.TryGetValue(name, out swaggerResponse))
                {
                    throw new ServiceDefinitionException($"Missing response named '{name}'.", position);
                }
            }

            return(swaggerResponse);
        }