Skip to content

Commit

Permalink
feat: added templates for Java + Spring. added new params for generation
Browse files Browse the repository at this point in the history
  • Loading branch information
myznikov committed Feb 5, 2025
1 parent f88b42b commit 37b7f0c
Show file tree
Hide file tree
Showing 16 changed files with 291 additions and 40 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// OperationGenerationModel.swift
//
//
//
// Created by Александр Кравченков on 19.10.2021.
//
Expand Down Expand Up @@ -63,11 +63,13 @@ public struct OperationGenerationModel: Encodable {
public let requestModel: Reference<RequestModel>?

public let pathParameters: [ParameterModel]
public let headerParameters: [ParameterModel]
public let queryParameters: [ParameterModel]
public let requestGenerationModel: DataGenerationModel?
public let responseGenerationModel: Keyed<DataGenerationModel>?

public let allGenerationResponses: [ResponseGenerationModel]?
public let id: String?

init(operationModel: OperationModel) {
self.httpMethod = operationModel.httpMethod
Expand All @@ -82,6 +84,7 @@ public struct OperationGenerationModel: Encodable {
.map { $0.value }
.sorted { $0.name < $1.name }
self.pathParameters = allParameters.filter { $0.location == .path }
self.headerParameters = allParameters.filter { $0.location == .header }
self.queryParameters = allParameters.filter { $0.location == .query }

let request = operationModel.requestModel?.value
Expand All @@ -108,6 +111,7 @@ public struct OperationGenerationModel: Encodable {
responses: response.values.map { DataGenerationModel(dataModel: $0) }
)
}
self.id = operationModel.id
}
}

