Skip to content
This repository was archived by the owner on Mar 16, 2025. It is now read-only.

Unknown formats #26

Merged
merged 3 commits into from
Oct 10, 2020
Merged
Show file tree
Hide file tree
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 @@ -213,8 +213,8 @@ class DataTypeConverter(
}

var typeFormat = schemaInfo.getType()
if (schemaInfo.getFormat() != null) {
typeFormat += "/" + schemaInfo.getFormat()
if (isSupportedFormat(schemaInfo.getFormat())) {
typeFormat += ":" + schemaInfo.getFormat()
}

// todo factory method in SchemaInfo
Expand All @@ -231,29 +231,42 @@ class DataTypeConverter(

return when(typeFormat) {
"integer",
"integer/int32" ->
"integer:int32" ->
IntegerDataType(constraints, schemaInfo.getDeprecated())
"integer/int64" ->
"integer:int64" ->
LongDataType(constraints, schemaInfo.getDeprecated())
"number",
"number/float" ->
"number:float" ->
FloatDataType(constraints, schemaInfo.getDeprecated())
"number/double" ->
"number:double" ->
DoubleDataType(constraints, schemaInfo.getDeprecated())
"boolean" ->
BooleanDataType(constraints, schemaInfo.getDeprecated())
"string" ->
createStringDataType(schemaInfo, constraints, dataTypes)
"string/date" ->
"string:date" ->
LocalDateDataType(constraints, schemaInfo.getDeprecated())
"string/date-time" ->
"string:date-time" ->
OffsetDateTimeDataType (constraints, schemaInfo.getDeprecated())
else ->
throw UnknownDataTypeException(schemaInfo.getName(), schemaInfo.getType(),
schemaInfo.getFormat())
}
}

private fun isSupportedFormat(format: String?): Boolean {
if(format == null)
return false

return format in listOf(
"int32",
"int64",
"float",
"double",
"date",
"date-time")
}

private fun createStringDataType(info: SchemaInfo, constraints: DataTypeConstraints, dataTypes: DataTypes): DataType {
if (!info.isEnum()) {
return StringDataType(constraints, info.getDeprecated())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
package io.openapiprocessor.core.parser.swagger

import io.openapiprocessor.core.misc.toURL
import io.openapiprocessor.core.parser.OpenApi as ParserOpenApi
import io.swagger.v3.parser.OpenAPIV3Parser
import io.swagger.v3.parser.core.models.ParseOptions
import io.swagger.v3.parser.core.models.SwaggerParseResult
import io.openapiprocessor.core.parser.OpenApi as ParserOpenApi

const val SCHEME_RESOURCE = "resource:"

Expand All @@ -35,10 +35,10 @@ open class Parser {
val opts = ParseOptions()
// loads $refs to other files into #/components/schema and replaces the $refs to the
// external files with $refs to #/components/schema.
opts.setResolve(true)
opts.isResolve = true

val result: SwaggerParseResult = OpenAPIV3Parser()
.readLocation (preparePath (apiPath), null, opts)
.readLocation(preparePath(apiPath), null, opts)

return OpenApi(result)
}
Expand All @@ -49,16 +49,16 @@ open class Parser {
// If it is something different (or nothing) it tries to find the given path as-is on the
// file system. If that fails it tries to load the path as resource.

if (isResource (path)) {
if (isResource(path)) {
// strip resource: (only used by tests) to load test files from the resources
return path.substring (SCHEME_RESOURCE.length)
return path.substring(SCHEME_RESOURCE.length)
}

return toURL(path).toString ()
}

private fun isResource(path: String): Boolean {
return path.startsWith (SCHEME_RESOURCE)
return path.startsWith(SCHEME_RESOURCE)
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright © 2020 https://github.com/openapi-processor/openapi-processor-core
* PDX-License-Identifier: Apache-2.0
*/

package io.openapiprocessor.core.converter

import io.kotest.core.spec.IsolationMode
import io.kotest.core.spec.style.StringSpec
import io.kotest.data.forAll
import io.kotest.data.row
import io.kotest.matchers.shouldBe
import io.mockk.every
import io.mockk.mockk
import io.openapiprocessor.core.converter.mapping.MappingFinder
import io.openapiprocessor.core.model.DataTypes
import io.openapiprocessor.core.parser.RefResolver
import io.openapiprocessor.core.parser.Schema

class DataTypeConverterPrimitiveSpec: StringSpec({
isolationMode = IsolationMode.InstancePerTest

val converter = DataTypeConverter(ApiOptions(), MappingFinder())
val resolver = mockk<RefResolver>()

"ignores unknown primitive data type format" {
forAll(
row("string","unknown", "String"),
row("integer","unknown", "Integer"),
row("number","unknown", "Float"),
row("boolean","unknown", "Boolean")
) { type, format, dataTypeName ->
val schema = mockk<Schema>(relaxed = true)
every { schema.getRef() } returns null
every { schema.getType() } returns type
every { schema.getFormat() } returns format

// when:
val info = SchemaInfo("", "foo", schema = schema, resolver = resolver)
val datatype = converter.convert(info, DataTypes())

// then:
datatype.getName() shouldBe dataTypeName
}

}

})