@Test(description = "it should deserialize a model with object example") public void deserializeModelWithObjectExample() throws IOException { final String json = "{\n" + " \"title\":\"Error\",\n" + " \"type\":\"object\",\n" + " \"properties\":{\n" + " \"code\":{\n" + " \"type\":\"integer\",\n" + " \"format\":\"int32\"\n" + " },\n" + " \"message\":{\n" + " \"type\":\"string\"\n" + " },\n" + " \"fields\":{\n" + " \"type\":\"string\"\n" + " }\n" + " },\n" + " \"example\":{\n" + " \"code\":1,\n" + " \"message\":\"hello\",\n" + " \"fields\":\"abc\"\n" + " }\n" + "}"; final Schema model = Json.mapper().readValue(json, Schema.class); assertEquals(Json.mapper().writeValueAsString(model.getExample()), "{\"code\":1,\"message\":\"hello\",\"fields\":\"abc\"}"); }
@Test(description = "it should read an example within an inlined schema") public void testIssue1261InlineSchemaExample() throws Exception { Operation operation = Yaml.mapper().readValue( " responses:\n" + " 200:\n" + " content:\n" + " '*/*':\n" + " description: OK\n" + " schema:\n" + " type: object\n" + " properties:\n" + " id:\n" + " type: integer\n" + " format: int32\n" + " name:\n" + " type: string\n" + " required: [id, name]\n" + " example: ok", Operation.class); ApiResponse response = operation.getResponses().get("200"); assertNotNull(response); Schema schema = response.getContent().get("*/*").getSchema(); Object example = schema.getExample(); assertNotNull(example); assertTrue(example instanceof String); assertEquals(example, "ok"); } }
.description(subtypeModel.getDescription()) .discriminator(subtypeModel.getDiscriminator()) .example(subtypeModel.getExample()) .exclusiveMaximum(subtypeModel.getExclusiveMaximum()) .exclusiveMinimum(subtypeModel.getExclusiveMinimum())
/** * Return the example value of the property * * @param property Schema property object * @return string presentation of the example value of the property */ public String toExampleValue(Schema property) { return String.valueOf(property.getExample()); }
/** * Return the example value of the property * * @param property Schema property object * @return string presentation of the example value of the property */ public String toExampleValue(Schema property) { return String.valueOf(property.getExample()); }
@Override public String toExampleValue(Schema p) { if (p.getExample() != null) { return escapeText(p.getExample().toString()); } else { return null; } }
/** * Return the example value of the property * * @param schema Open API Schema object * @return string presentation of the example value of the property */ @Override public String toExampleValue(Schema schema) { if (schema instanceof StringSchema) { if (schema.getExample() != null) { return String.format("\"%s\"", schema.getExample().toString()); } } if (schema instanceof DateSchema || schema instanceof DateTimeSchema) { // TODO still... return null; } else { if (schema.getExample() != null) { return schema.getExample().toString(); } } return null; }
@Override public String toExampleValue(Schema schemaProperty) { if(schemaProperty.getExample() != null) { return escapeText(schemaProperty.getExample().toString()); } else { return super.toExampleValue(schemaProperty); } }
@Override public String toExampleValue(Schema schemaProperty) { if(schemaProperty.getExample() != null) { return escapeText(schemaProperty.getExample().toString()); } else { return super.toExampleValue(schemaProperty); } }
/** * This function fix models that are string (mostly enum). Before this fix, the * example would look something like that in the doc: "\"example from def\"" * * @param m Schema implementation */ private void fixStringModel(Schema m) { if (m.getType() != null && m.getType().equals("string") && m.getExample() != null) { String example = m.getExample().toString(); if (example.substring(0, 1).equals("\"") && example.substring(example.length() - 1).equals("\"")) { m.setExample(example.substring(1, example.length() - 1)); } } }
/** * This function fix models that are string (mostly enum). Before this fix, the example * would look something like that in the doc: "\"example from def\"" * @param m Model implementation */ private void fixStringModel(Schema m) { if (m.getType() != null && m.getType().equals("string") && m.getExample() != null) { String example = m.getExample().toString(); if (example.substring(0, 1).equals("\"") && example.substring(example.length() - 1).equals("\"")) { m.setExample(example.substring(1, example.length() - 1)); } } }
/** * This function fix models that are string (mostly enum). Before this fix, the example * would look something like that in the doc: "\"example from def\"" * @param m Model implementation */ private void fixStringModel(Schema m) { if (m.getType() != null && m.getType().equals("string") && m.getExample() != null) { String example = m.getExample().toString(); if (example.substring(0, 1).equals("\"") && example.substring(example.length() - 1).equals("\"")) { m.setExample(example.substring(1, example.length() - 1)); } } }
/** * This function fix models that are string (mostly enum). Before this fix, the example * would look something like that in the doc: "\"example from def\"" * @param m Model implementation */ private void fixStringModel(Schema m) { if (m.getType() != null && m.getType().equals("string") && m.getExample() != null) { String example = m.getExample().toString(); if (example.substring(0, 1).equals("\"") && example.substring(example.length() - 1).equals("\"")) { m.setExample(example.substring(1, example.length() - 1)); } } }
private static Object generateExampleForMapProperty(MapSchema property, MarkupDocBuilder markupDocBuilder) { if (property.getExample() != null) { return property.getExample(); } Map<String, Object> exampleMap = new LinkedHashMap<>(); Schema valueProperty = (Schema) property.getAdditionalProperties(); if (valueProperty.getExample() != null) { return valueProperty.getExample(); } exampleMap.put("string", PropertyAdapter.generateExample(valueProperty, markupDocBuilder)); return exampleMap; }
private boolean isSchemaWithAdditionalValues(Schema schema) { return nonNull(schema.getDescription()) || nonNull(schema.getExample()) || nonNull(schema.getXml()) || nonNull(schema.getReadOnly()) || (nonNull(schema.getExtensions()) && !schema.getExtensions().isEmpty()); }
/** * Return the example value of the parameter. Overrides the * setParameterExampleValue(CodegenParameter, Parameter) method in * DefaultCodegen to always call setParameterExampleValue(CodegenParameter) * in this class, which adds single quotes around strings from the * x-example property. * * @param codegenParameter Codegen parameter * @param parameter Parameter */ public void setParameterExampleValue(CodegenParameter codegenParameter, Parameter parameter) { if (parameter.getExample() != null) { codegenParameter.example = parameter.getExample().toString(); } else if (parameter.getExamples() != null && !parameter.getExamples().isEmpty()) { Example example = parameter.getExamples().values().iterator().next(); if (example.getValue() != null) { codegenParameter.example = example.getValue().toString(); } } else { Schema schema = parameter.getSchema(); if (schema != null && schema.getExample() != null) { codegenParameter.example = schema.getExample().toString(); } } setParameterExampleValue(codegenParameter); }
@SuppressWarnings("static-method") public Schema modelFromProperty(Schema object, @SuppressWarnings("unused") String path) { String description = object.getDescription(); String example = null; Object obj = object.getExample(); if (obj != null) { example = obj.toString(); } ArraySchema model = new ArraySchema(); model.setDescription(description); model.setExample(example); if (object.getAdditionalProperties() != null && object.getAdditionalProperties() instanceof Schema) { model.setItems((Schema) object.getAdditionalProperties()); } return model; }
@SuppressWarnings("static-method") public Schema modelFromProperty(Schema object, @SuppressWarnings("unused") String path) { String description = object.getDescription(); String example = null; Object obj = object.getExample(); if (obj != null) { example = obj.toString(); } ArraySchema model = new ArraySchema(); model.setDescription(description); model.setExample(example); if (object.getAdditionalProperties() != null && object.getAdditionalProperties() instanceof Schema) { model.setItems((Schema) object.getAdditionalProperties()); } return model; }
@SuppressWarnings("static-method") public Schema modelFromProperty(Schema object, @SuppressWarnings("unused") String path) { String description = object.getDescription(); String example = null; Object obj = object.getExample(); if (obj != null) { example = obj.toString(); } ArraySchema model = new ArraySchema(); model.setDescription(description); model.setExample(example); if (object.getAdditionalProperties() != null && object.getAdditionalProperties() instanceof Schema) { model.setItems((Schema) object.getAdditionalProperties()); } return model; }
public AnyProperty(Schema prop) { if (prop != null) { setName(prop.getName()); setDescription(prop.getDescription()); setExample(prop.getExample()); setFormat(prop.getFormat()); setReadOnly(prop.getReadOnly()); setRequired(prop.getRequired()); setTitle(prop.getTitle()); setExtensions(prop.getExtensions()); setXml(prop.getXml()); setExternalDocs(prop.getExternalDocs()); setProperties(prop.getProperties()); set$ref(prop.get$ref()); } } }