Expand Down
20 changes: 19 additions & 1 deletion Sources/CodeGenerator/Models/PropertyModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,26 @@ public struct PropertyModel {
public let description: String?
public let type: PossibleType
public let isNullable: Bool
public let example: Any?
public let format: String?
public let minimum: Double?
public let maximum: Double?
public let maxLength: Int?
public let minLength: Int?

/// This value will be used as type for generation
public let typeModel: ItemTypeModel

init(name: String,
description: String?,
type: PropertyModel.PossibleType,
isNullable: Bool) {
isNullable: Bool,
example: Any?,
format: String?,
minimum: Double?,
maximum: Double?,
maxLength: Int?,
minLength: Int?) {
self.name = name
self.description = description
self.type = type
Expand All @@ -96,6 +108,12 @@ public struct PropertyModel {
isObject: type.isObject,
enumTypeName: type.enumTypeName,
aliasTypeName: type.aliasTypeName)
self.example = example
self.format = format
self.minimum = minimum
self.maximum = maximum
self.minLength = minLength
self.maxLength = maxLength
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// OperationModel.swift
//
//
//
// Created by Александр Кравченков on 17.12.2020.
//
Expand Down Expand Up @@ -56,19 +56,21 @@ public struct OperationModel: Encodable {
public let parameters: [Reference<ParameterModel>]?
public let responses: [Reference<ResponseModel>]?
public let requestModel: Reference<RequestModel>?
public let id: String?

init(httpMethod: String,
summary: String?,
description: String?,
parameters: [Reference<ParameterModel>]?,
responses: [Reference<ResponseModel>]?,
requestModel: Reference<RequestModel>?) {
requestModel: Reference<RequestModel>?,
id: String?) {
self.httpMethod = httpMethod
self.summary = summary

self.description = description
self.parameters = parameters
self.responses = responses
self.requestModel = requestModel
self.id = id
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -190,12 +190,24 @@ public class Resolver {
return .init(name: property.name,
description: property.description,
type: .array(.init(name: "", itemsType: try arrayItemTypeUnwrapper(arr.itemsType))),
isNullable: property.nullable)
isNullable: property.nullable,
example: property.example,
format: property.format,
minimum: property.minimum,
maximum: property.maximum,
maxLength: property.maxLength,
minLength: property.minLength)
case .simple(let val):
return .init(name: property.name,
description: property.description,
type: try propertyTypeUnwrapper(val),
isNullable: property.nullable)
isNullable: property.nullable,
example: property.example,
format: property.format,
minimum: property.minimum,
maximum: property.maximum,
maxLength: property.maxLength,
minLength: property.minLength)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// File.swift
//
//
//
// Created by Александр Кравченков on 17.12.2020.
//
Expand Down Expand Up @@ -67,7 +67,7 @@ public struct TreeParser {
func parse(operation: OperationNode, current: DependencyWithTree, other: [DependencyWithTree]) throws -> OperationModel {

let params = try operation.parameters.map { parameter -> Reference<ParameterModel> in

return try wrap(
self.parametersParser.parse(parameter: parameter, current: current, other: other),
message: "While parsing parameter \(parameter.view)")
Expand All @@ -94,7 +94,8 @@ public struct TreeParser {
description: operation.description,
parameters: params,
responses: responses,
requestModel: requestBody
requestModel: requestBody,
id: operation.id
)
}
}
60 changes: 49 additions & 11 deletions Sources/GASTBuilder/Builders/AnySchemaBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -161,25 +161,63 @@ public struct AnySchemaBuilder: SchemaBuilder {

func build(object: ObjectSchema, meta: Metadata, name: String, apiDefinitionFileRef: String) throws -> SchemaModelNode {
let properties = try object.properties.map { property -> PropertyNode in
let type = try wrap(property.schema.extractType(),
message: "In object \(name), in property \(property.name)")

let schema = property.schema
let type = try wrap(
schema.extractType(),
message: "In object \(name), in property \(property.name)"
)

var isNullable = property.isNullable

if self.useNewNullableDeterminationStrategy {
isNullable = property.schema.metadata.nullable
isNullable = schema.metadata.nullable
}

var format: String?
var minimum: Double?
var maximum: Double?
var minLength: Int?
var maxLength: Int?

return PropertyNode(name: property.name,
type: type,
description: property.schema.metadata.description,
example: property.schema.metadata.example,
nullable: isNullable)
switch schema.type {
case .string(let stringSchema):
format = stringSchema.format?.rawValue
minLength = stringSchema.minLength
maxLength = stringSchema.maxLength

case .number(let numberSchema):
format = numberSchema.format?.rawValue
minimum = numberSchema.minimum
maximum = numberSchema.maximum

case .integer(let integerSchema):
format = integerSchema.format?.rawValue
minimum = integerSchema.minimum.flatMap(Double.init)
maximum = integerSchema.maximum.flatMap(Double.init)

default:
break
}

return PropertyNode(
name: property.name,
type: type,
description: schema.metadata.description,
example: schema.metadata.example,
nullable: isNullable,
format: format,
minimum: minimum,
maximum: maximum,
maxLength: maxLength,
minLength: minLength
)
}

return SchemaModelNode(name: name, properties: properties, description: meta.description, apiDefinitionFileRef: apiDefinitionFileRef)
return SchemaModelNode(
name: name,
properties: properties,
description: meta.description,
apiDefinitionFileRef: apiDefinitionFileRef
)
}

func build(array: ArraySchema, name: String, apiDefinitionFileRef: String) throws -> SchemaArrayNode {
Expand Down
8 changes: 4 additions & 4 deletions Sources/GASTBuilder/Builders/AnyServiceBuilder.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// AnyServiceBuilder.swift
//
//
//
// Created by Александр Кравченков on 14.12.2020.
//
Expand All @@ -17,7 +17,7 @@ public protocol ServiceBuilder {
}

/// Default implementation for `ServiceBuilder`
///
///
/// Builds `path` elements of Open-API spec
///
/// **WARNING**
Expand All @@ -34,7 +34,6 @@ public struct AnyServiceBuilder: ServiceBuilder {
let schemaBuilder: SchemaBuilder
let requestBodyBuilder: RequestBodyBuilder
let responseBuilder: ResponseBuilder

public init(parameterBuilder: ParametersBuilder,
schemaBuilder: SchemaBuilder,
requestBodyBuilder: RequestBodyBuilder,
Expand Down Expand Up @@ -93,7 +92,8 @@ extension AnyServiceBuilder {
summary: operation.summary,
parameters: params,
requestBody: requestBody,
responses: responses)
responses: responses,
id: operation.generatedIdentifier)
}
}

Expand Down
17 changes: 16 additions & 1 deletion Sources/GASTTree/PropertyNode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,32 @@ public struct PropertyNode {
public let description: String?
public let example: Any?
public let nullable: Bool
public let format: String?
public let minimum: Double?
public let maximum: Double?
public let maxLength: Int?
public let minLength: Int?

public init(name: String,
type: PossibleType,
description: String?,
example: Any?,
nullable: Bool) {
nullable: Bool,
format: String?,
minimum: Double?,
maximum: Double?,
maxLength: Int?,
minLength: Int?) {
self.name = name
self.type = type
self.description = description
self.example = example
self.nullable = nullable
self.format = format
self.minimum = minimum
self.maximum = maximum
self.minLength = minLength
self.maxLength = maxLength
}
}

Expand Down
7 changes: 5 additions & 2 deletions Sources/GASTTree/Services/OperationNode.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// OperationNode.swift
//
//
//
// Created by Александр Кравченков on 14.12.2020.
//
Expand All @@ -27,18 +27,21 @@ public struct OperationNode {
public let parameters: [Referenced<ParameterNode>]
public let requestBody: Referenced<RequestBodyNode>?
public let responses: [ResponseBody]
public let id: String?

public init(method: String,
description: String?,
summary: String?,
parameters: [Referenced<ParameterNode>],
requestBody: Referenced<RequestBodyNode>?,
responses: [ResponseBody]) {
responses: [ResponseBody],
id: String?) {
self.method = method
self.description = description
self.summary = summary
self.parameters = parameters
self.requestBody = requestBody
self.responses = responses
self.id = id
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// OperationGenerationModel.swift
//
//
//
// Created by Dmitry Demyanov on 04.11.2020.
//
Expand All @@ -11,7 +11,7 @@ enum HttpMethod: String {
case patch
case put
case delete

var name: String {
switch self {
case .get, .post, .delete:
Expand All @@ -29,7 +29,7 @@ enum ResponseBody: Equatable {
}

public struct OperationGenerationModel {

private enum Constants {
static let multipartModel = "MultipartModel"
}
Expand All @@ -46,19 +46,21 @@ public struct OperationGenerationModel {

let hasBody: Bool
var requestBody: RequestBodyGenerationModel?
let id: String?

private(set) var hasUndefinedResponseBody = false
private(set) var hasResponseModel = false
private(set) var responseModel: String?

init(name: String,
description: String?,
path: PathGenerationModel,
httpMethod: String,
pathParameters: [ParameterGenerationModel],
queryParameters: [ParameterGenerationModel],
requestBody: RequestBodyGenerationModel.BodyType?,
responseBody: ResponseBody) {
responseBody: ResponseBody,
id: String?) {
self.name = name
self.hasDescription = description != nil
self.description = description
Expand All @@ -70,7 +72,7 @@ public struct OperationGenerationModel {
self.queryParameters = queryParameters
self.hasBody = requestBody != nil
self.requestBody = RequestBodyGenerationModel(type: requestBody)

self.id = id
switch responseBody {
case .model(let modelName):
self.hasResponseModel = true
Expand Down
Loading

0 comments on commit 37b7f0c

Please sign in to comment.